Documentation
¶
Overview ¶
Package dkapi provides shared primitives for Google Developer Knowledge API clients.
This module complements (rather than replaces) the official generated client at google.golang.org/api/developerknowledge/v1. It focuses on auth-mode selection, quota-project handling for local ADC, rate-limit retry, batch bisection helpers, and document-name normalization used by dkcli, gcp-docs-mirror-tools, and spanner-mycli.
Authentication supports API keys (DEVELOPERKNOWLEDGE_API_KEY or GOOGLE_API_KEY) and Application Default Credentials. When CLOUDSDK_CONFIG is set, its ADC file provides both token and quota-project metadata when present. If that optional file is absent, standard ADC discovery continues; other path or read errors are returned.
Index ¶
- Constants
- func APIKeyFromEnv() string
- func CheckResponse(resp *http.Response) ([]byte, error)
- func DefaultADCCredentialsPath(goos, homeDir, appData string) string
- func DefaultCredentialsPath() string
- func IsBisectableDocumentError(err error) bool
- func NewADCHTTPClient(ctx context.Context, cfg AuthConfig) (*http.Client, error)
- func NewAuthenticatedHTTPClient(ctx context.Context, cfg AuthConfig) (*http.Client, string, error)
- func NormalizeDocName(name string) string
- func ParseRetryAfter(resp *http.Response) time.Duration
- func SleepContext(ctx context.Context, wait time.Duration) error
- type ADCCredentialsMetadata
- type APIError
- type AuthConfig
- type AuthMode
- type BatchGetResponse
- type Client
- func (c *Client) BatchGetDocuments(ctx context.Context, names []string) ([]Document, error)
- func (c *Client) BatchGetDocumentsAll(ctx context.Context, names []string) ([]Document, error)
- func (c *Client) DoAPIRequest(ctx context.Context, method, reqURL string, body []byte, contentType string) ([]byte, error)
- func (c *Client) DoGet(ctx context.Context, reqURL string) ([]byte, error)
- func (c *Client) DoJSONPost(ctx context.Context, reqURL string, body []byte) ([]byte, error)
- type Document
- type DocumentChunk
- type QuotaProjectTransport
- type RateLimitError
- type TokenSourceFunc
- type Waiter
Constants ¶
const ( CloudPlatformScope = "https://www.googleapis.com/auth/cloud-platform" DefaultV1BaseURL = "https://developerknowledge.googleapis.com/v1" DefaultHTTPTimeout = time.Minute // MaxBatchGetDocuments is the maximum number of document names accepted by // documents:batchGet. Documents are returned in the same order as names. MaxBatchGetDocuments = 20 )
Variables ¶
This section is empty.
Functions ¶
func APIKeyFromEnv ¶
func APIKeyFromEnv() string
func DefaultCredentialsPath ¶
func DefaultCredentialsPath() string
func NewADCHTTPClient ¶
NewADCHTTPClient constructs an OAuth-authenticated HTTP client. Initial requests are restricted to AuthConfig.AllowedOrigin, which defaults to DefaultV1BaseURL, and redirects must remain on that origin.
func NewAuthenticatedHTTPClient ¶
NewAuthenticatedHTTPClient prefers an environment API key unless ADC is required. Initial requests are restricted to AuthConfig.AllowedOrigin, which defaults to DefaultV1BaseURL, and redirects must remain on that origin.
func NormalizeDocName ¶
NormalizeDocName converts a pasted URL or short document path into a Developer Knowledge API resource name (documents/...). Query strings, fragments, and trailing slashes are stripped. URL-like inputs must be hierarchical ASCII HTTP(S) URLs without userinfo; empty or invalid inputs return "".
Types ¶
type ADCCredentialsMetadata ¶
type ADCCredentialsMetadata struct {
Type string `json:"type"`
QuotaProjectID string `json:"quota_project_id"`
}
func LoadADCCredentialsMetadata ¶
func LoadADCCredentialsMetadata(credentialsPath func() string) ADCCredentialsMetadata
func ResolveQuotaProjectID ¶
func ResolveQuotaProjectID(credentialsPath func() string) (string, ADCCredentialsMetadata)
type AuthConfig ¶
type AuthConfig struct {
Mode AuthMode
Timeout time.Duration
// AllowedOrigin is the hierarchical ASCII HTTP(S) origin accepted by
// constructor-created clients. It defaults to DefaultV1BaseURL.
AllowedOrigin string
// TokenSource overrides ADC discovery. When set, CredentialsPath is ignored.
TokenSource TokenSourceFunc
// QuotaProjectID explicitly sets the x-goog-user-project header for ADC
// clients. It takes precedence over GOOGLE_CLOUD_QUOTA_PROJECT and
// credentials-file metadata.
QuotaProjectID string
// CredentialsPath returns an explicit ADC file path. When set, the path is
// evaluated once and must be readable; ADC discovery does not fall back.
CredentialsPath func() string
}
type BatchGetResponse ¶
type BatchGetResponse struct {
Documents []Document `json:"documents" yaml:"documents"`
}
type Client ¶
type Client struct {
BaseURL string
APIKey string
HTTPClient *http.Client
Limiter Waiter
Verbose bool
VerboseWriter io.Writer
// MaxRetries is the number of additional attempts after the first request.
MaxRetries int
}
func (*Client) BatchGetDocuments ¶
func (*Client) BatchGetDocumentsAll ¶ added in v0.2.0
BatchGetDocumentsAll fetches documents in chunks of MaxBatchGetDocuments while preserving the order of names. Invalid names fail the whole batch for that chunk.
func (*Client) DoAPIRequest ¶
func (c *Client) DoAPIRequest(ctx context.Context, method, reqURL string, body []byte, contentType string) ([]byte, error)
DoAPIRequest sends an authenticated API request. reqURL must be absolute and share an origin with BaseURL; redirects to another origin are rejected.
type Document ¶
type Document struct {
Name string `json:"name" yaml:"name"`
URI string `json:"uri" yaml:"uri"`
Content string `json:"content,omitempty" yaml:"content,omitempty"`
Description string `json:"description,omitempty" yaml:"description,omitempty"`
DataSource string `json:"dataSource,omitempty" yaml:"data_source,omitempty"`
Title string `json:"title,omitempty" yaml:"title,omitempty"`
UpdateTime string `json:"updateTime,omitempty" yaml:"update_time,omitempty"`
View string `json:"view,omitempty" yaml:"view,omitempty"`
ContentLengthBytes int64 `json:"contentLengthBytes,omitempty" yaml:"content_length_bytes,omitempty"`
}
type DocumentChunk ¶
type QuotaProjectTransport ¶
type QuotaProjectTransport struct {
Base http.RoundTripper
Project string
}
func (*QuotaProjectTransport) CloseIdleConnections ¶ added in v0.3.0
func (t *QuotaProjectTransport) CloseIdleConnections()
type RateLimitError ¶
func (*RateLimitError) Error ¶
func (e *RateLimitError) Error() string
type TokenSourceFunc ¶
var DefaultTokenSource TokenSourceFunc = func(ctx context.Context, scopes ...string) (oauth2.TokenSource, error) { return google.DefaultTokenSource(ctx, scopes...) }