client

package
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Sep 16, 2026 License: GPL-3.0 Imports: 14 Imported by: 0

Documentation

Overview

Package client is the hand-written base client the generated SDKs, lib/emby and lib/jf, send every request through, after go-azure-sdk's sdk/client. The two servers speak the same HTTP API family and differ here only in how the token is sent (EmbyToken, JellyfinToken).

A generated method builds RequestOptions (method, path, the status codes the operation is documented to answer, its options object), makes a Request, marshals the body into it, executes it and unmarshals the Response into its model:

opts := client.RequestOptions{
	ContentType:         "application/json",
	ExpectedStatusCodes: []int{http.StatusOK},
	HttpMethod:          http.MethodGet,
	OptionsObject:       options,
	Path:                "/Items",
}
req, err := c.Client.NewRequest(ctx, opts)
resp, err := req.Execute(ctx)
err = resp.Unmarshal(&model)

A status the operation does not document is an error (*StatusError), even another 2xx: that is how a document that has drifted from its server shows up, and the fix belongs in the importer's workarounds. The response is returned alongside the error, its body still readable.

Index

Constants

View Source
const (
	// MaxResponseBytes caps a buffered (non-stream) response body.
	MaxResponseBytes = 64 << 20
	// DefaultPageSize is the page a Complete method asks for when the
	// options leave Limit unset.
	DefaultPageSize = 500
	// UserAgent is sent with every request, and names the client to the
	// server in the MediaBrowser authorization header.
	UserAgent = "embyfin-mcp"
)

Variables

This section is empty.

Functions

func CSV

func CSV[T ~string | ~int | ~int64](values []T) string

CSV joins a list parameter the way both servers read arrays in a query string: comma-separated.

func IsNotFound

func IsNotFound(err error) bool

IsNotFound reports whether err is a 404 from the server.

func JSONObject

func JSONObject[T any](value map[string]T) string

JSONObject renders an object query parameter as the JSON string the servers bind it from.

func StatusCode

func StatusCode(err error) int

StatusCode returns the status of a *StatusError in err's chain, or 0.

func WasNotFound

func WasNotFound(resp *http.Response) bool

WasNotFound reports whether a response is a 404.

Types

type Authorizer

type Authorizer interface {
	Authorize(req *http.Request)
}

Authorizer adds credentials to a request.

type Client

type Client struct {
	// BaseURL is the server address without a trailing slash.
	BaseURL    string
	HTTPClient *http.Client
	Authorizer Authorizer
}

Client sends requests to one server.

func New

func New(baseURL string, authorizer Authorizer) (*Client, error)

New returns a client for the server at baseURL (scheme and host, optionally a path prefix such as /emby).

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, input RequestOptions) (*Request, error)

NewRequest builds a request with authentication, the fixed client headers and the options object applied.

type EmbyToken

type EmbyToken string

EmbyToken authenticates to Emby with an API key or a user access token. Emby reads the token from X-Emby-Token and identifies the client from X-Emby-Authorization, which carries the token as well.

func (EmbyToken) Authorize

func (t EmbyToken) Authorize(req *http.Request)

type Headers

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

Headers are request headers from an options object.

func (*Headers) Append

func (h *Headers) Append(key, value string)

Append adds a header.

func (*Headers) Values

func (h *Headers) Values() http.Header

Values returns the headers.

type JellyfinToken

type JellyfinToken string

JellyfinToken authenticates to Jellyfin with an API key or a user access token. Jellyfin takes the token and the client identity from one MediaBrowser Authorization header (Jellyfin 12 answers 401 to a key sent only as X-Emby-Token).

func (JellyfinToken) Authorize

func (t JellyfinToken) Authorize(req *http.Request)

type Options

type Options interface {
	ToHeaders() *Headers
	ToQuery() *QueryParams
}

Options is what a generated options struct implements: the query and header parameters it holds.

type QueryParams

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

QueryParams are query parameters from an options object.

func (*QueryParams) Append

func (q *QueryParams) Append(key, value string)

Append adds a query parameter.

func (*QueryParams) Values

func (q *QueryParams) Values() url.Values

Values returns the parameters.

type Request

type Request struct {
	*http.Request

	ExpectedStatusCodes []int
	StreamResponse      bool
	// contains filtered or unexported fields
}

Request is a request being built.

func (*Request) Execute

func (r *Request) Execute(_ context.Context) (*Response, error)

Execute sends the request. The response is returned whenever the server answered, including with a *StatusError, so the caller can read its status and body. Its body is buffered (and readable again) unless the request streams a successful response.

func (*Request) Marshal

func (r *Request) Marshal(payload any) error

Marshal sets the request body to payload as JSON.

func (*Request) SetBody

func (r *Request) SetBody(body io.Reader, contentType string) error

SetBody sets the request body to raw bytes of the given media type, or the operation's own when contentType is empty. A nil body sends none. An operation that takes a range of types (image/*) needs the concrete one.

type RequestOptions

type RequestOptions struct {
	// ContentType is the media type of the request body, when there is one.
	ContentType string
	// ExpectedStatusCodes are the statuses the operation documents; any
	// other is a *StatusError.
	ExpectedStatusCodes []int
	HttpMethod          string //nolint:revive,staticcheck // go-azure-sdk's spelling, which the generated code mirrors
	// OptionsObject supplies query parameters and headers; nil for none.
	OptionsObject Options
	// Path is the escaped path below BaseURL.
	Path string
	// StreamResponse leaves a successful response's body unread for the
	// caller, for operations that answer a file.
	StreamResponse bool
}

RequestOptions describes one request.

type Response

type Response struct {
	*http.Response
}

Response is a server's answer.

func (*Response) Unmarshal

func (r *Response) Unmarshal(model any) error

Unmarshal decodes the JSON body into model, leaving the body readable again for a caller that wants the bytes. An empty body leaves model as it is.

type StatusError

type StatusError struct {
	Method              string
	Path                string
	StatusCode          int
	ExpectedStatusCodes []int
	// Body is the start of the response body.
	Body string
}

StatusError is an answer with a status the operation does not document.

func (*StatusError) Error

func (e *StatusError) Error() string

Jump to

Keyboard shortcuts

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