client

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: Oct 2, 2026 License: MIT Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrAuthentication = errors.New("jamfprotect: authentication failed")
	ErrGraphQL        = errors.New("jamfprotect: graphql error")
	ErrNotFound       = errors.New("jamfprotect: resource not found")
	// ErrUnexpectedResponse indicates the server returned a non-JSON body where a
	// JSON response was expected — typically an HTML error page from an edge proxy
	// or WAF — and is distinct from a genuine JSON syntax error from the API.
	ErrUnexpectedResponse = errors.New("jamfprotect: unexpected non-JSON response")
	// ErrResponseTooLarge indicates a response body exceeded the client's size
	// limit and was not read in full.
	ErrResponseTooLarge = errors.New("jamfprotect: response body too large")
	// ErrPaginationLimit indicates a paginated list was abandoned because the
	// server repeated a cursor or the page limit was reached.
	ErrPaginationLimit = errors.New("jamfprotect: pagination limit exceeded")
)

Sentinel errors returned by the client.

Functions

func CacheKey added in v0.2.0

func CacheKey(baseURL, clientID string) string

CacheKey computes a deterministic cache key from a base URL and client ID.

func ListAll

func ListAll[T any](
	ctx context.Context,
	c *Client,
	endpoint string,
	query string,
	baseVars map[string]any,
	resultKey string,
) ([]T, error)

ListAll executes a paginated GraphQL list query, accumulating all pages. The resultKey must match the JSON field name of the list operation in the GraphQL response (e.g. "listGroups", "listRoles").

Types

type Client

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

Client communicates with the Jamf Protect GraphQL API.

func NewClient

func NewClient(baseURL, clientID, clientSecret string) *Client

NewClient creates a new Jamf Protect GraphQL client.

func NewClientWithUserAgent

func NewClientWithUserAgent(baseURL, clientID, clientSecret, userAgent string, opts ...Option) *Client

NewClientWithUserAgent creates a new Jamf Protect GraphQL client with a custom user agent string.

func (*Client) AccessToken

func (c *Client) AccessToken(ctx context.Context) (*Token, error)

AccessToken ensures a valid token is available and returns it. Tokens returned by Jamf Protect do not include a "Bearer" prefix.

func (*Client) BaseURL

func (c *Client) BaseURL() string

BaseURL returns the base URL configured for the client.

func (*Client) DoGraphQL

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

DoGraphQL executes a raw GraphQL query/mutation against a custom endpoint path. Use "/app" for the main API and "/graphql" for the limited schema endpoint.

func (*Client) NewCursorGuard added in v0.9.0

func (c *Client) NewCursorGuard() *CursorGuard

NewCursorGuard returns a CursorGuard for one paginated call, using the client's page limit.

func (*Client) SetLogger

func (c *Client) SetLogger(logger Logger)

SetLogger sets the logger for the client.

type CursorGuard added in v0.9.0

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

CursorGuard stops pagination that would never terminate. It rejects any cursor already returned earlier in the same run and caps the page count.

func (*CursorGuard) Next added in v0.9.0

func (g *CursorGuard) Next(cursor string) error

Next records that a page was received with the given next cursor and reports ErrPaginationLimit if following it would revisit a cursor or exceed the page limit.

type FileTokenCache added in v0.2.0

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

FileTokenCache persists tokens to disk as JSON files. The directory must be private to the running user: entries are only read from, and written to, a directory owned by the current user that group and others cannot write.

func NewFileTokenCache added in v0.2.0

func NewFileTokenCache(dir string) *FileTokenCache

NewFileTokenCache creates a FileTokenCache that stores tokens in the given directory.

func (*FileTokenCache) Load added in v0.2.0

func (c *FileTokenCache) Load(key string) (string, time.Time, bool)

Load reads a cached token from disk. Entries that are not regular files owned by the current user, are accessible by group or others, live in a directory that is not private, or carry an implausible expiry are treated as a miss.

func (*FileTokenCache) Store added in v0.2.0

func (c *FileTokenCache) Store(key string, token string, expiresAt time.Time) error

Store writes a token to disk. The token is written to a freshly created 0600 temporary file and renamed into place, so an existing file or symlink at the cache path is replaced rather than written through.

type Logger

type Logger interface {
	LogRequest(ctx context.Context, method, url string, headers http.Header, body []byte)
	LogResponse(ctx context.Context, statusCode int, headers http.Header, body []byte)
}

Logger is an interface for logging HTTP requests and responses.

type Option

type Option func(*Client)

Option configures a Client.

func WithHTTPClient

func WithHTTPClient(httpClient *http.Client) Option

WithHTTPClient overrides the HTTP client used by the API client.

func WithMaxPages added in v0.9.0

func WithMaxPages(n int) Option

WithMaxPages sets how many pages a single paginated call may follow before failing with ErrPaginationLimit. The default is 10,000; a non-positive value keeps the default.

func WithMinRequestInterval added in v0.6.0

func WithMinRequestInterval(d time.Duration) Option

WithMinRequestInterval sets the minimum gap enforced between outbound GraphQL requests. The default is 100ms; a non-positive value disables throttling.

func WithTokenCache added in v0.2.0

func WithTokenCache(cache TokenCache, cacheKey string) Option

WithTokenCache sets a persistent token cache and its lookup key.

type PaginatedResult

type PaginatedResult[T any] struct {
	Items    []T `json:"items"`
	PageInfo struct {
		Next  *string `json:"next"`
		Total int     `json:"total"`
	} `json:"pageInfo"`
}

PaginatedResult is the common shape returned by all paginated list queries.

type Token

type Token struct {
	AccessToken string
	TokenType   string
	Expiry      time.Time
}

Token holds an access token and its metadata.

func (*Token) Valid

func (t *Token) Valid() bool

Valid reports whether the token is present and not expired.

type TokenCache added in v0.2.0

type TokenCache interface {
	Load(key string) (token string, expiresAt time.Time, ok bool)
	Store(key string, token string, expiresAt time.Time) error
}

TokenCache persists OAuth2 tokens across process restarts.

Jump to

Keyboard shortcuts

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