pubchem

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 15, 2026 License: Apache-2.0 Imports: 11 Imported by: 0

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

View Source
const AutocompleteURL = "https://pubchem.ncbi.nlm.nih.gov/rest/autocomplete"

AutocompleteURL is the root for the autocomplete endpoint (not under /rest/pug).

View Source
const BaseURL = "https://pubchem.ncbi.nlm.nih.gov/rest/pug"

BaseURL is the PubChem PUG REST root.

View Source
const DefaultUserAgent = "pubchem-cli/dev (+https://github.com/tamnd/pubchem-cli)"

DefaultUserAgent identifies the client to PubChem.

View Source
const Host = "pubchem.ncbi.nlm.nih.gov"

Host is the canonical hostname for PubChem.

Variables

This section is empty.

Functions

func ParseCID

func ParseCID(s string) (int, bool)

ParseCID tries to parse s as a CID integer. Returns (cid, true) on success.

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 NewClient

func NewClient() *Client

NewClient returns a Client with DefaultConfig settings.

func NewClientFromConfig

func NewClientFromConfig(cfg Config) *Client

NewClientFromConfig builds a Client from a Config, falling back to DefaultConfig for zero fields.

func (*Client) Get

func (c *Client) Get(ctx context.Context, rawURL string) ([]byte, error)

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

func (c *Client) GetCompound(ctx context.Context, cid int) (*Compound, error)

GetCompound fetches a compound by CID and returns its normalized record.

func (*Client) GetCompoundByName

func (c *Client) GetCompoundByName(ctx context.Context, name string) (*Compound, error)

GetCompoundByName fetches the first compound matching name.

func (*Client) GetSynonyms

func (c *Client) GetSynonyms(ctx context.Context, cid int) (*Synonyms, error)

GetSynonyms fetches all known synonyms for a compound by CID.

func (*Client) GetSynonymsByName

func (c *Client) GetSynonymsByName(ctx context.Context, name string) (*Synonyms, error)

GetSynonymsByName fetches synonyms for a compound by name, first resolving the name to a CID via properties.

func (*Client) SearchCompounds

func (c *Client) SearchCompounds(ctx context.Context, query string, limit int) ([]*Compound, error)

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

func (Domain) Classify(input string) (uriType, id string, err error)

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.

func (Domain) Locate

func (Domain) Locate(uriType, id string) (string, error)

Locate returns the live https URL for a (uriType, id).

func (Domain) Register

func (Domain) Register(app *kit.App)

Register installs the client factory and every operation onto app.

type Synonyms

type Synonyms struct {
	CID      int      `json:"cid" kit:"id"`
	Synonyms []string `json:"synonyms"`
}

Synonyms is a list of known names for one compound.

Jump to

Keyboard shortcuts

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