api

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package api is the demografix CLI's standalone HTTP client for the three Demografix services (genderize.io, agify.io, nationalize.io). It mirrors the SDK's wire contract but is independent of it: the User-Agent is injected (so CLI traffic is distinguishable in the logs) and an API key is required on every request — there is no keyless mode.

Index

Constants

View Source
const (
	// GenderizeBaseURL, AgifyBaseURL and NationalizeBaseURL are the three
	// service hosts. The account is shared across all of them.
	GenderizeBaseURL   = "https://api.genderize.io/"
	AgifyBaseURL       = "https://api.agify.io/"
	NationalizeBaseURL = "https://api.nationalize.io/"

	// DefaultTimeout is the per-request timeout when none is given.
	DefaultTimeout = 10 * time.Second

	// MaxBatch is the largest number of names allowed in one request.
	MaxBatch = 10
)

Variables

This section is empty.

Functions

This section is empty.

Types

type AgifyPrediction

type AgifyPrediction struct {
	Name      string `json:"name"`
	Age       *int   `json:"age"`
	Count     int    `json:"count"`
	CountryID string `json:"country_id,omitempty"`
}

AgifyPrediction is one agify.io result. Age is nil when the API returns null.

type AuthError

type AuthError struct{ DemografixError }

AuthError is a 401 (invalid or missing API key).

type Client

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

Client calls the three services. It is safe for concurrent use.

func New

func New(apiKey, userAgent string, timeout time.Duration) *Client

New builds a Client. apiKey is required; an empty key makes every call fail with a *ValidationError before any HTTP request. userAgent is sent on every request. A non-positive timeout falls back to DefaultTimeout.

func (*Client) Agify

func (c *Client) Agify(ctx context.Context, names []string, countryID string) ([]AgifyPrediction, Quota, error)

Agify predicts the age for up to ten names, returned in input order.

func (*Client) Genderize

func (c *Client) Genderize(ctx context.Context, names []string, countryID string) ([]GenderizePrediction, Quota, error)

Genderize predicts the gender for up to ten names, returned in input order.

func (*Client) Nationalize

func (c *Client) Nationalize(ctx context.Context, names []string) ([]NationalizePrediction, Quota, error)

Nationalize predicts the nationality for up to ten names, returned in input order. The service does not accept a country parameter.

func (*Client) RateLimit

func (c *Client) RateLimit(ctx context.Context) (RateLimit, Quota, error)

RateLimit fetches the account quota from the undocumented GET /rate_limit endpoint. The account is shared across all three services, so the genderize host is used. The JSON body wins; header values fill any field the body omits.

type DemografixError

type DemografixError struct {
	Status  int
	Message string
	Quota   *Quota
}

DemografixError is the base error for every API failure. Status is the HTTP status (0 for client-side or transport failures); Quota is the rate-limit view parsed from the response headers, when present.

func (*DemografixError) Base

func (e *DemografixError) Base() *DemografixError

Base returns the embedded base error. It lets callers reach the status, message, and quota of any typed error via errors.As(err, &apiErr) where apiErr is an Error.

func (*DemografixError) Error

func (e *DemografixError) Error() string

type Error

type Error interface {
	error
	Base() *DemografixError
}

Error is implemented by every Demografix SDK error (the base and each typed variant), exposing the underlying DemografixError.

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,omitempty"`
}

GenderizePrediction is one genderize.io result. Gender is "male", "female", or "" when the API returns null. CountryID is set only when the request was scoped to a country.

type NationalizeCountry

type NationalizeCountry struct {
	CountryID   string  `json:"country_id"`
	Probability float64 `json:"probability"`
}

NationalizeCountry is one candidate country for a nationalize.io result.

type NationalizePrediction

type NationalizePrediction struct {
	Name    string               `json:"name"`
	Country []NationalizeCountry `json:"country"`
	Count   int                  `json:"count"`
}

NationalizePrediction is one nationalize.io result. Country holds up to five candidates in descending probability and is empty on no match.

type Quota

type Quota struct {
	Limit     int `json:"limit"`
	Remaining int `json:"remaining"`
	Reset     int `json:"reset"`
}

Quota is the remaining-quota view carried on every response via the x-rate-limit-* headers. Reset is the number of seconds until the window resets.

type RateLimit

type RateLimit struct {
	Limit     int    `json:"limit"`
	Remaining int    `json:"remaining"`
	Reset     int    `json:"reset"`
	Tier      string `json:"tier"`
}

RateLimit is the body of the undocumented GET /rate_limit endpoint. The same numbers are also carried on the x-rate-limit-* headers.

type RateLimitError

type RateLimitError struct{ DemografixError }

RateLimitError is a 429. Quota is always populated and Quota.Reset carries the seconds until the window resets.

type SubscriptionError

type SubscriptionError struct{ DemografixError }

SubscriptionError is a 402 (subscription inactive or expired).

type TransportError

type TransportError struct {
	DemografixError
	Err error
}

TransportError wraps a network, timeout, or non-JSON-body failure.

func (*TransportError) Unwrap

func (e *TransportError) Unwrap() error

type ValidationError

type ValidationError struct{ DemografixError }

ValidationError is a 422 from the server, or a client-side validation failure (Status 0) raised before any HTTP call.

Jump to

Keyboard shortcuts

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