Documentation
¶
Overview ¶
Package operations provides support for invoking various operations on web APIs.
Index ¶
- func RunCrawl[ScanT, EndpointT any](ctx context.Context, crawler *Crawler[ScanT, EndpointT], ...) error
- type Auth
- type CrawlHandler
- type Crawler
- type Encoding
- type Endpoint
- type Error
- type FS
- type Fetcher
- type Marshal
- type Option
- func WithAuth(a Auth) Option
- func WithHTTPClient(client *http.Client) Option
- func WithLogger(logger *slog.Logger) Option
- func WithMarshal(marshal Marshal, e Encoding) Option
- func WithRateController(rc *ratecontrol.Controller, statusCodes ...int) Option
- func WithSigner(signer Signer) Option
- func WithSuccessCodes(codes ...int) Option
- func WithUnmarshal(u Unmarshal, e Encoding) Option
- type Paginator
- type PutEndpoint
- func (ep *PutEndpoint[RequestT, ResponseT]) IssueRequest(ctx context.Context, req *http.Request, data RequestT) (ResponseT, []byte, Encoding, *http.Response, error)
- func (ep *PutEndpoint[RequestT, ResponseT]) Post(ctx context.Context, url string, data RequestT) (ResponseT, []byte, Encoding, error)
- func (ep *PutEndpoint[RequestT, ResponseT]) Put(ctx context.Context, url string, data RequestT) (ResponseT, []byte, Encoding, error)
- type Response
- type Scanner
- type Signer
- type Unmarshal
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Auth ¶
type Auth interface {
// WithAuthorization adds an authorization header or other required
// authorization information to the provided http.Request.
WithAuthorization(context.Context, *http.Request) error
}
Auth represents an authorization mechanism.
type CrawlHandler ¶
type Crawler ¶
Crawler is a generic crawler that can be used to iterate over a paginated API endpoint (using a Scanner) that enumerates objects that can be downloaded using a Fetcher. The Fetcher is responsible for decoding the results of each response from the paginated API and downloading the objects.
func NewCrawler ¶
func NewCrawler[ScanT, EndpointT any](scanner *Scanner[ScanT], fetcher Fetcher[ScanT, EndpointT]) *Crawler[ScanT, EndpointT]
NewCrawler creates a new crawler that scans the API using the provided Scanner with the result of each scan being passed to the Fetcher to download each item returned by the scan.
type Encoding ¶
type Encoding int
Encoding represents the encoding scheme used for the response body.
func (Encoding) ContentType ¶
ContentType returns the content type associated with this encoding.
type Endpoint ¶
type Endpoint[T any] struct { // contains filtered or unexported fields }
Endpoint represents an API endpoint that can be invoked using GET. The response body is unmarshaled into the specified type T. Use PutEndpoint for operations where both the request and response bodies can be typed.
func NewEndpoint ¶
NewEndpoint returns a new endpoint for the specified type.
func (*Endpoint[T]) IssueRequest ¶
func (ep *Endpoint[T]) IssueRequest(ctx context.Context, req *http.Request) (T, []byte, Encoding, *http.Response, error)
IssueRequest invokes an arbitrary request on this endpoint using the supplied http.Request. The Body in the http.Response has already been read and its contents returned as the second return value.
type FS ¶
FS defines a filesystem interface to be broadly used by webapi packages and clients. It is defined in operations for convenience.
type Fetcher ¶
type Fetcher[ScanT, EndpointT any] interface { Fetch(context.Context, ScanT, chan<- []content.Object[EndpointT, Response]) error }
Fetcher is a generic interface for fetching objects from an API endpoint as part of a crawl. The Fetcher extracts/decodes items to be fetched from the result of a Scan and then downloads each item.
type Option ¶
type Option func(o *options)
Option represents an option that can be used when creating new Endpoints and Streams.
func WithHTTPClient ¶
WithHTTPClient specifies the http.Client to use for making requests. If not specified, http.DefaultClient is used.
func WithLogger ¶
WithLogger specifies the logger to use for logging request and response information. If not specified, no logging is performed.
func WithMarshal ¶
WithMarshal specifies a custom marshaling function to use for encoding request bodies. The default is json.Marshal.
func WithRateController ¶
func WithRateController(rc *ratecontrol.Controller, statusCodes ...int) Option
WithRateController sets the rate controller to use to enforce rate control and backoff.
func WithSigner ¶
WithSigner specifies a Signer function to use for signing requests.
func WithSuccessCodes ¶
WithSuccessCodes specifies the HTTP status codes that should be considered successful responses. If not specified, only http.StatusOK (200) is considered a successful response for Get operations and http.StatusOK (200) http.StatusAccepted or for Put/Post operations.
func WithUnmarshal ¶
WithUnmarshal specifies a custom unmarshaling function to use for decoding response bodies. The default is json.Unmarshal.
type Paginator ¶
type Paginator[T any] interface { // Next is called with the returned type and response for that operation. // The first URL to use in a scan is generated by calling Next with an empty // payload and nil *http.Response Next(ctx context.Context, t T, r *http.Response) (req *http.Request, done bool, err error) }
Paginator represents the ability to generate the next request (URL with optional body) given the response from the previous request. Paginators are typically used with Scanners to iterate over a paginated API.
type PutEndpoint ¶
type PutEndpoint[RequestT, ResponseT any] struct { // contains filtered or unexported fields }
PutEndpoint represents an API endpoint that supports PUT requests with a request body of type RequestT and a response body of type ResponseT.
func NewPutEndpoint ¶
func NewPutEndpoint[RequestT, ResponseT any](opts ...Option) *PutEndpoint[RequestT, ResponseT]
func (*PutEndpoint[RequestT, ResponseT]) IssueRequest ¶
func (ep *PutEndpoint[RequestT, ResponseT]) IssueRequest(ctx context.Context, req *http.Request, data RequestT) (ResponseT, []byte, Encoding, *http.Response, error)
IssueRequest invokes an arbitrary request on this endpoint using the supplied http.Request except that the Request body is overridden with encoding of the supplied data.
type Response ¶
type Response struct {
// The raw bytes of the response.
Bytes []byte
// The encoding used for Bytes.
Encoding Encoding
// When the response was received.
When time.Time
// Fields copied from the http.Response.
Headers http.Header
Trailers http.Header
ContentLength int64
StatusCode int
ProtoMajor, ProtoMinir int
TransferEncoding []string
// Any error encountered during the operation.
Error error
// Checkpoint is an opaque value that can be used to resume an
// operation at a later time. This is used generally used by implementations
// of Crawler/Fetcher.
Checkpoint []byte
// Current and Total, if non-zero, provide an indication of progress.
Current int64
Total int64
}
Response contains metadata for the result of an operation and is used for the response field of the content.Object returned by the Fetcher.
func (*Response) FromHTTPResponse ¶
type Scanner ¶
type Scanner[T any] struct { // contains filtered or unexported fields }
Scanner provides the ability to iterate over a paginated API a page at a time.
func NewScanner ¶
NewScanner creates a new Scanner using the supplied paginator. The options are used to create the underlying Endpoint.
func (*Scanner[T]) HTTPResponse ¶
HTTPResponse returns the response for the current page.
Directories
¶
| Path | Synopsis |
|---|---|
|
Package apicrawlcmd provides support for building command line tools that implement API crawls.
|
Package apicrawlcmd provides support for building command line tools that implement API crawls. |
|
Package apitokens provides types and functions for managing API tokens and is built on top of the cloudeng.io/cmdutil/keys package and its InmemoryKeyStore.
|
Package apitokens provides types and functions for managing API tokens and is built on top of the cloudeng.io/cmdutil/keys package and its InmemoryKeyStore. |