Client
Create and configure the client, and read the metadata of every API response.
A Client is safe for concurrent use. Create one and reuse it for every request. It has no Close method.
NewClient
Returns a client that authenticates with apiKey.
func NewClient(apiKey string, opts ...Option) (*Client, error)
client, err := synthient.NewClient(os.Getenv("SYNTHIENT_API_KEY"),
synthient.WithMaxRetries(4),
synthient.WithUserAgent("my-app/1.0"),
)
if err != nil {
log.Fatal(err)
}
- Input: Surrounding whitespace is trimmed from the key, and an empty key returns
ErrMissingAPIKey. An invalid option, such as a malformed base URL or a negative retry count, also returns an error. - Network: It performs no network I/O. To check a key, call
Account, which costs no credits. - Auth: The key is sent in the
x-api-keyheader. A key's scopes decide what it can call:BASICcovers the lookups andAccount, and each feed has its own scopes, listed under Feeds.
Options
Each option is a function passed to NewClient.
- Name
WithBaseURL- Type
- func(u string) Option
- Description
Sets the API base URL, including the version path. The default is
DefaultBaseURL. It must be anhttporhttpsURL with a host and no query or fragment.
- Name
WithHTTPClient- Type
- func(hc *http.Client) Option
- Description
Sets the HTTP client used to send requests. The client is copied and never modified. Don't set
http.Client.Timeouton it, because that would also cut off streams and downloads; use context deadlines instead. The default client bounds the wait for response headers to one minute. Custom transports must not replay requests whose bodies cannot be rewound.
- Name
WithMaxRetries- Type
- func(n int) Option
- Description
Sets how many times a failed request is retried. The default is
2, and0disables retries. See Retries and timeouts.
- Name
WithUserAgent- Type
- func(s string) Option
- Description
Appends
s, such as"my-app/1.2", to theUser-Agentheader, which starts withsynthient-go/followed by the SDK version.
- Name
WithResponseHook- Type
- func(hook func(context.Context, ResponseMetadata)) Option
- Description
Calls
hookafter each API response's headers arrive. See Response metadata.
Use WithBaseURL to send requests elsewhere, such as a mock server in tests:
client, err := synthient.NewClient(os.Getenv("SYNTHIENT_API_KEY"),
synthient.WithBaseURL("http://localhost:8080/api/v4"),
)
if err != nil {
log.Fatal(err)
}
Pass your own *http.Client with WithHTTPClient, for example to use a proxy. Clone the default transport to keep its connection and TLS settings:
proxyURL, err := url.Parse("http://proxy.example.com:8080")
if err != nil {
log.Fatal(err)
}
transport := http.DefaultTransport.(*http.Transport).Clone()
transport.Proxy = http.ProxyURL(proxyURL)
transport.ResponseHeaderTimeout = time.Minute
client, err := synthient.NewClient(os.Getenv("SYNTHIENT_API_KEY"),
synthient.WithHTTPClient(&http.Client{Transport: transport}),
)
if err != nil {
log.Fatal(err)
}
- Timeouts: Set
ResponseHeaderTimeoutyourself, because your client replaces the default one-minute limit.
Constants and variables
- Name
DefaultBaseURL- Type
- const string
- Description
"https://api.synthient.com/api/v4", the base URL of the Synthient HTTP API.
- Name
Version- Type
- const string
- Description
The SDK version, such as
"1.0.0". It is sent in theUser-Agentheader.
- Name
ErrMissingAPIKey- Type
- error
- Description
Returned by
NewClientwhen the API key is empty or all whitespace.
Response metadata
WithResponseHook calls a function synchronously after each API response's headers arrive.
client, err := synthient.NewClient(os.Getenv("SYNTHIENT_API_KEY"),
synthient.WithResponseHook(func(_ context.Context, meta synthient.ResponseMetadata) {
if meta.RateLimit != nil {
fmt.Println(meta.StatusCode, meta.RequestID, meta.RateLimit.Remaining, meta.RateLimit.Reset)
}
}),
)
if err != nil {
log.Fatal(err)
}
- Coverage: The hook sees retries, pages, stream handshakes, and decoding failures. Storage download responses are excluded.
- Context: The context belongs to the caller's operation.
- Concurrency: The hook must be safe for concurrent use, and it may retain or modify the metadata without affecting the client.
- Disabling: A
nilhook disables the callback.
ResponseMetadata
- Name
StatusCode- Type
- int
- Description
The HTTP status code of the response.
- Name
RequestID- Type
- string
- Description
The
X-Request-Idheader.
- Name
RateLimit- Type
- *RateLimit
- Description
The rate limit that applies to the operation, or
nilif the response does not report one, as onHealth.
- Name
Header- Type
- http.Header
- Description
An independent copy of the response headers. Headers can contain sensitive data, such as presigned download URLs. Avoid logging
meta.Headerin production.
RateLimit
The state of an operation's rate limit, from the RateLimit-Limit, RateLimit-Remaining, and RateLimit-Reset headers. Limits are shared by every key of a team. See Rate Limits.
- Name
Limit- Type
- int
- Description
The bucket size, which is the burst the operation allows.
- Name
Remaining- Type
- int
- Description
The number of requests left in the bucket.
- Name
Reset- Type
- .
- Description
How long until the bucket is full again.