restclientgo

package module
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Aug 6, 2023 License: MIT Imports: 6 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
}

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 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 map[string]string) 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

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/url_values command

Jump to

Keyboard shortcuts

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