Documentation
¶
Overview ¶
Package pkgsite provides a small client for the pkg.go.dev v1beta HTTP API.
The API is stateless and GET-only. See https://go.dev/blog/pkgsite-api for background. This client mirrors the surface of the reference client that ships with golang.org/x/pkgsite (cmd/internal/pkgsite-cli/client), but is an independent, dependency-free reimplementation.
Index ¶
- Constants
- type APIError
- type Candidate
- type Client
- func (c *Client) GetImportedBy(ctx context.Context, path, version string, opts ImportedByOptions) (*PackageImportedBy, error)
- func (c *Client) GetModule(ctx context.Context, path, version string, opts ModuleOptions) (*Module, error)
- func (c *Client) GetPackage(ctx context.Context, path, version string, opts PackageOptions) (*Package, error)
- func (c *Client) GetPackages(ctx context.Context, modulePath, version string, opts PaginationOptions) (*PaginatedResponse[ModulePackageResponse], error)
- func (c *Client) GetSymbols(ctx context.Context, path, version string, opts SymbolsOptions) (*PaginatedResponse[Symbol], error)
- func (c *Client) GetVersions(ctx context.Context, path string, opts PaginationOptions) (*PaginatedResponse[VersionResponse], error)
- func (c *Client) GetVulns(ctx context.Context, path, version string, opts PaginationOptions) (*PaginatedResponse[Vulnerability], error)
- func (c *Client) Search(ctx context.Context, query string, opts SearchOptions) (*PaginatedResponse[SearchResult], error)
- type ImportedByOptions
- type License
- type Module
- type ModuleOptions
- type ModulePackageResponse
- type Option
- type Package
- type PackageImportedBy
- type PackageInfo
- type PackageOptions
- type PackageSymbols
- type PackagesResponse
- type PaginatedResponse
- type PaginationOptions
- type Readme
- type SearchOptions
- type SearchResult
- type Symbol
- type SymbolsOptions
- type VersionResponse
- type Vulnerability
Constants ¶
const DefaultServer = "https://pkg.go.dev"
DefaultServer is the public pkg.go.dev API server.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type APIError ¶
type APIError struct {
Code int `json:"code"` // HTTP status code
Message string `json:"message"`
Fixes []string `json:"fixes,omitempty"` // suggestions for how to fix
Candidates []Candidate `json:"candidates,omitempty"` // for ambiguous paths
}
APIError is a structured error returned by the API for a non-200 response. It implements error.
type Candidate ¶
type Candidate struct {
ModulePath string `json:"modulePath"`
PackagePath string `json:"packagePath"`
}
Candidate is a possible resolution for an ambiguous package path.
type Client ¶
type Client struct {
// contains filtered or unexported fields
}
Client fetches data from the pkg.go.dev v1beta API.
func (*Client) GetImportedBy ¶
func (c *Client) GetImportedBy(ctx context.Context, path, version string, opts ImportedByOptions) (*PackageImportedBy, error)
GetImportedBy fetches the packages that import the given package (reverse dependencies).
func (*Client) GetModule ¶
func (c *Client) GetModule(ctx context.Context, path, version string, opts ModuleOptions) (*Module, error)
GetModule fetches information about a module at the given path and version. An empty version means the latest version.
func (*Client) GetPackage ¶
func (c *Client) GetPackage(ctx context.Context, path, version string, opts PackageOptions) (*Package, error)
GetPackage fetches information about a package at the given path and version. An empty version means the latest version.
func (*Client) GetPackages ¶
func (c *Client) GetPackages(ctx context.Context, modulePath, version string, opts PaginationOptions) (*PaginatedResponse[ModulePackageResponse], error)
GetPackages fetches the packages contained in the given module path and version.
func (*Client) GetSymbols ¶
func (c *Client) GetSymbols(ctx context.Context, path, version string, opts SymbolsOptions) (*PaginatedResponse[Symbol], error)
GetSymbols fetches the exported symbols of a package.
func (*Client) GetVersions ¶
func (c *Client) GetVersions(ctx context.Context, path string, opts PaginationOptions) (*PaginatedResponse[VersionResponse], error)
GetVersions fetches the list of versions for the given module path.
func (*Client) GetVulns ¶
func (c *Client) GetVulns(ctx context.Context, path, version string, opts PaginationOptions) (*PaginatedResponse[Vulnerability], error)
GetVulns fetches known vulnerabilities for the given module path and version.
func (*Client) Search ¶
func (c *Client) Search(ctx context.Context, query string, opts SearchOptions) (*PaginatedResponse[SearchResult], error)
Search queries pkg.go.dev for packages matching query.
type ImportedByOptions ¶
type ImportedByOptions struct {
Module string
PaginationOptions
}
ImportedByOptions are options for GetImportedBy.
type License ¶
type License struct {
Types []string `json:"types"`
FilePath string `json:"filePath"`
Contents string `json:"contents,omitempty"`
}
License is license information in API responses.
type Module ¶
type Module struct {
Path string `json:"path"`
Version string `json:"version"`
// CommitTime is the time the version was created, as reported by the
// module proxy's .info endpoint.
CommitTime time.Time `json:"commitTime"`
IsLatest bool `json:"isLatest"`
IsRedistributable bool `json:"isRedistributable"`
IsStandardLibrary bool `json:"isStandardLibrary"`
HasGoMod bool `json:"hasGoMod"`
RepoURL string `json:"repoUrl"`
GoModContents string `json:"goModContents,omitempty"`
Readme *Readme `json:"readme,omitempty"`
Licenses []License `json:"licenses,omitempty"`
}
Module is the response for /v1beta/module/{modulePath}.
type ModuleOptions ¶
type ModuleOptions struct {
Readme bool // include the README contents
Licenses bool // include license information
}
ModuleOptions are options for GetModule.
type ModulePackageResponse ¶
type ModulePackageResponse struct {
Path string `json:"path"`
Name string `json:"name"`
Synopsis string `json:"synopsis"`
}
ModulePackageResponse is a single package listed for a module.
type Option ¶
type Option func(*Client)
Option configures a Client.
func WithCache ¶ added in v0.2.0
WithCache caches successful responses for ttl, keyed by request URL, bounded to maxEntries. Because version-pinned data is immutable and "latest" lookups tolerate a short staleness window, this safely avoids refetching during an assistant's multi-step exploration. A non-positive ttl or maxEntries disables caching.
func WithHTTPClient ¶
WithHTTPClient sets the HTTP client used for requests. The default client has a 30s timeout.
func WithRetry ¶ added in v0.2.0
WithRetry retries transient failures (network errors and HTTP 429/5xx) up to maxRetries additional times, with exponential backoff starting at baseDelay. A maxRetries of 0 disables retries. A baseDelay <= 0 keeps the default.
func WithUserAgent ¶
WithUserAgent sets the User-Agent header sent with each request.
type Package ¶
type Package struct {
ModulePath string `json:"modulePath"`
Version string `json:"version"`
IsLatest bool `json:"isLatest"`
IsStandardLibrary bool `json:"isStandardLibrary"`
GOOS string `json:"goos"`
GOARCH string `json:"goarch"`
Docs string `json:"docs,omitempty"`
Imports []string `json:"imports,omitempty"`
Licenses []License `json:"licenses,omitempty"`
PackageInfo
}
Package is the response for /v1beta/package/{packagePath}.
type PackageImportedBy ¶
type PackageImportedBy struct {
ModulePath string `json:"modulePath"`
Version string `json:"version"`
ImportedBy PaginatedResponse[string] `json:"importedBy"`
}
PackageImportedBy is the response for /v1beta/imported-by/{packagePath}.
type PackageInfo ¶
type PackageInfo struct {
Path string `json:"path"`
Name string `json:"name"`
Synopsis string `json:"synopsis"`
// IsRedistributable reports whether the license allows distribution.
IsRedistributable bool `json:"isRedistributable"`
}
PackageInfo holds the fields common to a package across responses.
type PackageOptions ¶
type PackageOptions struct {
Module string // disambiguate the module path for an ambiguous package path
Doc string // render docs in this format: "text", "md", or "html"
Examples bool // include examples (requires Doc)
Imports bool // list imported packages
Licenses bool // include license information
GOOS string // target GOOS
GOARCH string // target GOARCH
}
PackageOptions are options for GetPackage.
type PackageSymbols ¶
type PackageSymbols struct {
ModulePath string `json:"modulePath"`
Version string `json:"version"`
Symbols PaginatedResponse[Symbol] `json:"symbols"`
}
PackageSymbols is the response for /v1beta/symbols/{packagePath}.
type PackagesResponse ¶
type PackagesResponse struct {
ModulePath string `json:"modulePath"`
Version string `json:"version"`
IsStandardLibrary bool `json:"isStandardLibrary"`
Packages PaginatedResponse[PackageInfo] `json:"packages"`
}
PackagesResponse is the response for /v1beta/packages/{modulePath}.
type PaginatedResponse ¶
type PaginatedResponse[T any] struct { Items []T `json:"items"` Total int `json:"total"` NextPageToken string `json:"nextPageToken,omitempty"` }
PaginatedResponse is a generic paginated response. A non-empty NextPageToken indicates more results are available; pass it back as the token option to fetch the next page.
type PaginationOptions ¶
type PaginationOptions struct {
Limit int // maximum items per page; 0 means the server default
Token string // page token from a previous response's NextPageToken
}
PaginationOptions are common options for paginated endpoints.
type SearchOptions ¶
type SearchOptions struct {
Symbol string // restrict results to packages exporting this symbol
PaginationOptions
}
SearchOptions are options for Search.
type SearchResult ¶
type SearchResult struct {
PackagePath string `json:"packagePath"`
ModulePath string `json:"modulePath"`
Version string `json:"version"`
Synopsis string `json:"synopsis"`
}
SearchResult is a single result from /v1beta/search.
type Symbol ¶
type Symbol struct {
Name string `json:"name"`
Kind string `json:"kind"`
Synopsis string `json:"synopsis"`
Parent string `json:"parent,omitempty"`
}
Symbol is an exported symbol in a package.
type SymbolsOptions ¶
type SymbolsOptions struct {
Module string
GOOS string
GOARCH string
PaginationOptions
}
SymbolsOptions are options for GetSymbols.
type VersionResponse ¶
type VersionResponse struct {
Version string `json:"version"`
}
VersionResponse is a single version from /v1beta/versions/{modulePath}.