restclientgo

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2024 License: MIT Imports: 7 Imported by: 51

README

RESTclientGo

Build Status GoDoc Go Report Card GitHub release

REST client for Go focusing on Request and Response data modeling.

Rest methods

restclientgo offers the following methods

  • GET
  • POST
  • PUT
  • DELETE
  • PATCH

Modeling

Request

Define your request model and attach restclientgo methods to satisfy the Request interface.

type MyRequest struct {
    // Add your request fields here
    // ID string `json:"id"`
    // ...
}

func (r *MyRequest) Path() (string, error) {
    // Return the path of the request including the query string if any.
}

func (r *MyRequest) Encode() (io.Reader, error) {
    // Return the request body as string
}

func (r *MyRequest) ContentType() string {
    // Return the content type of the request
}
Response

Define your response model and attach restclientgo methods to satisfy the Response interface.

type MyResponse struct {
    // Add your response fields here
    // ID string `json:"id"`
    // ...
}

func (r *MyResponse) Decode(body io.Reader) error {
    // Decode the response body into the response model
}

func (r *MyResponse) SetBody(body io.Reader) error {
    // Set the response body if needed
}

func (r *MyResponse) SetStatusCode(code int) error {
    // Handler to set the HTTP status code of the response
}

func (r *MyResponse) AcceptContentType() string {
    // Return the accepted content type of the response
}

// Optional. Implement this method if you want to stream the response body.
func (r *MyResponse) StreamCallback() StreamCallback {
    // Return the stream callback if any.
}

Usage

Please referr to the examples folder for usage examples.

Documentation

Index

Constants

View Source
const (
	ErrNoContentType  = Error("no content-type found in response")
	ErrRequestPath    = Error("invalid request path")
	ErrRequestEncode  = Error("invalid request encode")
	ErrHTTPRequest    = Error("invalid http request")
	ErrResponseDecode = Error("invalid response decode")
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

type Error string

func (Error) Error

func (e Error) Error() string

type Headers added in v1.0.5

type Headers map[string][]string

type Request

type Request interface {
	// Path returns the path of the request including the query string if any.
	Path() (string, error)
	// Encode return the encoded representation of the request.
	Encode() (io.Reader, error)
	ContentType() string
}

type Response

type Response interface {
	// Decode decodes the response body into the given interface if the
	// response matches the AcceptContentType.
	Decode(body io.Reader) error
	// SetBody sets the response raw body if the response can't be decoded.
	SetBody(body io.Reader) error
	// AcceptContentType returns the content type that the response should be decoded to.
	AcceptContentType() string
	// SetStatusCode sets the HTTP response status code.
	SetStatusCode(code int) error
	// SetHeaders sets the HTTP response headers.
	SetHeaders(headers Headers) error
}

type RestClient

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

func New

func New(endpoint string) *RestClient

New creates a new RestClient.

func (*RestClient) Delete

func (r *RestClient) Delete(ctx context.Context, request Request, response Response) error

Delete performs a DELETE request.

func (*RestClient) Endpoint added in v1.0.3

func (r *RestClient) Endpoint() string

func (*RestClient) Get

func (r *RestClient) Get(ctx context.Context, request Request, response Response) error

Get performs a GET request.

func (*RestClient) Patch

func (r *RestClient) Patch(ctx context.Context, request Request, response Response) error

Patch performs a PATCH request.

func (*RestClient) Post

func (r *RestClient) Post(ctx context.Context, request Request, response Response) error

Post performs a POST request.

func (*RestClient) Put

func (r *RestClient) Put(ctx context.Context, request Request, response Response) error

Put performs a PUT request.

func (*RestClient) SetEndpoint added in v1.0.3

func (r *RestClient) SetEndpoint(endpoint string)

func (*RestClient) SetHTTPClient

func (r *RestClient) SetHTTPClient(client *http.Client)

SetHTTPClient overrides the default http client.

func (*RestClient) SetRequestModifier

func (r *RestClient) SetRequestModifier(requestModifier func(*http.Request) *http.Request)

SetRequestModifier adds a function that will modify each request

func (*RestClient) WithDecodeOnError added in v1.1.0

func (r *RestClient) WithDecodeOnError(decodeOnError bool) *RestClient

WithDecodeOnError forces the response to be decoded even if the status code is >= 400.

func (*RestClient) WithHTTPClient added in v1.1.0

func (r *RestClient) WithHTTPClient(client *http.Client) *RestClient

WithHTTPClient overrides the default http client.

func (*RestClient) WithRequestModifier added in v1.1.0

func (r *RestClient) WithRequestModifier(requestModifier func(*http.Request) *http.Request) *RestClient

WithRequestModifier adds a function that will modify each request

type StreamCallback added in v1.1.1

type StreamCallback func([]byte) error

type Streamable added in v1.2.0

type Streamable interface {
	// StreamCallback get the stream callback if any.
	StreamCallback() StreamCallback
}

type URLValues

type URLValues url.Values

func NewURLValues

func NewURLValues() *URLValues

NewURLValues creates a new URLValues.

func (*URLValues) Add

func (p *URLValues) Add(key string, value *string)

Add adds a string value to the URLValues.

func (*URLValues) AddBool

func (p *URLValues) AddBool(key string, value *bool)

AddBool adds a bool value to the URLValues.

func (*URLValues) AddBoolAsInt

func (p *URLValues) AddBoolAsInt(key string, value *bool)

AddBoolAsInt adds a bool value to the URLValues as an int (0=false, 1=true).

func (*URLValues) AddFloat

func (p *URLValues) AddFloat(key string, value *float64)

AddFloat adds a float value to the URLValues.

func (*URLValues) AddInt

func (p *URLValues) AddInt(key string, value *int)

AddInt adds an int value to the URLValues.

func (*URLValues) Del

func (p *URLValues) Del(key string)

Del deletes the URLValues associated with key.

func (*URLValues) Encode

func (p *URLValues) Encode() string

Encode encodes the URLValues into "URL encoded" form ("bar=baz&foo=quux").

Directories

Path Synopsis
examples
cmd/context command
cmd/delete command
cmd/get command
cmd/patch command
cmd/post command
cmd/put command
cmd/stream command
cmd/url_values command

Jump to

Keyboard shortcuts

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