Documentation
¶
Overview ¶
Package usda is the library behind the usda command line: the HTTP client, request shaping, and typed data models for the USDA FoodData Central API (api.nal.usda.gov/fdc).
Use the DEMO_KEY (default) or supply your own API key via Config.APIKey. DEMO_KEY is rate-limited to 30 requests per hour per IP; the default Rate of 3 seconds keeps a typical interactive session well clear of that limit.
Index ¶
- Constants
- Variables
- type Client
- func (c *Client) Food(ctx context.Context, fdcID int) (*Food, error)
- func (c *Client) Get(ctx context.Context, rawURL string) ([]byte, error)
- func (c *Client) List(ctx context.Context, dataType string, limit int) ([]*Food, error)
- func (c *Client) Search(ctx context.Context, query, dataType string, limit int) ([]*Food, error)
- type Config
- type Domain
- type Food
- type Nutrient
Constants ¶
const DefaultAPIKey = "DEMO_KEY"
DefaultAPIKey is the USDA demo key, rate-limited to 30 req/hour per IP.
const DefaultBaseURL = "https://api.nal.usda.gov/fdc"
DefaultBaseURL is the root every request is built from.
const DefaultUserAgent = "usda/dev (+https://github.com/tamnd/usda-cli)"
DefaultUserAgent identifies the client to the USDA FDC API.
const Host = "api.nal.usda.gov"
Host is the API hostname; the URI driver in domain.go claims it.
Variables ¶
var ErrNotFound = errors.New("not found")
ErrNotFound is returned when the API returns 404 for a food ID.
Functions ¶
This section is empty.
Types ¶
type Client ¶
type Client struct {
HTTP *http.Client
UserAgent string
// Rate is the minimum gap between requests. Zero means no pacing.
Rate time.Duration
Retries int
// contains filtered or unexported fields
}
Client talks to api.nal.usda.gov over HTTP.
func (*Client) List ¶
List lists foods of a given data type. dataType filters by data type (e.g. "Foundation"); empty means all. limit 0 uses the API default of 25.
type Config ¶
type Config struct {
BaseURL string
APIKey string
UserAgent string
Rate time.Duration
Retries int
Timeout time.Duration
}
Config holds constructor parameters.
type Domain ¶
type Domain struct{}
Domain is the usda driver.
func (Domain) Info ¶
func (Domain) Info() kit.DomainInfo
Info describes the scheme, the hostnames a pasted link is matched against, and the identity reused for the binary's help and version.