Documentation
¶
Index ¶
- Constants
- Variables
- func DecodeResponse[T Validator](resp *http.Response) (T, error)
- func DecodeResponseList[T Validator](resp *http.Response) ([]T, error)
- type APIError
- type AuthClient
- type Client
- func (c *Client) Do(req *http.Request) (*http.Response, error)
- func (c *Client) MakeRequest(ctx context.Context, method string, path string, options QueryOptions, ...) (*http.Response, error)
- func (c *Client) NewRequest(ctx context.Context, method string, path string, opts QueryOptions, data any) (*http.Request, error)
- type ClientOption
- type Config
- type QueryOptions
- type Table
- func (t *Table[T]) Client() *Client
- func (t *Table[T]) Delete(ctx context.Context, id string) error
- func (t *Table[T]) GetByAltKey(ctx context.Context, key, value string, opts QueryOptions) (T, error)
- func (t *Table[T]) GetByID(ctx context.Context, id string, opts QueryOptions) (T, error)
- func (t *Table[T]) List(ctx context.Context, opts QueryOptions) ([]T, error)
- func (t *Table[T]) New(ctx context.Context, newParams any, opts QueryOptions) (T, error)
- func (t *Table[T]) SetDefaultExpand(fields []string)
- func (t *Table[T]) Update(ctx context.Context, id string, updateParams any, opts QueryOptions) (T, error)
- type Validator
Constants ¶
const ( ErrorTypeConnection = "connection" ErrorTypeAPI = "api" ErrorTypeAuth = "auth" )
const ( OrderByAsc = "asc" OrderByDesc = "desc" )
Variables ¶
var ( Version = "1.00" ErrMissingConfig = errors.New("missing config") )
var ErrAuthorizationError = errors.New("unauthorized")
var ErrInvalidAPIError = errors.New("invalid api error format")
Functions ¶
func DecodeResponse ¶ added in v0.4.0
DecodeResponse decodes the body into [T] and provides the error handling.
Types ¶
type AuthClient ¶
AuthClient represents an auth client that gets a token.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client interacts with the Dataverse API. It handles authentication, decoding JSON, and errors.
func NewClient ¶
func NewClient(config Config, options ...ClientOption) (*Client, error)
NewClient returns an instance of Client. It creates an [MSALClient] internally that implements AuthClient. It takes a variadic set of ClientOption. Current provided options are WithHTTPClient, WithUserAgent, WithAuthClient.
func (*Client) Do ¶
Do makes the request using the internal http.Client and checks the error for non-200 status codes and handles it appropriately. Returns an APIError if a standard response is returned from the Dataverse server.
func (*Client) MakeRequest ¶
func (c *Client) MakeRequest(ctx context.Context, method string, path string, options QueryOptions, data any) (*http.Response, error)
MakeRequest is a convenience method that calls Client.NewRequest then Client.Do.
func (*Client) NewRequest ¶
func (c *Client) NewRequest(ctx context.Context, method string, path string, opts QueryOptions, data any) (*http.Request, error)
NewRequest creates an http.Request with the provided options. It adds the path and query params to the base URL, and marshals the data.
type ClientOption ¶
type ClientOption func(*Client)
ClientOption is an optional value that modifies the client.
func WithAuthClient ¶
func WithAuthClient(client AuthClient) ClientOption
WithAuthClient sets the auth client that implements AuthClient.
func WithHTTPClient ¶
func WithHTTPClient(client *http.Client) ClientOption
WithHTTPClient sets the underlying *http.Client.
func WithUserAgent ¶
func WithUserAgent(agent string) ClientOption
WithUserAgent sets the user agent.
type QueryOptions ¶
QueryOptions are optional fields that apply to all table operations.
func (*QueryOptions) AddExpand ¶
func (qo *QueryOptions) AddExpand(fields ...string)
AddExpand is a convenience method to set multiple fields to expand. This is in addition to the default expands.
func (*QueryOptions) AddFilter ¶
func (qo *QueryOptions) AddFilter(filters ...string)
AddFilter adds a filter joined with " and ".
func (*QueryOptions) AddSelect ¶
func (qo *QueryOptions) AddSelect(fields ...string)
AddSelect is a convenience method add fields to the select param.
func (*QueryOptions) SetOrderBy ¶
func (qo *QueryOptions) SetOrderBy(field, direction string)
SetOrderBy is a convenience method to set the OrderBy field.
func (*QueryOptions) ToParams ¶
func (qo *QueryOptions) ToParams() url.Values
type Table ¶
type Table[T Validator] struct { // contains filtered or unexported fields }
Table represnts a dataverse table. It wraps a Client and adds CRUD methods that decode and validate the JSON.
func (*Table[T]) GetByAltKey ¶
func (t *Table[T]) GetByAltKey(ctx context.Context, key, value string, opts QueryOptions) (T, error)
GetByAltKey returns a table row by an alternate unique key.
func (*Table[T]) List ¶
func (t *Table[T]) List(ctx context.Context, opts QueryOptions) ([]T, error)
Lists returns a slice of table rows.
func (*Table[T]) New ¶
New creates a new row, it then calls GetByID and returns the row with any applied options.
func (*Table[T]) SetDefaultExpand ¶
SetDefaultExpand sets the expand field for all operations.