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 ¶
- type Client
- func (c Client) Collection(resource string) Collection
- func (c Client) PostMultipart(url string, data []byte) (status int, err error)
- func (c Client) RawDelete(path string) (int, error)
- func (c Client) RawGet(path string, result interface{}) (int, error)
- func (c *Client) RawGetBlobWithHeader(path string, header map[string]string, blob *[]byte) (int, http.Header, error)
- func (c Client) RawGetWithHeader(path string, header map[string]string, result interface{}) (int, http.Header, error)
- func (c Client) RawPatch(path string, body interface{}, result interface{}) (int, error)
- func (c Client) RawPost(path string, body interface{}, result interface{}) (int, error)
- func (c Client) RawPostBlob(path string, header map[string]string, blob []byte, result interface{}) (int, error)
- func (c Client) RawPut(path string, body interface{}, result interface{}) (int, error)
- func (c Client) RawPutBlob(path string, header map[string]string, blob []byte, result interface{}) (int, error)
- func (c Client) Relation(resource string) Relation
- func (c Client) WithAdminAuthorization() Client
- func (c Client) WithAuthorization(auth *access.Authorization) Client
- func (c Client) WithContext(ctx context.Context) Client
- func (c Client) WithRole(role string) Client
- func (c Client) WithToken(token string) Client
- type Collection
- func (r Collection) Clear() (int, error)
- func (r Collection) CollectionPath() string
- func (r Collection) Create(body interface{}, result interface{}) (int, error)
- func (r Collection) CreateBlob(blob []byte, meta interface{}, result interface{}) (int, error)
- func (r Collection) FirstPage() Page
- func (r Collection) Item(id uuid.UUID) Item
- func (r Collection) List(result interface{}) (int, error)
- func (r Collection) Singleton() Item
- func (r Collection) SingletonPath() string
- func (r Collection) Upsert(body interface{}, result interface{}) (int, error)
- func (r Collection) UpsertBlob(blob []byte, meta interface{}, result interface{}) (int, error)
- func (r Collection) WithFilter(key string, value string) Collection
- func (r Collection) WithParameter(key string, value string) Collection
- func (r Collection) WithParameters(keyValues map[string]string) Collection
- func (r Collection) WithParent(parentID uuid.UUID) Collection
- func (r Collection) WithSelector(key string, value uuid.UUID) Collection
- func (r Collection) WithSelectors(keyValues map[string]string) Collection
- type Item
- func (r Item) Delete() (int, error)
- func (r Item) Patch(body interface{}, result interface{}) (int, error)
- func (r Item) Path() string
- func (r Item) Read(result interface{}, children ...string) (int, error)
- func (r Item) Relate(resource string, id uuid.UUID) (int, error)
- func (r Item) Subcollection(resource string) Collection
- func (r Item) UpdateProperty(jsonName string, value string) (int, error)
- func (r Item) Upsert(body interface{}, result interface{}) (int, error)
- func (r Item) WithParameter(key string, value string) Item
- func (r Item) WithParameters(keyValues map[string]string) Item
- type Page
- type Relation
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 ¶
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 ¶
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 ¶
PostMultipart upload data using a Multipart Form
func (Client) RawDelete ¶
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 ¶
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 ¶
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 ¶
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 ¶
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) WithAdminAuthorization ¶
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 ¶
WithContext returns a new client with specific request context
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 ¶
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 ¶
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) Read ¶
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 ¶
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 ¶
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 ¶
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 ¶
WithParameter returns a new item client with a URL parameter added.
type Page ¶
type Page struct {
// contains filtered or unexported fields
}
Page is a requester for one page in a collection
func (Page) HasData ¶
HasData returns true if the page has data (by definition true for the first page)
func (Page) TotalCount ¶
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