apiclient

package
v0.13.0 Latest Latest
Warning

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

Go to latest
Published: Mar 25, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultTimeout = 30 * time.Second

DefaultTimeout is the HTTP client timeout applied when no custom HTTPDoer is provided.

Variables

This section is empty.

Functions

func DecodeJSON added in v0.13.0

func DecodeJSON(resp *http.Response, dest interface{}, contextArgs ...interface{}) error

DecodeJSON reads and decodes a JSON response body into dest, then closes the body. The context args are passed to errors.WrapWithDetails on decode failure.

func ValidateURL

func ValidateURL(rawURL string) error

ValidateURL checks that rawURL is a valid HTTP(S) URL. This guards against SSRF by rejecting non-HTTP schemes.

Types

type AuthFunc

type AuthFunc func(req *http.Request)

AuthFunc applies authentication to an outgoing HTTP request.

func BasicAuth

func BasicAuth(user, password string) AuthFunc

BasicAuth returns an AuthFunc that sets HTTP Basic Authentication.

func BearerToken

func BearerToken(token string) AuthFunc

BearerToken returns an AuthFunc that sets a Bearer token Authorization header.

func HeaderAuth

func HeaderAuth(name, value string) AuthFunc

HeaderAuth returns an AuthFunc that sets a custom header for authentication.

func NoAuth

func NoAuth() AuthFunc

NoAuth returns an AuthFunc that does not set any authentication.

type Client

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

Client is a shared HTTP API client that handles URL construction, authentication, headers, and error handling.

func New

func New(baseURL string, opts ...Option) *Client

New creates a new API client with the given base URL and options.

func (*Client) Do

func (c *Client) Do(ctx context.Context, method, path, rawQuery string, body io.Reader) (*http.Response, error)

Do executes an HTTP request with the client's configuration.

func (*Client) DoGraphQL

func (c *Client) DoGraphQL(ctx context.Context, graphqlPath, query string, variables map[string]any) (json.RawMessage, error)

DoGraphQL marshals a GraphQL request, posts it, and returns the data field.

func (*Client) DoWithContentType

func (c *Client) DoWithContentType(ctx context.Context, method, path, rawQuery string, body io.Reader, contentType string) (*http.Response, error)

DoWithContentType executes an HTTP request with an explicit Content-Type, overriding the client's default for this single request.

func (*Client) SetHTTPDoer

func (c *Client) SetHTTPDoer(doer HTTPDoer)

SetHTTPDoer replaces the HTTP client used for API requests.

type ErrorFormatter

type ErrorFormatter func(providerName, method, path string, statusCode int, body []byte) error

ErrorFormatter formats an HTTP error response into an error value.

type GraphQLError

type GraphQLError struct {
	Message string `json:"message"`
}

GraphQLError represents a single GraphQL error.

type GraphQLRequest

type GraphQLRequest struct {
	Query     string         `json:"query"`
	Variables map[string]any `json:"variables,omitempty"`
}

GraphQLRequest is the standard GraphQL request body.

type GraphQLResponse

type GraphQLResponse struct {
	Data   json.RawMessage `json:"data"`
	Errors []GraphQLError  `json:"errors,omitempty"`
}

GraphQLResponse is the standard GraphQL response envelope.

type HTTPDoer

type HTTPDoer interface {
	Do(req *http.Request) (*http.Response, error)
}

HTTPDoer abstracts HTTP request execution for testability and to decouple from the concrete *http.Client type.

type Option

type Option func(*Client)

Option configures a Client.

func WithAuth

func WithAuth(auth AuthFunc) Option

WithAuth sets the authentication strategy.

func WithContentType

func WithContentType(ct string) Option

WithContentType sets a Content-Type header on every request, regardless of whether a body is present. When empty (the default), Content-Type is set to "application/json" only when a body is provided.

func WithErrorFormatter

func WithErrorFormatter(ef ErrorFormatter) Option

WithErrorFormatter sets a custom error formatter.

func WithHTTPDoer

func WithHTTPDoer(doer HTTPDoer) Option

WithHTTPDoer sets the HTTP client used for requests.

func WithHeader

func WithHeader(name, value string) Option

WithHeader adds a header to every request.

func WithProviderName

func WithProviderName(name string) Option

WithProviderName sets the provider name used in error messages.

func WithTimeout

func WithTimeout(d time.Duration) Option

WithTimeout sets the HTTP client timeout. Only effective when no custom HTTPDoer is provided via WithHTTPDoer.

func WithURLBuilder

func WithURLBuilder(ub URLBuilder) Option

WithURLBuilder sets the URL construction strategy.

type URLBuilder

type URLBuilder func(base *url.URL, path, rawQuery string) (string, error)

URLBuilder constructs a full URL from a parsed base URL, path, and raw query string.

func ParsePathURL

func ParsePathURL() URLBuilder

ParsePathURL returns a URLBuilder that parses the path as a URL to extract embedded query parameters. Used by Notion, Figma, and Telegram where the path may include query strings (e.g. "/v1/files/key?depth=1").

func RawPathURL

func RawPathURL() URLBuilder

RawPathURL returns a URLBuilder that also sets u.RawPath to preserve percent-encoding in paths. Used by GitLab where project paths contain encoded slashes (e.g. "mygroup%2Fmyproject").

func StandardURL

func StandardURL() URLBuilder

StandardURL returns a URLBuilder that sets u.Path and u.RawQuery directly. Used by Jira, GitHub, Azure DevOps, Shortcut, and Amplitude.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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