dataverse

package module
v0.5.0 Latest Latest
Warning

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

Go to latest
Published: Jun 11, 2025 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

View Source
const (
	ErrorTypeConnection = "connection"
	ErrorTypeAPI        = "api"
	ErrorTypeAuth       = "auth"
)
View Source
const (
	OrderByAsc  = "asc"
	OrderByDesc = "desc"
)

Variables

View Source
var (
	Version = "1.00"

	ErrMissingConfig = errors.New("missing config")
)
View Source
var ErrAuthorizationError = errors.New("unauthorized")
View Source
var ErrInvalidAPIError = errors.New("invalid api error format")

Functions

func DecodeResponse added in v0.4.0

func DecodeResponse[T Validator](resp *http.Response) (T, error)

DecodeResponse decodes the body into [T] and provides the error handling.

func DecodeResponseList added in v0.4.0

func DecodeResponseList[T Validator](resp *http.Response) ([]T, error)

Types

type APIError

type APIError struct {
	Path       string
	StatusCode int
	Code       string
	Message    string
}

func (APIError) Error

func (err APIError) Error() string

type AuthClient

type AuthClient interface {
	GetToken(context.Context) (string, error)
}

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

func (c *Client) Do(req *http.Request) (*http.Response, error)

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 Config

type Config struct {
	BaseURL      string
	TenantID     string
	ClientID     string
	ClientSecret string
}

Config holds the necessary configuration strings.

func (Config) Validate

func (c Config) Validate() error

Validate returns an error that lists the missing fields.

type QueryOptions

type QueryOptions struct {
	Select  []string
	Expand  []string
	OrderBy string
	Filter  []string
	Top     int
}

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 NewTable

func NewTable[T Validator](client *Client, resource string) *Table[T]

NewTable returns a table that wraps a Client with type-specific methods.

func (*Table[T]) Client

func (t *Table[T]) Client() *Client

Client returns the wrapped Client.

func (*Table[T]) Delete

func (t *Table[T]) Delete(ctx context.Context, id string) error

Delete deletes the given row.

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]) GetByID

func (t *Table[T]) GetByID(ctx context.Context, id string, opts QueryOptions) (T, error)

GetByID returns a table row by its ID.

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

func (t *Table[T]) New(ctx context.Context, newParams any, opts QueryOptions) (T, error)

New creates a new row, it then calls GetByID and returns the row with any applied options.

func (*Table[T]) SetDefaultExpand

func (t *Table[T]) SetDefaultExpand(fields []string)

SetDefaultExpand sets the expand field for all operations.

func (*Table[T]) Update

func (t *Table[T]) Update(ctx context.Context, id string, updateParams any, opts QueryOptions) (T, error)

type Validator

type Validator interface {
	Validate() error
}

Validator validates a struct that represents a table.

Directories

Path Synopsis
internal
e2e

Jump to

Keyboard shortcuts

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