<commit_before>// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
	"context"
	"io"
	"time"

	"github.com/google/gapid/core/event"
	"github.com/google/gapid/core/event/task"
	"github.com/google/gapid/core/log"
	"github.com/google/gapid/core/log/log_pb"
	"github.com/google/gapid/core/net/grpcutil"
	"github.com/google/gapid/gapis/api"
	"github.com/google/gapid/gapis/service"
	"github.com/google/gapid/gapis/service/path"
	"github.com/google/gapid/gapis/stringtable"
	"github.com/pkg/errors"
	"google.golang.org/grpc"
)

// Client is the interface used for service calls.
type Client interface {
	service.Service

	// Close closes the client connection.
	Close() error
}

// Bind creates a new rpc client using conn for communication.
func Bind(conn *grpc.ClientConn) Client {
	return &client{service.NewGapidClient(conn), conn.Close}
}

// New creates a new client using c for communication.
func New(c service.GapidClient) service.Service {
	return &client{c, func() error { return nil }}
}

type client struct {
	client service.GapidClient
	close  func() error
}

func (c *client) Close() error { return c.close() }

func (c *client) Ping(ctx context.Context) error {
	_, err := c.client.Ping(ctx, &service.PingRequest{})
	return err
}

func (c *client) GetServerInfo(ctx context.Context) (*service.ServerInfo, error) {
	res, err := c.client.GetServerInfo(ctx, &service.GetServerInfoRequest{})
	if err != nil {
		return nil, err
	}
	if err := res.GetError(); err != nil {
		return nil, err.Get()
	}
	return res.GetInfo(), nil
}

func (c *client) CheckForUpdates(ctx context.Context, includePrereleases bool) (*service.Release, error) {
	res, err := c.client.CheckForUpdates(ctx, &service.CheckForUpdatesRequest{
		IncludePrereleases: includePrereleases,
	})
	if err != nil {
		return nil, err
	}
	if err := res.GetError(); err != nil {
		return nil, err.Get()
	}
	return res.GetRelease(), nil
}

func (c *client) Get(ctx context.Context, p *path.Any, r *path.ResolveConfig) (interface{}, error) {
	res, err := c.client.Get(ctx, &service.GetRequest{
		Path:   p,
		Config: r,
	})
	if err != nil {
		return nil, err
	}
	if err := res.GetError(); err != nil {
		return nil, err.Get()
	}
	return res.GetValue().Get(), nil
}

func (c *client) Set(ctx context.Context, p *path.Any, v interface{}, r *path.ResolveConfig) (*path.Any, error) {
	res, err := c.client.Set(ctx, &service.SetRequest{
		Path:   p,
		Value:  service.NewValue(v),
		Config: r,
	})
	if err != nil {
		return nil, err
	}
	return res.GetPath(), nil
}

func (c *client) Follow(ctx context.Context, p *path.Any, r *path.ResolveConfig) (*path.Any, error) {
	res, err := c.client.Follow(ctx, &service.FollowRequest{
		Path:   p,
		Config: r,
	})
	if err != nil {
		return nil, err
	}
	if err := res.GetError(); err != nil {
		return nil, err.Get()
	}
	return res.GetPath(), nil
}

func (c *client) Profile(<commit_msg>Fix error handling in gapis Set grpc primitive (#2576)<commit_after>// Copyright (C) 2017 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package client

import (
	"context"
	"io"
	"time"

	"github.com/google/gapid/core/event"
	"github.com/google/gapid/core/event/task"
	"github.com/google/gapid/core/log"
	"github.com/google/gapid/core/log/log_pb"
	"github.com/google/gapid/core/net/grpcutil"
	"github.com/google/gapid/gapis/api"
	"github.com/google/gapid/gapis/service"
	"github.com/google/gapid/gapis/service/path"
	"github.com/google/gapid/gapis/stringtable"
	"github.com/pkg/errors"
	"google.golang.org/grpc"
)

// Client is the interface used for service calls.
type Client interface {
	service.Service

	// Close closes the client connection.
	Close() error
}

// Bind creates a new rpc client using conn for communication.
func Bind(conn *grpc.ClientConn) Client {
	return &client{service.NewGapidClient(conn), conn.Close}
}

// New creates a new client using c for communication.
func New(c service.GapidClient) service.Service {
	return &client{c, func() error { return nil }}
}

type client struct {
	client service.GapidClient
	close  func() error
}

func (c *client) Close() error { return c.close() }

func (c *client) Ping(ctx context.Context) error {
	_, err := c.client.Ping(ctx, &service.PingRequest{})
	return err
}

func (c *client) GetServerInfo(ctx context.Context) (*service.ServerInfo, error) {
	res, err := c.client.GetServerInfo(ctx, &service.GetServerInfoRequest{})
	if err != nil {
		return nil, err
	}
	if err := res.GetError(); err != nil {
		return nil, err.Get()
	}
	return res.GetInfo(), nil
}

func (c *client) CheckForUpdates(ctx context.Context, includePrereleases bool) (*service.Release, error) {
	res, err := c.client.CheckForUpdates(ctx, &service.CheckForUpdatesRequest{
		IncludePrereleases: includePrereleases,
	})
	if err != nil {
		return nil, err
	}
	if err := res.GetError(); err != nil {
		return nil, err.Get()
	}
	return res.GetRelease(), nil
}

func (c *client) Get(ctx context.Context, p *path.Any, r *path.ResolveConfig) (interface{}, error) {
	res, err := c.client.Get(ctx, &service.GetRequest{
		Path:   p,
		Config: r,
	})
	if err != nil {
		return nil, err
	}
	if err := res.GetError(); err != nil {
		return nil, err.Get()
	}
	return res.GetValue().Get(), nil
}

func (c *client) Set(ctx context.Context, p *path.Any, v interface{}, r *path.ResolveConfig) (*path.Any, error) {
	res, err := c.client.Set(ctx, &service.SetRequest{
		Path:   p,
		Value:  service.NewValue(v),
		Config: r,
	})
	if err != nil {
		return nil, err
	}
	if err := res.GetError(); err != nil {
		return nil, err.Get()
	}
	return res.GetPath(), nil
}

func (c *client) Follow(ctx context.Context, p *path.Any, r *path.ResolveConfig) (*path.Any, error) {
	res, err := c.client.Follow(ctx, &service.FollowRequest{
		Path:   p,
		Config: r,
	})
	if err != nil {
		return nil, err
	}
	if err := res.GetError(); err != nil {
		return nil, err.Get()
	}
	return res.GetPath(), nil
}

func (c *client) Profile(
{'fraction_non_alphanumeric': 0.14377498572244432, 'fraction_numerical': 0.0028555111364934323, 'mean_word_length': 3.814432989690722, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4701652', 'n_tokens_mistral': 2478, 'n_tokens_neox': 2399, 'n_words': 715}