api

package
v0.0.0-...-efd12d5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 4, 2024 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package api provides a basic client library for the Sourcegraph API.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NullInt

func NullInt(n int) *int

NullInt returns a nullable int for use in a GraphQL variable, where -1 is treated as a nil value.

func NullString

func NullString(s string) *string

NullString returns a nullable string for use in a GraphQL variable, where "" is treated as a nil value.

Types

type Client

type Client interface {
	// NewQuery is a convenience method to create a GraphQL request without
	// variables.
	NewQuery(query string) Request

	// NewRequest creates a GraphQL request.
	NewRequest(query string, vars map[string]interface{}) Request

	// NewHTTPRequest creates an http.Request for the Sourcegraph API.
	//
	// path is joined against the API route. For example on Sourcegraph.com this
	// will result the URL: https://sourcegraph.com/.api/path.
	NewHTTPRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error)

	// Do runs an http.Request against the Sourcegraph API.
	Do(req *http.Request) (*http.Response, error)
}

Client instances provide methods to create API requests.

func NewClient

func NewClient(opts ClientOpts) Client

NewClient creates a new API client.

type ClientOpts

type ClientOpts struct {
	Endpoint          string
	AccessToken       string
	AdditionalHeaders map[string]string

	// Flags are the standard API client flags provided by NewFlags. If nil,
	// default values will be used.
	Flags *Flags

	// Out is the writer that will be used when outputting diagnostics, such as
	// curl commands when -get-curl is enabled.
	Out io.Writer
}

ClientOpts encapsulates the options given to NewClient.

type Flags

type Flags struct {
	// contains filtered or unexported fields
}

Flags encapsulates the standard flags that should be added to all commands that issue API requests.

func NewFlags

func NewFlags(flagSet *flag.FlagSet) *Flags

NewFlags instantiates a new Flags structure and attaches flags to the given flag set.

func (*Flags) Trace

func (f *Flags) Trace() bool

func (*Flags) UserAgentTelemetry

func (f *Flags) UserAgentTelemetry() bool

type GraphQlError

type GraphQlError struct {
	// contains filtered or unexported fields
}

GraphQlError wraps a raw JSON error returned from a GraphQL endpoint.

func (*GraphQlError) Code

func (g *GraphQlError) Code() (string, error)

Code returns the GraphQL error code, if one was set on the error.

func (*GraphQlError) Error

func (g *GraphQlError) Error() string

func (*GraphQlError) Extensions

func (g *GraphQlError) Extensions() (map[string]interface{}, error)

Extensions returns the GraphQL error extensions, if set, or nil if no extensions were set on the error.

type GraphQlErrors

type GraphQlErrors []*GraphQlError

GraphQlErrors contains one or more GraphQlError instances.

func (GraphQlErrors) Error

func (gg GraphQlErrors) Error() string

type Request

type Request interface {
	// Do actions the request. Normally, this means that the request is
	// transmitted and the response is unmarshalled into result.
	//
	// If no data was available to be unmarshalled — for example, due to the
	// -get-curl flag being set — then ok will return false.
	Do(ctx context.Context, result interface{}) (ok bool, err error)

	// DoRaw has the same behaviour as Do, with one exception: the result will
	// not be unwrapped, and will include the GraphQL errors. Therefore the
	// structure that is provided as the result should have top level Data and
	// Errors keys for the GraphQL wrapper to be unmarshalled into.
	DoRaw(ctx context.Context, result interface{}) (ok bool, err error)
}

Request instances represent GraphQL requests.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL