client

package
v0.0.0-...-0b08f29 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MIT Imports: 16 Imported by: 0

Documentation

Overview

Package client provides easy and fast in-process access to a REST api

Instead of marshalling HTTP, the client talks directly to the mux router. The client is the tool of choice if one request handler needs to call other handlers to fulfill its task. It is also perfectly suited for unit tests.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client provides easy access to the REST API.

func NewWithRouter

func NewWithRouter(router *mux.Router) Client

NewWithRouter creates a client to make pseudo-REST requests to the backend, through the mux router

WithAuthorization() adds an authorization to the request context. WithContext() specifies a different base context all together.

func NewWithURL

func NewWithURL(url string) Client

NewWithURL creates a client to make REST requests to the backend

WithToken adds an authorization token to the request header.

func (Client) Collection

func (c Client) Collection(resource string) Collection

Collection returns a new collection client

func (Client) PostMultipart

func (c Client) PostMultipart(url string, data []byte) (status int, err error)

PostMultipart upload data using a Multipart Form

func (Client) RawDelete

func (c Client) RawDelete(path string) (int, error)

RawDelete deletes the resource at path. Expects http.StatusNoContent as response, otherwise it will flag an error.

The path can be extend with query strings.

Returns the actual http status code.

func (Client) RawGet

func (c Client) RawGet(path string, result interface{}) (int, error)

RawGet gets the resource from path. Expects http.StatusOK as response, otherwise it will flag an error. Returns the actual http status code.

The path can be extend with query strings.

result can be map[string]interface{} or a raw *[]byte. result can be nil.

func (*Client) RawGetBlobWithHeader

func (c *Client) RawGetBlobWithHeader(path string, header map[string]string, blob *[]byte) (int, http.Header, error)

RawGetBlobWithHeader gets a binary resource from path. Expects http.StatusOK as response, otherwise it will flag an error.

The path can be extend with query strings.

Returns the actual http status code and the return header

func (Client) RawGetWithHeader

func (c Client) RawGetWithHeader(path string, header map[string]string, result interface{}) (int, http.Header, error)

RawGetWithHeader gets the resource from path. Expects http.StatusOK as response, otherwise it will flag an error. Returns the actual http status code and the header.

The path can be extend with query strings.

result can be map[string]interface{} or a raw *[]byte. result can be nil.

func (Client) RawPatch

func (c Client) RawPatch(path string, body interface{}, result interface{}) (int, error)

RawPatch puts a patch to path. Expects http.StatusOK, http.StatusCreated, or http.StatusNoContent as valid responses, otherwise it will flag an error. Returns the actual http status code.

The path can be extend with query strings.

body can also be a []byte, result can also be raw *[]byte. result can be nil.

func (Client) RawPost

func (c Client) RawPost(path string, body interface{}, result interface{}) (int, error)

RawPost posts a resource to path. Expects http.StatusCreated as response, otherwise it will flag an error. Returns the actual http status code.

The path can be extend with query strings.

body can also be a []byte, result can also be raw *[]byte. result can be nil.

func (Client) RawPostBlob

func (c Client) RawPostBlob(path string, header map[string]string, blob []byte, result interface{}) (int, error)

RawPostBlob posts a resource to path. Expects http.StatusCreated as response, otherwise it will flag an error. Returns the actual http status code.

The path can be extend with query strings.

func (Client) RawPut

func (c Client) RawPut(path string, body interface{}, result interface{}) (int, error)

RawPut puts a resource to path. Expects http.StatusOK, http.StatusCreated or http.StatusNoContent as valid responses, otherwise it will flag an error. Returns the actual http status code.

In case of http.StatusConflict, the conflicting version of the object has been returned as result.

The path can be extend with query strings.

body can also be a []byte, result can also be raw *[]byte. result can be nil.

func (Client) RawPutBlob

func (c Client) RawPutBlob(path string, header map[string]string, blob []byte, result interface{}) (int, error)

RawPutBlob puts a binary resource to path. Expects http.StatusOK, http.StatusCreated or http.StatusNoContent as valid responses, otherwise it will flag an error.

The path can be extend with query strings.

Returns the actual http status code. result can be nil.

func (Client) Relation

func (c Client) Relation(resource string) Relation

Relation returns a new relation client

func (Client) WithAdminAuthorization

func (c Client) WithAdminAuthorization() Client

WithAdminAuthorization returns a new client with admin authorizations (this works only directly against the mux router, for a normal client

use WithToken()))

func (Client) WithAuthorization

func (c Client) WithAuthorization(auth *access.Authorization) Client

WithAuthorization returns a new client with specific authorizations (this works only directly against the mux router, for a normal client

use WithToken())

func (Client) WithContext

func (c Client) WithContext(ctx context.Context) Client

WithContext returns a new client with specific request context

func (Client) WithRole

func (c Client) WithRole(role string) Client

WithRole returns a new client with role authorization (this works only directly against the mux router, for a normal client

use WithToken()))

func (Client) WithToken

func (c Client) WithToken(token string) Client

WithToken returns a new client with admin authorizations

type Collection

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

Collection represents a collection of particular resource

func (Collection) Clear

func (r Collection) Clear() (int, error)

Clear deletes the entire collection

This operation does not accept any filters nor does it generate notifications. If you need filters or delete notifications, you should iterate of the items and delete them one by one.

The operation corresponds to a DELETE request.

Expects http.StatusNoContent as response, otherwise it will flag an error.

func (Collection) CollectionPath

func (r Collection) CollectionPath() string

CollectionPath returns the created path for the collection plus optional query strings

func (Collection) Create

func (r Collection) Create(body interface{}, result interface{}) (int, error)

Create always creates a new item.

The operation corresponds to a POST request.

Expects http.StatusCreated as response, otherwise it will flag an error. Returns the actual http status code.

body can also be a []byte, result can also be raw *[]byte. result can be nil.

func (Collection) CreateBlob

func (r Collection) CreateBlob(blob []byte, meta interface{}, result interface{}) (int, error)

CreateBlob always creates a new blob item.

The operation corresponds to a POST request.

Expects http.StatusCreated as response, otherwise it will flag an error. Returns the actual http status code. result can be nil.

func (Collection) FirstPage

func (r Collection) FirstPage() Page

FirstPage returns a requester for the first page of a collection

Do not specify the page filter when using the page requester, as it manages page itself. You can set all others parameters, including limit.

func (Collection) Item

func (r Collection) Item(id uuid.UUID) Item

Item gets an item from a collection

func (Collection) List

func (r Collection) List(result interface{}) (int, error)

List gets the entire collection up until the specified limit.

If you potentially need multiple pages, use FirstPage() instead.

The operation corresponds to a GET request.

Expects http.StatusOK as response, otherwise it will flag an error. Returns the actual http status code.

result can be map[string]interface{} or a raw *[]byte.

func (Collection) Singleton

func (r Collection) Singleton() Item

Singleton gets a singleton from this collection

func (Collection) SingletonPath

func (r Collection) SingletonPath() string

SingletonPath returns the created path for a singleton

func (Collection) Upsert

func (r Collection) Upsert(body interface{}, result interface{}) (int, error)

Upsert updates an item, or creates it if it doesn't exist yet. The item must be fully qualified, i.e. it must contain all identifiers, either in the body itself or as selectors.

The operation corresponds to a PUT request.

Expects http.StatusOK, http.StatusCreated or http.StatusNoContent as valid responses, otherwise it will flag an error. Returns the actual http status code.

In case of http.StatusConflict, the conflicting version of the object has been returned as result.

body can also be a []byte, result can also be raw *[]byte. result can be nil.

func (Collection) UpsertBlob

func (r Collection) UpsertBlob(blob []byte, meta interface{}, result interface{}) (int, error)

UpsertBlob updates a blob item, or creates it if it doesn't exist yet. The blob item must be fully qualified, i.e. it must contain all identifiers, either in the meta body itself or as selectors.

The operation corresponds to a PUT request.

Expects http.StatusOK, http.StatusCreated or http.StatusNoContent as valid responses, otherwise it will flag an error. Returns the actual http status code. result can be nil.

func (Collection) WithFilter

func (r Collection) WithFilter(key string, value string) Collection

WithFilter returns a new collection client with a URL filter parameter added. This is a shortcut for WithParameter("filter", key+"="+value)

func (Collection) WithParameter

func (r Collection) WithParameter(key string, value string) Collection

WithParameter returns a new collection client with a URL parameter added.

func (Collection) WithParameters

func (r Collection) WithParameters(keyValues map[string]string) Collection

WithParameters returns a new collection client with all URL parameters added.

func (Collection) WithParent

func (r Collection) WithParent(parentID uuid.UUID) Collection

WithParent returns a new collection client with a parent selector added

func (Collection) WithSelector

func (r Collection) WithSelector(key string, value uuid.UUID) Collection

WithSelector returns a new collection client with a selector added

func (Collection) WithSelectors

func (r Collection) WithSelectors(keyValues map[string]string) Collection

WithSelectors returns a new collection client with all selectors added

type Item

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

Item represents a single item in a collection

func (Item) Delete

func (r Item) Delete() (int, error)

Delete deletes an item from a collection

The operation corresponds to a DELETE request.

Expects http.StatusNoContent as response, otherwise it will flag an error.

Returns the actual http status code.

func (Item) Patch

func (r Item) Patch(body interface{}, result interface{}) (int, error)

Patch updates selected fields of an item

Expects http.StatusOK, http.StatusCreated or http.StatusNoContent as valid responses, otherwise it will flag an error. Returns the actual http status code.

body can also be a []byte, result can also be raw *[]byte. result can be nil.

func (Item) Path

func (r Item) Path() string

Path returns the created path for this item

func (Item) Read

func (r Item) Read(result interface{}, children ...string) (int, error)

Read reads an item from a collection

The operation corresponds to a GET request.

Expects http.StatusOK as response, otherwise it will flag an error. Returns the actual http status code.

Optional singleton children are added to the response.

result can also be map[string]interface{} or a raw *[]byte.

func (Item) Relate

func (r Item) Relate(resource string, id uuid.UUID) (int, error)

Relate creates a realation to another resource, provided that the relation actually exists

The operation corresponds to a PUT request.

Expects http.StatusOK, http.StatusCreated or http.StatusNoContent as valid responses, otherwise it will flag an error. Returns the actual http status code.

func (Item) Subcollection

func (r Item) Subcollection(resource string) Collection

Subcollection returns a subcollection for this item

func (Item) UpdateProperty

func (r Item) UpdateProperty(jsonName string, value string) (int, error)

UpdateProperty updates a single static property in the fastest possible way. Note: this method does trigger an update resource notificatino, but not with the entire object, only with the updated property.

The operation corresponds to a PUT request.

Expects http.StatusOK, http.StatusCreated or http.StatusNoContent as valid responses, otherwise it will flag an error. Returns the actual http status code.

func (Item) Upsert

func (r Item) Upsert(body interface{}, result interface{}) (int, error)

Upsert updates an item, or creates if it doesn't exist yet. The item must be fully qualified, i.e. it must contain all identifiers, either in the body itself or as selectors.

The operation corresponds to a PUT request.

Expects http.StatusOK, http.StatusCreated or http.StatusNoContent as valid responses, otherwise it will flag an error. Returns the actual http status code.

In case of http.StatusConflict, the conflicting version of the object has been returned as result.

body can also be a []byte, result can also be raw *[]byte. result can be nil.

func (Item) WithParameter

func (r Item) WithParameter(key string, value string) Item

WithParameter returns a new item client with a URL parameter added.

func (Item) WithParameters

func (r Item) WithParameters(keyValues map[string]string) Item

WithParameters returns a new item client with all URL parameters added.

type Page

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

Page is a requester for one page in a collection

func (*Page) Get

func (p *Page) Get(result interface{}) (int, error)

Get gets one page of the collection

func (Page) HasData

func (p Page) HasData() bool

HasData returns true if the page has data (by definition true for the first page)

func (Page) Next

func (p Page) Next() Page

Next returns the next page

func (Page) TotalCount

func (p Page) TotalCount() int

TotalCount returns the total number of elements (only available after you have called Get on the page)

type Relation

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

Relation represents a relation of particular resources

func (Relation) Collection

func (r Relation) Collection(resource string) Collection

Collection returns a new collection client, for a collection within this relation

Jump to

Keyboard shortcuts

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