Back to godoc.org
git.sr.ht/~samwhited/sourcehut-go

Package sourcehut

v0.0.3
Latest Go to latest

The latest major version is .

Published: Dec 4, 2019 | License: BSD-2-Clause | Module: git.sr.ht/~samwhited/sourcehut-go

Overview

Package sourcehut provides access to the Sourcehut HTTP API.

Index

type Client

type Client struct {
	// contains filtered or unexported fields
}

Client is like http.Client except that it knows how to authenticate to the Sourcehut API.

func NewBaseClient

func NewBaseClient(base *http.Client) Client

NewBaseClient returns a new Sourcehut API client configured to use the provided http.Client to perform HTTP requests.

To add authentication use NewClient or provide a base client that is authenticated with the Sourcehut API.

func NewClient

func NewClient(opts ...Option) Client

NewClient returns a new Sourcehut API client configured with the provided options.

func (Client) Do

func (c Client) Do(req *http.Request, v interface{}) (*http.Response, error)

Do sends an API request and returns the API response. The response is unmarshaled into v if successful, or returned as an error value if an API error has occured.

func (Client) List

func (c Client) List(req *http.Request, d func() interface{}) *Iter

List returns an iterator that can transparently make API requests to a paginated endpoint. Each item will be decoded into the value returned from a call to d. If d is nil, a map[string]interface{} is created for each item.

No HTTP request will be issued until iteration is started by a call to Next.

type Error

type Error struct {
	Field  string
	Reason string
	// contains filtered or unexported fields
}

Error represents an individual error returned from a Sourcehut API call.

API docs: https://man.sr.ht/api-conventions.md#error-responses

func (Error) Error

func (err Error) Error() string

Error satisfies the error interface for Error.

func (Error) StatusCode

func (err Error) StatusCode() int

StatusCode returns the HTTP status code of the request that unmarshaled this error. May not be set for Errors originating from code outside this package.

type Errors

type Errors []Error

Errors is a slice of Error's that itself implements error.

func (Errors) Error

func (err Errors) Error() string

Error satisfies the error interface for Errors.

func (Errors) StatusCode

func (err Errors) StatusCode() int

StatusCode returns the HTTP status code of the request that unmarshaled this error. May not be set for Errors originating from code outside this package.

type Iter

type Iter struct {
	// contains filtered or unexported fields
}

Iter provides a convenient API for iterating over the elements returned from paginated list API calls. Successive calls to the Next method step through each item in the list, fetching pages as needed.

func (*Iter) Current

func (i *Iter) Current() interface{}

Current returns the most recent item visited by the iterator.

func (*Iter) Err

func (i *Iter) Err() error

Err returns the last error encountered by the iterator. It will only return a non-nil value if the previous call to Next returned false (but Next returning false does not guarantee that Err will return a non-nil value).

func (*Iter) Next

func (i *Iter) Next() bool

Next advances the iterator to the next item in the list and makes it available through the Current method. When the end of the list is reached it returns False.

type Option

type Option func(*Transport)

Option is used to configure a Sourcehut API client.

func RoundTripper

func RoundTripper(rt http.RoundTripper) Option

RoundTripper returns an option that configures the client to use the provided http.RoundTripper for HTTP requests. If unspecified, http.DefaultTransport is used.

func Token

func Token(t string) Option

Token returns an option that configures the client to use the provided access token when making API requests. If no token is provided, the client can only make requests that do not require authentication.

func UserAgent

func UserAgent(ua string) Option

UserAgent returns an option that configures the client to use the provided user agent when making API requests.

type Response

type Response struct {
	*http.Response `json:"-"`

	Next           string          `json:"next"`
	Results        json.RawMessage `json:"results"`
	ResultsPerPage int64           `json:"results_per_page"`
	Total          int64           `json:"total"`
}

Response is a Sourcehut API response. This wraps the standard http.Response and provides convenient access to pagination links.

API docs: https://man.sr.ht/api-conventions.md

type ShortUser

type ShortUser struct {
	CanonicalName string `json:"canonical_name"`
	Name          string `json:"name"`
}

ShortUser represents the unexpanded form of a user returned by most API endpoints.

type Transport

type Transport struct {
	// contains filtered or unexported fields
}

Transport is an http.RoundTripper wrapping a base RoundTripper and adding a Sourcehut API authorization header or user agent.

Transport is a low-level mechanism. Most code will use the NewClient method instead.

func NewTransport

func NewTransport(opts ...Option) *Transport

NewTransport returns an http.RoundTripper that is configured with the provided options.

func (*Transport) CancelRequest

func (t *Transport) CancelRequest(req *http.Request)

CancelRequest cancels an in-flight request by closing its connection.

If the underlying http.RoundTripper does not support cancelation, CancelRequest is a noop.

func (*Transport) RoundTrip

func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip authorizes and authenticates the request with an access token from Transport's Source.

type User

type User struct {
	ShortUser

	Email    string `json:"email"`
	URL      string `json:"url"`
	Location string `json:"location"`
	Bio      string `json:"bio"`
}

User represents the expanded form of a user.

Package Files

Documentation was rendered with GOOS=linux and GOARCH=amd64.

Jump to identifier

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to identifier