sanity

package module
v0.0.0-...-74479a0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2020 License: MIT Imports: 11 Imported by: 0

README

Sanity client in Go

Under development! For developers with an adventurous spirit only.

This is a client for Sanity written in Go.

Using

See the API reference for the full documentation.

package main

import (
  "log"
  "context"

  sanity "github.com/sanity-io/client-go"
)

func main() {
  client, err := sanity.New("zx3vzmn!",
    sanity.WithCallbacks(sanity.Callbacks{
      OnQueryResult: func(result *sanity.QueryResult) {
        log.Printf("Sanity queried in %d ms!", result.Ms)
      },
    }),
    sanity.WithToken("mytoken"),
    sanity.WithDataset("production"))
  if err != nil {
    log.Fatal(err)
  }

  var project struct {
    ID    string `json:"_id"`
    Title string
  }
  if err = client.Query(context.Background(), "*[_type == 'project' && _id == $id][0]", &project,
    sanity.Param("id", "123")); err != nil {
    log.Fatal(err)
  }

  log.Printf("Project: %+v", project)
}

Installation

go get github.com/sanity-io/client-go

Requirements

Go 1.13 or later.

License

See LICENSE file.

Documentation

Index

Constants

View Source
const DefaultDataset = "production"

Variables

This section is empty.

Functions

This section is empty.

Types

type Callbacks

type Callbacks struct {
	OnErrorWillRetry func(error)
	OnQueryResult    func(*QueryResult)
}

type Client

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

Client implements a client for interacting with the Sanity API.

func New

func New(projectID string, opts ...Option) (*Client, error)

New returns a new client. A project ID must be provided. Zero or more options can be passed. For example:

client := sanity.New("foobar123",
  sanity.WithCDN(true), sanity.WithToken("mytoken"))

func (*Client) Mutate

func (c *Client) Mutate(ctx context.Context, payload []byte, out interface{}) error

Mutate performs a post. will receive the result. json.Unmarshal is used to deserialize the JSON; if the type supports json.Unmarshaller, then it can override the unmarshalling.

On API failure, this will return an error of type *RequestError.

func (*Client) Query

func (c *Client) Query(ctx context.Context, query string, out interface{}, params ...Parameter) error

Query performs a query. The out parameter must point to a value or struct that will receive the result. json.Unmarshal is used to deserialize the JSON; if the type supports json.Unmarshaller, then it can override the unmarshalling.

Zero or more parameters can be passed. For simplicity, use the Param() function to construct each parameter.

On API failure, this will return an error of type *RequestError.

func (*Client) WithOptions

func (c *Client) WithOptions(opts ...Option) (*Client, error)

WithOptions returns a new client instance with options modified.

type MutateQueryResult

type MutateQueryResult struct {
	TransactionID string           `json:"transactionId"`
	Results       *json.RawMessage `json:"results"`
}

MutateQueryResult holds the result of a query API call.

type Option

type Option func(c *Client) error

func WithBackoff

func WithBackoff(b backoff.Backoff) Option

WithBackoff returns an option that configures network request backoff. For how backoff works, see the underlying backoff package: https://github.com/jpillora/backoff. By default, the client uses the backoff package's default (maximum 10 seconds wait, backoff factor of 2).

func WithCDN

func WithCDN(b bool) Option

WithCDN returns an option that enables or disables the use of the Sanity API CDN.

func WithCallbacks

func WithCallbacks(cbs Callbacks) Option

WithCallbacks returns an option that enables callbacks for common events such as errors.

func WithDataset

func WithDataset(id string) Option

WithDataset returns an option that sets the dataset name.

func WithHTTPClient

func WithHTTPClient(client *http.Client) Option

WithHTTPClient returns an option for setting a custom HTTP client.

func WithToken

func WithToken(t string) Option

WithToken returns an option that sets the API token to use.

type Parameter

type Parameter struct {
	// Name is the name of the parameter, which can be used in the query as $name.
	Name string

	// Value is the value of the parameter. The value must be JSON-serializable.
	Value interface{}
}

Parameter is a query parameter.

func Param

func Param(name string, value interface{}) Parameter

Param creates a named parameter.

type QueryResult

type QueryResult struct {
	// Ms is the time taken, in milliseconds.
	Ms float64 `json:"ms"`

	// Query is the GROQ query.
	Query string `json:"query"`

	// Result is the raw JSON of the query result.
	Result *json.RawMessage `json:"result"`
}

QueryResult holds the result of a query API call.

type RequestError

type RequestError struct {
	// Request is the attempted HTTP request that failed.
	Request *http.Request

	// Response is the HTTP response. Note that the body will no longer be valid.
	Response *http.Response

	// Body is the body of the response.
	Body []byte
}

RequestError is returned for API requests that fail with a non-successful HTTP status code.

func (*RequestError) Error

func (e *RequestError) Error() string

Error implements the error interface.

Jump to

Keyboard shortcuts

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