Documentation
¶
Overview ¶
Package pubchem is the library behind the pubchem command line: the HTTP client, request shaping, and the typed data models for the PubChem REST API (pubchem.ncbi.nlm.nih.gov).
The Client here is the spine every command shares. It sets a real User-Agent, paces requests so a busy session stays polite (<=5 req/s), and retries transient failures (429 and 5xx) that the public API throws under load. All domain methods build their URLs and decode JSON on top of it.
Index ¶
- Constants
- func ParseCID(s string) (int, bool)
- type Client
- func (c *Client) Get(ctx context.Context, rawURL string) ([]byte, error)
- func (c *Client) GetCompound(ctx context.Context, cid int) (*Compound, error)
- func (c *Client) GetCompoundByName(ctx context.Context, name string) (*Compound, error)
- func (c *Client) GetSynonyms(ctx context.Context, cid int) (*Synonyms, error)
- func (c *Client) GetSynonymsByName(ctx context.Context, name string) (*Synonyms, error)
- func (c *Client) SearchCompounds(ctx context.Context, query string, limit int) ([]*Compound, error)
- type Compound
- type Config
- type Domain
- type Synonyms
Constants ¶
const AutocompleteURL = "https://pubchem.ncbi.nlm.nih.gov/rest/autocomplete"
AutocompleteURL is the root for the autocomplete endpoint (not under /rest/pug).
const BaseURL = "https://pubchem.ncbi.nlm.nih.gov/rest/pug"
BaseURL is the PubChem PUG REST root.
const DefaultUserAgent = "pubchem-cli/dev (+https://github.com/tamnd/pubchem-cli)"
DefaultUserAgent identifies the client to PubChem.
const Host = "pubchem.ncbi.nlm.nih.gov"
Host is the canonical hostname for PubChem.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Client ¶
type Client struct {
HTTP *http.Client
UserAgent string
BaseURL string
Rate time.Duration
Retries int
// contains filtered or unexported fields
}
Client talks to the PubChem REST API over HTTPS.
func NewClientFromConfig ¶
NewClientFromConfig builds a Client from a Config, falling back to DefaultConfig for zero fields.
func (*Client) Get ¶
Get fetches the given URL and returns the body. It paces and retries on transient errors (429, 5xx). The body is fully read and closed.
func (*Client) GetCompound ¶
GetCompound fetches a compound by CID and returns its normalized record.
func (*Client) GetCompoundByName ¶
GetCompoundByName fetches the first compound matching name.
func (*Client) GetSynonyms ¶
GetSynonyms fetches all known synonyms for a compound by CID.
func (*Client) GetSynonymsByName ¶
GetSynonymsByName fetches synonyms for a compound by name, first resolving the name to a CID via properties.
func (*Client) SearchCompounds ¶
SearchCompounds autocompletes query, then fetches properties for each suggested name, deduplicating by CID. At most limit compounds are returned (limit <= 0 means 10).
type Compound ¶
type Compound struct {
CID int `json:"cid" kit:"id"`
IUPACName string `json:"iupac_name"`
Formula string `json:"formula"`
Weight string `json:"weight"`
InChI string `json:"inchi"`
InChIKey string `json:"inchikey"`
SMILES string `json:"smiles"`
XLogP float64 `json:"xlogp,omitempty"`
TPSA float64 `json:"tpsa,omitempty"`
HBondDonors int `json:"hbond_donors,omitempty"`
HBondAcceptors int `json:"hbond_acceptors,omitempty"`
RotatableBonds int `json:"rotatable_bonds,omitempty"`
Complexity float64 `json:"complexity,omitempty"`
URL string `json:"url"`
}
Compound is a normalized PubChem compound record.
type Config ¶
type Config struct {
BaseURL string
UserAgent string
Rate time.Duration // min gap between requests; 200ms = 5 req/s cap
Timeout time.Duration
Retries int
}
Config holds the tunables for a Client. Zero values fall back to the defaults in DefaultConfig.
func DefaultConfig ¶
func DefaultConfig() Config
DefaultConfig returns the configuration PubChem recommends.
type Domain ¶
type Domain struct{}
Domain is the PubChem driver. It carries no state; the per-run client is built by the factory Register hands kit.
func (Domain) Classify ¶
Classify turns any accepted input into a canonical (uriType, id). Numeric inputs are CIDs; anything else is treated as a compound name.
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.