sendy

package module
v0.0.0-...-6561042 Latest Latest
Warning

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

Go to latest
Published: Aug 15, 2023 License: MIT Imports: 16 Imported by: 0

README

sendy

godoc License Go Report Card

Go HTTP Client that prevents you from having to write boilerplate code setting up a native *http.Client, creating a request, and parsing the response. This package uses the builder pattern for constructing requests and parsing responses.

Quick Start

package main

import (
    "fmt"

    "github.com/hypnobrando/sendy"
)

type (
    User struct {
        ID   int    `json:"id"`
        Name string `json:"name"`
    }
)

func main() {
    var user User

    err := sendy.
        Get("https://myapi.com/users/1").
        SendIt().
        JSON(&user).
        Error()

    if err != nil {
        panic(err)
    }

    fmt.Println(user)
}

Installation / Usage

To install sendy, use go get:

go get github.com/hypnobrando/sendy

Import the hypnobrando/sendy package into your code:

import "github.com/hypnobrando/sendy"

func main() {
    httpClient := sendy.NewClient()
}

Staying Up to Date

To update sendy to the latest version, use go get -u github.com/hypnobrando/sendy.

Contributing

Please feel free to submit issues, fork the repository and send pull requests!

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

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

Client is a struct used for making HTTP requests to a a single host.

func NewClient

func NewClient() *Client

NewClient instantiates a client for making HTTP requests to a single host.

func (*Client) BasicAuth

func (c *Client) BasicAuth(username, password string) *Client

BasicAuth sets the HTTP basic auth username and password.

func (*Client) Connect

func (c *Client) Connect() *Request

Connect instantiates an HTTP Connect request builder.

func (*Client) Delete

func (c *Client) Delete() *Request

Delete instantiates an HTTP Delete request builder.

func (*Client) DumpRequests

func (c *Client) DumpRequests() *Client

DumpRequests prints out the entire contents of every request to stdout.

func (*Client) Get

func (c *Client) Get() *Request

Get instantiates an HTTP GET request builder.

func (*Client) Head

func (c *Client) Head() *Request

Head instantiates an HTTP Head request builder.

func (*Client) Header

func (c *Client) Header(key, value string) *Client

Header appends an HTTP request header.

func (*Client) Hook

func (c *Client) Hook(hook Hook) *Client

Hook takes in a Hook interface that gets called right before an HTTP request is made.

func (*Client) Host

func (c *Client) Host(host string) *Client

Host overrides the host of the Client.

func (*Client) Options

func (c *Client) Options() *Request

Options instantiates an HTTP Options request builder.

func (*Client) Patch

func (c *Client) Patch() *Request

Patch instantiates an HTTP Patch request builder.

func (*Client) Post

func (c *Client) Post() *Request

Post instantiates an HTTP Post request builder.

func (*Client) Put

func (c *Client) Put() *Request

Put instantiates an HTTP Put request builder.

func (*Client) Timeout

func (c *Client) Timeout(timeout time.Duration) *Client

Timeout sets the client request timeout.

func (*Client) Trace

func (c *Client) Trace() *Request

Trace instantiates an HTTP Trace request builder.

func (*Client) Transport

func (c *Client) Transport(transport http.RoundTripper) *Client

Transport sets the HTTP transport on the client. This incldues proxying, retry policy, etc.

func (*Client) UserAgent

func (c *Client) UserAgent(userAgent string) *Client

UserAgent sets the User-Agent HTTP Header.

func (*Client) WithRetriesAndTimeout

func (c *Client) WithRetriesAndTimeout(maxRetries int, timeout time.Duration) *Client

WithRetriesAndTimeout sets the HTTP transport on the client.

type Error

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

Error is a struct that implements the native error interface and is instantiated after making and handling an HTTP request.

func (*Error) Error

func (err *Error) Error() string

Error implements the native error interface.

type FormEntry

type FormEntry struct {
	Key   string
	Value io.Reader
}
type Header struct {
	Key, Value string
}

Header contains a single HTTP header.

type Headers

type Headers []Header

Headers is a slice of Header structs.

type Hook

type Hook interface {
	Request(*http.Request)
}

Hook is used as a hook before a Client makes a request.

type Hooks

type Hooks []Hook

Hooks is a slice of Hook interfaces.

type Param

type Param struct {
	Key, Value string
}

Param contains a single URL query param.

type Params

type Params []Param

Params is a slice of Param structs.

type Request

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

Request is an HTTP builder struct for making an HTTP request.

func Connect

func Connect(host string) *Request

Connect instantiates an HTTP Connect request builder.

func Delete

func Delete(host string) *Request

Delete instantiates an HTTP Delete request builder.

func Get

func Get(host string) *Request

Get instantiates an HTTP GET request builder.

func Head(host string) *Request

Head instantiates an HTTP Head request builder.

func Options

func Options(host string) *Request

Options instantiates an HTTP Options request builder.

func Patch

func Patch(host string) *Request

Patch instantiates an HTTP Patch request builder.

func Post

func Post(host string) *Request

Post instantiates an HTTP Post request builder.

func Put

func Put(host string) *Request

Put instantiates an HTTP Put request builder.

func Trace

func Trace(host string) *Request

Trace instantiates an HTTP Trace request builder.

func (*Request) BasicAuth

func (request *Request) BasicAuth(username, password string) *Request

BasicAuth sets the HTTP basic auth username and password.

func (*Request) Dump

func (r *Request) Dump() *Request

Dump prints out the entire contents of the request when the request is made.

func (*Request) Header

func (request *Request) Header(key, value string) *Request

Header appends an HTTP request header.

func (*Request) Hook

func (request *Request) Hook(hook Hook) *Request

Hook takes in a Hook interface that gets called right before an HTTP request is made.

func (*Request) JSON

func (request *Request) JSON(object interface{}) *Request

JSON serializes the input object into the body of the request in the form of JSON.

func (*Request) Method

func (request *Request) Method(method string) *Request

Method overrides the HTTP request method.

func (*Request) MultiPartForm

func (request *Request) MultiPartForm(values []FormEntry) *Request

MultiPartForm serializes the input values into a multi-part form request.

func (*Request) Param

func (request *Request) Param(key, value string) *Request

Param appends a URL query param.

func (*Request) Path

func (request *Request) Path(path string) *Request

Path overrides the HTTP request path.

func (*Request) RawBody

func (request *Request) RawBody(raw []byte) *Request

RawBody sets the raw bytes as the body of the request.

func (*Request) SendIt

func (request *Request) SendIt() *Response

SendIt commits the HTTP request builder and captures the response as a Response struct.

func (*Request) URLEncodedParam

func (request *Request) URLEncodedParam(key, value string) *Request

func (*Request) UserAgent

func (request *Request) UserAgent(userAgent string) *Request

UserAgent sets the User-Agent request header.

func (*Request) XML

func (request *Request) XML(object interface{}) *Request

XML serializes the input object into the body of the request in the form of XML.

type Response

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

Response contains the response after making an HTTP request and can be used for parsing / inspecting the response.

func (*Response) Error

func (response *Response) Error() error

Error returns an Error struct. This Error contains an error's that might have occurred during the build process, during the lifetime of the request, or even during the parsing of the response. Non-2XX status codes are also returned as an Error.

func (*Response) Headers

func (response *Response) Headers() http.Header

Headers returns the response headers.

func (*Response) JSON

func (response *Response) JSON(object interface{}) *Response

JSON parses the response body as JSON and deserializes it into the input object.

func (*Response) Raw

func (response *Response) Raw() ([]byte, error)

Raw returns the raw body and any errors associated with the request.

func (*Response) StatusCode

func (response *Response) StatusCode() int

StatusCode returns the HTTP status code of the response. If there was an error making the request and no response was read then the status code returned is -1.

func (*Response) XML

func (response *Response) XML(object interface{}) *Response

XML parses the response body as XML and deserializes it into the input object.

Jump to

Keyboard shortcuts

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