chttp

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Sep 2, 2018 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package chttp provides a minimal HTTP driver backend for communicating with CouchDB servers.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DecodeJSON

func DecodeJSON(r *http.Response, i interface{}) error

DecodeJSON unmarshals the response body into i. This method consumes and closes the response body.

func EncodeBody

func EncodeBody(i interface{}) io.ReadCloser

EncodeBody JSON encodes i to r. A call to errFunc will block until encoding has completed, then return the errur status of the encoding job. If an encoding error occurs, cancel() called.

func EncodeDocID

func EncodeDocID(docID string) string

EncodeDocID encodes a document ID according to CouchDB's path encoding rules.

In particular: - '_design/' and '_local/' prefixes are unaltered. - The rest of the docID is Query-URL encoded (despite being part of the path)

func GetRev

func GetRev(resp *http.Response) (rev string, err error)

GetRev extracts the revision from the response's Etag header

func ResponseError

func ResponseError(resp *http.Response) error

ResponseError returns an error from an *http.Response.

func ValidateAuth

func ValidateAuth(ctx context.Context, username string, client *Client) error

ValidateAuth validates that the requested username is authenticated.

Types

type Authenticator

type Authenticator interface {
	Authenticate(context.Context, *Client) error
}

Authenticator is an interface that provides authentication to a server.

type BasicAuth

type BasicAuth struct {
	Username string
	Password string
	// contains filtered or unexported fields
}

BasicAuth provides HTTP Basic Auth for a client.

func (*BasicAuth) Authenticate

func (a *BasicAuth) Authenticate(ctx context.Context, c *Client) error

Authenticate sets HTTP Basic Auth headers for the client.

func (*BasicAuth) RoundTrip

func (a *BasicAuth) RoundTrip(req *http.Request) (*http.Response, error)

RoundTrip fulfills the http.RoundTripper interface. It sets HTTP Basic Auth on outbound requests.

type Client

type Client struct {
	*http.Client
	// contains filtered or unexported fields
}

Client represents a client connection. It embeds an *http.Client

func New

func New(ctx context.Context, dsn string) (*Client, error)

New returns a connection to a remote CouchDB server. If credentials are included in the URL, CookieAuth is attempted first, with BasicAuth used as a fallback. If both fail, an error is returned. If you wish to use some other authentication mechanism, do not specify credentials in the URL, and instead call the Auth() method later.

func (*Client) Auth

func (c *Client) Auth(ctx context.Context, a Authenticator) error

Auth authenticates using the provided Authenticator.

func (*Client) DSN

func (c *Client) DSN() string

DSN returns the unparsed DSN used to connect.

func (*Client) DoError

func (c *Client) DoError(ctx context.Context, method, path string, opts *Options) (*http.Response, error)

DoError is the same as DoReq(), followed by checking the response error. This method is meant for cases where the only information you need from the response is the status code. It unconditionally closes the response body.

func (*Client) DoJSON

func (c *Client) DoJSON(ctx context.Context, method, path string, opts *Options, i interface{}) (*http.Response, error)

DoJSON combines DoReq() and, ResponseError(), and (*Response).DecodeJSON(), and closes the response body.

func (*Client) DoReq

func (c *Client) DoReq(ctx context.Context, method, path string, opts *Options) (*http.Response, error)

DoReq does an HTTP request. An error is returned only if there was an error processing the request. In particular, an error status code, such as 400 or 500, does _not_ cause an error to be returned.

func (*Client) NewRequest

func (c *Client) NewRequest(ctx context.Context, method, path string, body io.Reader) (*http.Request, error)

NewRequest returns a new *http.Request to the CouchDB server, and the specified path. The host, schema, etc, of the specified path are ignored.

type CookieAuth

type CookieAuth struct {
	Username string `json:"name"`
	Password string `json:"password"`
	// contains filtered or unexported fields
}

CookieAuth provides CouchDB Cookie auth services as described at http://docs.couchdb.org/en/2.0.0/api/server/authn.html#cookie-authentication

CookieAuth stores authentication state after use, so should not be re-used.

func (*CookieAuth) Authenticate

func (a *CookieAuth) Authenticate(ctx context.Context, c *Client) error

Authenticate initiates a session with the CouchDB server.

func (*CookieAuth) Cookie added in v1.0.7

func (a *CookieAuth) Cookie() (*http.Cookie, bool)

Cookie returns the current session cookie and true, if found, or nil and false if not.

type HTTPError

type HTTPError struct {
	Code   int
	Reason string `json:"reason"`
}

HTTPError is an error that represents an HTTP transport error.

func (*HTTPError) Error

func (e *HTTPError) Error() string

func (*HTTPError) StatusCode

func (e *HTTPError) StatusCode() int

StatusCode returns the embedded status code.

type Options

type Options struct {
	// Accept sets the request's Accept header. Defaults to "application/json".
	// To specify any, use "*/*".
	Accept string

	// ContentType sets the requests's Content-Type header. Defaults to "application/json".
	ContentType string

	// Body sets the body of the request.
	Body io.ReadCloser

	// JSON is an arbitrary data type which is marshaled to the request's body.
	// It an error to set both Body and JSON on the same request. When this is
	// set, ContentType is unconditionally set to 'application/json'. Note that
	// for large JSON payloads, it can be beneficial to do your own JSON stream
	// encoding, so that the request can be live on the wire during JSON
	// encoding.
	JSON interface{}

	// FullCommit adds the X-Couch-Full-Commit: true header to requests
	FullCommit bool

	// IfNoneMatch adds the If-None-Match header.
	IfNoneMatch string

	// Destination is the target ID for COPY
	Destination string
}

Options are optional parameters which may be sent with a request.

type Response

type Response struct {
	*http.Response

	// ContentType is the base content type, parsed from the response headers.
	ContentType string
}

Response represents a response from a CouchDB server.

Jump to

Keyboard shortcuts

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