sanity

package module
v1.0.0-alpha.5 Latest Latest
Warning

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

Go to latest
Published: Dec 18, 2020 License: MIT Imports: 12 Imported by: 3

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() *MutationBuilder

Mutate returns a new mutation builder.

func (*Client) Query

func (c *Client) Query(query string) *QueryBuilder

Query returns a new query builder.

func (*Client) WithOptions

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

WithOptions returns a new client instance with options modified.

type MutateResult

type MutateResult struct {
	TransactionID string
	Results       []*api.MutateResultItem
}

type MutationBuilder

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

func (*MutationBuilder) Create

func (mb *MutationBuilder) Create(doc interface{}) *MutationBuilder

func (*MutationBuilder) CreateIfNotExists

func (mb *MutationBuilder) CreateIfNotExists(doc interface{}) *MutationBuilder

func (*MutationBuilder) CreateOrReplace

func (mb *MutationBuilder) CreateOrReplace(doc interface{}) *MutationBuilder

func (*MutationBuilder) Delete

func (mb *MutationBuilder) Delete(id string) *MutationBuilder

func (*MutationBuilder) Do

func (*MutationBuilder) Patch

func (mb *MutationBuilder) Patch(id string) *PatchBuilder

func (*MutationBuilder) ReturnDocuments

func (mb *MutationBuilder) ReturnDocuments(enable bool) *MutationBuilder

func (*MutationBuilder) ReturnIDs

func (mb *MutationBuilder) ReturnIDs(enable bool) *MutationBuilder

func (*MutationBuilder) TransactionID

func (mb *MutationBuilder) TransactionID(id string) *MutationBuilder

func (*MutationBuilder) Visibility

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 WithBaseURL

func WithBaseURL(url url.URL) Option

WithBaseURL returns an option that changes the API URL.

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 PatchBuilder

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

func (*PatchBuilder) Dec

func (pb *PatchBuilder) Dec(path string, n float64) *PatchBuilder

func (*PatchBuilder) End

func (pb *PatchBuilder) End() *MutationBuilder

func (*PatchBuilder) IfRevisionID

func (pb *PatchBuilder) IfRevisionID(id string) *PatchBuilder

func (*PatchBuilder) Inc

func (pb *PatchBuilder) Inc(path string, n float64) *PatchBuilder

func (*PatchBuilder) InsertAfter

func (pb *PatchBuilder) InsertAfter(path string, items ...interface{}) *PatchBuilder

func (*PatchBuilder) InsertBefore

func (pb *PatchBuilder) InsertBefore(path string, items ...interface{}) *PatchBuilder

func (*PatchBuilder) InsertReplace

func (pb *PatchBuilder) InsertReplace(path string, items ...interface{}) *PatchBuilder

func (*PatchBuilder) Query

func (pb *PatchBuilder) Query(query string) *PatchBuilder

func (*PatchBuilder) Set

func (pb *PatchBuilder) Set(path string, val interface{}) *PatchBuilder

func (*PatchBuilder) SetIfMissing

func (pb *PatchBuilder) SetIfMissing(path string, val interface{}) *PatchBuilder

func (*PatchBuilder) Unset

func (pb *PatchBuilder) Unset(paths ...string) *PatchBuilder

type QueryBuilder

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

QueryBuilder is a builder for queries.

func (*QueryBuilder) Do

func (qb *QueryBuilder) Do(ctx context.Context) (*QueryResult, error)

Query performs the query. On API failure, this will return an error of type *RequestError.

func (*QueryBuilder) Param

func (qb *QueryBuilder) Param(name string, val interface{}) *QueryBuilder

Param adds a query parameter. For example, Param("foo", "bar") makes $foo usable inside the query. The passed-in value must be serializable to a JSON primitive.

type QueryResult

type QueryResult struct {
	// Time is the time taken.
	Time time.Duration

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

QueryResult holds the result of a query API call.

func (*QueryResult) Unmarshal

func (q *QueryResult) Unmarshal(dest interface{}) error

Unmarshal unmarshals the result into a Go value or struct. If there were no results, the destination value is set to the zero value.

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.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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