client

package
v0.46.0 Latest Latest
Warning

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

Go to latest
Published: Nov 18, 2026 License: Apache-2.0 Imports: 10 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package client re-exports the authenticated Dynatrace HTTP client from github.com/Dynatrace/dtctl/sdk/session, where the implementation moved: client construction from a context — token resolution, OAuth refresh, the 401 retry — is shared session behavior, and the User-Agent is parameterized there so each consumer ships its own identity. This package pins dtctl's identity (dtctl/<version>) and keeps the CLI-only OTel trace propagation; typed API errors (errors.go) and pagination helpers (pagination.go) also remain here.

Index

Constants

View Source
const (
	ExitSuccess         = 0
	ExitError           = 1
	ExitUsageError      = 2
	ExitAuthError       = 3
	ExitNotFoundError   = 4
	ExitPermissionError = 5
)

Error codes matching the CLI error handling spec

Variables

This section is empty.

Functions

func AccountBaseURLForEnvironment

func AccountBaseURLForEnvironment(env auth.Environment) string

AccountBaseURLForEnvironment maps a tier to the account API base URL.

func ExitCodeForStatus

func ExitCodeForStatus(statusCode int) int

ExitCodeForStatus returns the exit code for an HTTP error status.

func ExtractUserIDFromToken

func ExtractUserIDFromToken(token string) (string, error)

ExtractUserIDFromToken extracts the user ID (sub claim) from a JWT token.

func GetTokenForContext

func GetTokenForContext(cfg *config.Config, environmentURL, tokenRef string) (string, error)

GetTokenForContext retrieves a token from config with OAuth token refresh support, detecting the OAuth configuration from the supplied environment URL.

func GetTokenWithOAuthSupport

func GetTokenWithOAuthSupport(cfg *config.Config, tokenRef string) (string, error)

GetTokenWithOAuthSupport retrieves a token from config with OAuth token refresh support, using the current context's environment to detect the OAuth configuration.

func InjectTraceContext

func InjectTraceContext(c *Client, ctx context.Context)

InjectTraceContext configures the client to inject W3C trace context headers (traceparent / tracestate) on every outgoing HTTP request. It lives here — not in the sdk — so the sdk stays OTel-free; tracing is a dtctl CLI concern.

The provided ctx is captured once at registration time; all subsequent HTTP requests share the same span context (the root CLI span). This means individual API calls do NOT get their own child spans — they all propagate the same trace-id and parent span-id. This is intentional for a short-lived CLI: one invocation = one logical operation.

The propagator is resolved once from the global OTel registry at registration time to avoid per-request mutex overhead from otel.GetTextMapPropagator().

func IsPlatformToken

func IsPlatformToken(token string) bool

IsPlatformToken reports whether token is a Dynatrace platform token.

func RefreshedTokenForContext

func RefreshedTokenForContext(cfg *config.Config, environmentURL, tokenRef, rejected string) (string, error)

RefreshedTokenForContext re-resolves the context's bearer token after a request was rejected with HTTP 401 (see session.RefreshedTokenForContext).

func WrapError

func WrapError(err error, message string) error

WrapError wraps an error with additional context

Types

type Client

type Client = session.Client

Client is the authenticated HTTP client for a Dynatrace environment.

func New

func New(baseURL, token string) (*Client, error)

New creates a new client with base URL and token.

func NewForTesting

func NewForTesting(baseURL, token string) (*Client, error)

NewForTesting creates a client with retries disabled, suitable for unit tests that use httptest servers. This avoids the 3×1s retry wait on 500/429 responses.

func NewFromConfig

func NewFromConfig(cfg *config.Config) (*Client, error)

NewFromConfig creates a client for the config's current context with OAuth-aware token resolution and re-resolution on 401.

func NewFromConfigWithOAuth deprecated

func NewFromConfigWithOAuth(cfg *config.Config) (*Client, error)

NewFromConfigWithOAuth creates a new client from config with OAuth support.

Deprecated: Use NewFromConfig instead, which now supports OAuth tokens automatically.

type PaginationParams

type PaginationParams struct {
	// Style determines the pagination variant.
	Style PaginationStyle

	// PageKeyParam is the query parameter name for the page key
	// (e.g. "page-key", "next-page-key", "nextPageKey").
	PageKeyParam string

	// PageSizeParam is the query parameter name for the page size
	// (e.g. "page-size", "pageSize"). May be empty if no page size is used.
	PageSizeParam string

	// NextPageKey is the current page key (empty string for the first page).
	NextPageKey string

	// PageSize is the requested page size. Zero means "use API default / no pagination".
	PageSize int64

	// Filters are additional query parameters (e.g. filter, name, schemaIds, scopes)
	// that should be sent according to the pagination style rules.
	// For Default and DocumentAPI styles, filters are sent on every request.
	// For SettingsAPI style, filters are only sent on the first request (page token embeds them).
	Filters map[string]string
}

PaginationParams configures how pagination query parameters are applied to a request.

func (PaginationParams) Apply

func (p PaginationParams) Apply(req *resty.Request) *resty.Request

Apply sets the pagination query parameters on the given resty request. It returns the request for chaining.

This helper encapsulates the three Dynatrace pagination styles so that callers cannot accidentally send page-size alongside page-key on APIs that reject that combination.

type PaginationStyle

type PaginationStyle int

PaginationStyle controls how page-key and page-size interact.

Dynatrace APIs have three distinct pagination behaviors:

  • Default: page-size must NOT be sent with page-key (HTTP 400). Filter/search params must be resent on every request.

  • DocumentAPI: page-size CAN be sent with page-key. Filter params must be resent (the page token does not embed them).

  • SettingsAPI: when nextPageKey is present, NO other params may be sent (pageSize, schemaIds, scopes, and fields are all embedded in the page token).

const (
	// PaginationDefault is the standard style used by most Dynatrace APIs.
	// page-size is only sent on the first request; filter params are sent on every request.
	PaginationDefault PaginationStyle = iota

	// PaginationDocumentAPI is used by the Document API (/platform/document/v1/).
	// page-size and filter params are sent on every request alongside page-key.
	PaginationDocumentAPI

	// PaginationSettingsAPI is used by the Settings API (/platform/classic/environment-api/v2/settings/).
	// When nextPageKey is present, no other query params may be sent (not even fields).
	PaginationSettingsAPI
)

type UserInfo

type UserInfo = session.UserInfo

UserInfo is the platform metadata user (from /platform/metadata/v1/user).

Jump to

Keyboard shortcuts

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