httputil

package
v0.0.0-...-c39e8e5 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2022 License: AGPL-3.0 Imports: 12 Imported by: 14

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddErrCode

func AddErrCode(statusCode int, err error) error

AddErrCode adds error code to an error given the http status.

func ErrorStatusCode

func ErrorStatusCode(err error) int

ErrorStatusCode returns the status code is it is an HTTP error.

func ExtendServer

func ExtendServer(s string) string

ExtendServer extends the server host string with an https:// prefix if it is not a localhost, or an http:// prefix if it is localhost. It only extends the server when the server string can be extends to be a valid URL.

func GetCode

func GetCode(c *http.Client, url string) (int, error)

GetCode gets the reply code of the given URL.

func GetString

func GetString(c *http.Client, url string) (string, error)

GetString gets the string response of the given URL using the given client. It returns an error if the reply code is not 200.

func RespError

func RespError(resp *http.Response) error

RespError returns the error from an HTTP response.

func SetAuthToken

func SetAuthToken(h http.Header, tok string)

SetAuthToken sets authorization header token.

Types

type Client

type Client struct {
	Server *url.URL

	// TokenSource is an optional token source to proivde bearer token.
	TokenSource TokenSource

	UserAgent string // Optional User-Agent for each request.
	Accept    string // Optional Accept header.

	Transport http.RoundTripper
}

Client performs client that calls to a remote server with an optional token.

func NewClient

func NewClient(s string) (*Client, error)

NewClient creates a new client.

func NewClientMust

func NewClientMust(s string) *Client

NewClientMust creates a client and panics on error.

func NewTokenClient

func NewTokenClient(s, tok string) (*Client, error)

NewTokenClient creates a new client with a Bearer token.

func NewTokenClientMust

func NewTokenClientMust(s, tok string) *Client

NewTokenClientMust creates a client with auth token and panics on error.

func NewUnixClient

func NewUnixClient(sockAddr string) *Client

NewUnixClient creates a new client that always goes to a particular unix domain socket.

func (*Client) Call

func (c *Client) Call(p string, req, resp interface{}) error

Call performs a CallContext with context.TODO().

func (*Client) CallContext

func (c *Client) CallContext(
	ctx context.Context, p string, req, resp interface{},
) error

CallContext performs a call with the request as a marshalled JSON object, and the response unmarshalled as a JSON object.

func (*Client) Delete

func (c *Client) Delete(p string) error

Delete sends a delete message to the particular path.

func (*Client) Get

func (c *Client) Get(p string) (*http.Response, error)

Get gets a response from a route on the server.

func (*Client) GetBytes

func (c *Client) GetBytes(p string) ([]byte, error)

GetBytes gets the byte array from a route on the server.

func (*Client) GetCode

func (c *Client) GetCode(p string) (int, error)

GetCode gets a response from a route and returns the status code.

func (*Client) GetInto

func (c *Client) GetInto(p string, w io.Writer) (int64, error)

GetInto gets the specified path and writes everything from the body to the given writer.

func (*Client) GetString

func (c *Client) GetString(p string) (string, error)

GetString gets the string response from a route on the server.

func (*Client) JSONCall

func (c *Client) JSONCall(p string, req, resp interface{}) error

JSONCall is an alias to Call.

func (*Client) JSONGet

func (c *Client) JSONGet(p string, resp interface{}) error

JSONGet gets the content of a path and decodes the response into resp as JSON.

func (*Client) JSONPost

func (c *Client) JSONPost(p string, req interface{}, w io.Writer) error

JSONPost posts a JSON object as the request body and writes the body into the given writer.

func (*Client) JSONPut

func (c *Client) JSONPut(p string, v interface{}) error

JSONPut puts an object in JSON encoding.

func (*Client) Poke

func (c *Client) Poke(p string) error

Poke posts a signal to the given route on the server.

func (*Client) Post

func (c *Client) Post(p string, r io.Reader, w io.Writer) error

Post posts with request body from r, and copies the response body to w.

func (*Client) Put

func (c *Client) Put(p string, r io.Reader) error

Put puts a stream to a path on the server.

func (*Client) PutBytes

func (c *Client) PutBytes(p string, bs []byte) error

PutBytes puts bytes to a path on the server.

func (*Client) PutN

func (c *Client) PutN(p string, r io.Reader, n int64) error

PutN puts a stream to a path on the server with content length set to n.

type StaticToken

type StaticToken struct {
	T string
}

StaticToken is a token source that provides a fixed, static token.

func NewStaticToken

func NewStaticToken(tok string) *StaticToken

NewStaticToken creates a new static token provider.

func (*StaticToken) Token

func (s *StaticToken) Token(
	ctx context.Context, tr http.RoundTripper,
) (string, error)

Token always returns the fixed, static token T.

type TokenSource

type TokenSource interface {
	Token(ctx context.Context, tr http.RoundTripper) (string, error)
}

TokenSource is an interface that can provides a bearer token for authentication.

Jump to

Keyboard shortcuts

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