client

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 10, 2026 License: GPL-3.0 Imports: 12 Imported by: 0

Documentation

Overview

Package client is the smallest base client the code pandorest generates builds and passes its tests on: what a generated package needs of its base client, in code. The generator's tests build a generated package against it, so a change to what generated code asks of a base client shows up here first.

It is not the base client of any SDK. Each repository keeps its own beside its generated packages, after go-azure-sdk's sdk/client, because that is where a service's own behaviour belongs: how it takes a credential, what it retries, how it words a failure. This one authenticates with a bearer token and does nothing more than it must.

Index

Constants

View Source
const DefaultPageSize = 500

DefaultPageSize is the page a Complete method asks for when the options leave the page size unset.

Variables

This section is empty.

Functions

func CSV

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

CSV joins a list parameter into one comma-separated value.

func DeepObject

func DeepObject[K ~string](name string, value map[K]string) [][2]string

DeepObject renders an object query parameter the deepObject way, one parameter per key as name[key]=value, in key order.

func JSONObject

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

JSONObject renders an object query parameter as one JSON string.

func StatusCode

func StatusCode(err error) int

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

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).

func (*Client) NewRequest

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

NewRequest builds a request with authentication and the options object applied.

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 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(ctx 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
	// 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. An empty body leaves model as it is.

type StatusError

type StatusError struct {
	Method     string
	Path       string
	StatusCode int
	Body       string
}

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

func (*StatusError) Error

func (e *StatusError) Error() string

type Token

type Token string

Token authenticates with a bearer token.

func (Token) Authorize

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

Authorize sends the token in the Authorization header.

Jump to

Keyboard shortcuts

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