Documentation
¶
Overview ¶
Package demografix is the official Go client for the Demografix APIs: genderize.io (gender), agify.io (age), and nationalize.io (nationality). One client covers all three services through the same shape and reports the remaining quota carried on every response.
Index ¶
- Constants
- type AgifyBatchResult
- type AgifyPrediction
- type AgifyResult
- type AuthError
- type Client
- func (c *Client) Agify(ctx context.Context, name string, opts ...RequestOption) (AgifyResult, error)
- func (c *Client) AgifyBatch(ctx context.Context, names []string, opts ...RequestOption) (AgifyBatchResult, error)
- func (c *Client) Genderize(ctx context.Context, name string, opts ...RequestOption) (GenderizeResult, error)
- func (c *Client) GenderizeBatch(ctx context.Context, names []string, opts ...RequestOption) (GenderizeBatchResult, error)
- func (c *Client) Nationalize(ctx context.Context, name string) (NationalizeResult, error)
- func (c *Client) NationalizeBatch(ctx context.Context, names []string) (NationalizeBatchResult, error)
- type DemografixError
- type GenderizeBatchResult
- type GenderizePrediction
- type GenderizeResult
- type NationalizeBatchResult
- type NationalizeCountry
- type NationalizePrediction
- type NationalizeResult
- type Option
- type Quota
- type RateLimitError
- type RequestOption
- type SubscriptionError
- type TransportError
- type ValidationError
Constants ¶
const (
// Version is the SDK version, sent in the User-Agent on every request.
Version = "0.1.0"
)
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type AgifyBatchResult ¶
type AgifyBatchResult struct {
Results []AgifyPrediction
Quota Quota
}
AgifyBatchResult holds the per-name agify predictions in input order plus one quota for the whole response.
type AgifyPrediction ¶
type AgifyPrediction struct {
Name string `json:"name"`
Age *int `json:"age"`
Count int `json:"count"`
CountryID string `json:"country_id"`
}
AgifyPrediction is one agify result for a single name. Age is nil when the API returns null. CountryID is populated only when the request sent a country_id.
type AgifyResult ¶
type AgifyResult struct {
AgifyPrediction
Quota Quota
}
AgifyResult is a single agify prediction plus the response quota.
type AuthError ¶
type AuthError struct{ DemografixError }
AuthError reports an invalid or rejected API key (HTTP 401).
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client calls the three Demografix services. Construct one with New and reuse it; it is safe for concurrent use. The hosts and the User-Agent are hardcoded constants, not options.
func New ¶
New builds a Client for the given API key. The key is required and is sent as the apikey query parameter on every request; an empty or blank key makes every request fail with a ValidationError before any HTTP call. Pass WithTimeout to override the default ten-second timeout. The same key works across all three services.
func (*Client) Agify ¶
func (c *Client) Agify(ctx context.Context, name string, opts ...RequestOption) (AgifyResult, error)
Agify predicts the age for one name.
func (*Client) AgifyBatch ¶
func (c *Client) AgifyBatch(ctx context.Context, names []string, opts ...RequestOption) (AgifyBatchResult, error)
AgifyBatch predicts the age for up to ten names. Results are returned in input order. A batch of more than ten names raises a ValidationError before any HTTP call.
func (*Client) Genderize ¶
func (c *Client) Genderize(ctx context.Context, name string, opts ...RequestOption) (GenderizeResult, error)
Genderize predicts the gender for one name.
func (*Client) GenderizeBatch ¶
func (c *Client) GenderizeBatch(ctx context.Context, names []string, opts ...RequestOption) (GenderizeBatchResult, error)
GenderizeBatch predicts the gender for up to ten names. Results are returned in input order. A batch of more than ten names raises a ValidationError before any HTTP call.
func (*Client) Nationalize ¶
Nationalize predicts the nationality for one name.
func (*Client) NationalizeBatch ¶
func (c *Client) NationalizeBatch(ctx context.Context, names []string) (NationalizeBatchResult, error)
NationalizeBatch predicts the nationality for up to ten names. Results are returned in input order. A batch of more than ten names raises a ValidationError before any HTTP call.
type DemografixError ¶
type DemografixError struct {
// Status is the HTTP status code, or 0 when no response was received.
Status int
// Message is the API error string passed through unchanged.
Message string
// Quota is the parsed rate-limit state, or nil when no headers were present.
Quota *Quota
}
DemografixError is the base error type for every failure the SDK reports. Every typed error embeds it, so it carries the HTTP status, the API message, and the response quota when the rate-limit headers were present.
Discover a specific error type with errors.As:
var rate *demografix.RateLimitError
if errors.As(err, &rate) {
time.Sleep(time.Duration(rate.Quota.Reset) * time.Second)
}
The base type itself is also matchable, so errors.As against *DemografixError succeeds for any SDK error.
func AsDemografixError ¶
func AsDemografixError(err error) (*DemografixError, bool)
AsDemografixError reports whether err is a Demografix SDK error and, if so, returns the embedded base. It is a convenience over errors.As for callers that want the status, message, and quota without naming a concrete type.
func (*DemografixError) Error ¶
func (e *DemografixError) Error() string
Error implements the error interface.
type GenderizeBatchResult ¶
type GenderizeBatchResult struct {
Results []GenderizePrediction
Quota Quota
}
GenderizeBatchResult holds the per-name genderize predictions in input order plus one quota for the whole response.
type GenderizePrediction ¶
type GenderizePrediction struct {
Name string `json:"name"`
Gender string `json:"gender"`
Probability float64 `json:"probability"`
Count int `json:"count"`
CountryID string `json:"country_id"`
}
GenderizePrediction is one genderize result for a single name. Gender is "male", "female", or "" when the API returns null. CountryID is populated only when the request sent a country_id.
type GenderizeResult ¶
type GenderizeResult struct {
GenderizePrediction
Quota Quota
}
GenderizeResult is a single genderize prediction plus the response quota.
type NationalizeBatchResult ¶
type NationalizeBatchResult struct {
Results []NationalizePrediction
Quota Quota
}
NationalizeBatchResult holds the per-name nationalize predictions in input order plus one quota for the whole response.
type NationalizeCountry ¶
type NationalizeCountry struct {
CountryID string `json:"country_id"`
Probability float64 `json:"probability"`
}
NationalizeCountry is one candidate country for a nationalize prediction.
type NationalizePrediction ¶
type NationalizePrediction struct {
Name string `json:"name"`
Country []NationalizeCountry `json:"country"`
Count int `json:"count"`
}
NationalizePrediction is one nationalize result for a single name. Country holds up to five candidates in descending probability and is empty on no match.
type NationalizeResult ¶
type NationalizeResult struct {
NationalizePrediction
Quota Quota
}
NationalizeResult is a single nationalize prediction plus the response quota.
type Option ¶
type Option func(*Client)
Option configures a Client in New.
func WithTimeout ¶
WithTimeout sets the per-request timeout. The default is ten seconds.
type Quota ¶
type Quota struct {
// Limit is the number of names allowed in the current window.
Limit int
// Remaining is the number of names left in the current window.
Remaining int
// Reset is the number of seconds until the window resets.
Reset int
}
Quota reports the rate-limit state carried on every response, parsed from the x-rate-limit-* headers. It is read from a returned value or a raised error and is never cached on the Client.
type RateLimitError ¶
type RateLimitError struct{ DemografixError }
RateLimitError reports an exhausted quota (HTTP 429). Quota is always populated; read Quota.Reset for the seconds to wait before retrying.
func (*RateLimitError) Unwrap ¶
func (e *RateLimitError) Unwrap() error
Unwrap exposes the embedded base, so errors.As matches *DemografixError too.
type RequestOption ¶
type RequestOption func(*requestConfig)
RequestOption configures a single genderize or agify request. The only option is WithCountry.
func WithCountry ¶
func WithCountry(countryID string) RequestOption
WithCountry scopes a genderize or agify prediction to an ISO 3166-1 alpha-2 country. The nationalize methods do not accept it.
type SubscriptionError ¶
type SubscriptionError struct{ DemografixError }
SubscriptionError reports an inactive or expired subscription (HTTP 402).
func (*SubscriptionError) Unwrap ¶
func (e *SubscriptionError) Unwrap() error
Unwrap exposes the embedded base, so errors.As matches *DemografixError too.
type TransportError ¶
type TransportError struct {
DemografixError
// Err is the underlying cause, or nil for a synthesized failure such as a
// non-JSON response body.
Err error
}
TransportError reports a network failure, a timeout, or a response body that is not JSON. Status and Quota may be absent. When the failure came from an underlying call, Err holds the original error, so errors.Is and errors.As reach it (for example errors.Is(err, context.DeadlineExceeded) on a timeout).
func (*TransportError) Unwrap ¶
func (e *TransportError) Unwrap() []error
Unwrap exposes both the embedded base and the underlying cause, so errors.As matches *DemografixError and errors.Is reaches the wrapped transport error.
type ValidationError ¶
type ValidationError struct{ DemografixError }
ValidationError reports a rejected request (HTTP 422). It is also raised client-side, before any HTTP call, when a batch holds more than ten names.
func (*ValidationError) Unwrap ¶
func (e *ValidationError) Unwrap() error
Unwrap exposes the embedded base, so errors.As matches *DemografixError too.
Directories
¶
| Path | Synopsis |
|---|---|
|
Command example reads a list of names and reports the aggregate demographic mix: a gender split, an age distribution, and a nationality breakdown.
|
Command example reads a list of names and reports the aggregate demographic mix: a gender split, an age distribution, and a nationality breakdown. |