requests

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2022 License: MIT Imports: 7 Imported by: 7

README

go-requests

Request builder for flexible and composable requests.

CICD CICD Go Report Card GoDoc License Latest Version codecov

Usage

var token = "secret"

var builder = requests.NewPost("example.com/test").
    Query("key", "val").
    Header("token", &token)

jsonResp, err := builder.ExecJSON()	

token = "super-secret"// update token		

jsonResp, err := builder.ExecJSON()

Todo

  • Context
  • Error validation. 200, 2xx, customer, other?
  • Retry
  • Throttling

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Doer

type Doer interface {
	Do(req *http.Request) (*http.Response, error)
}

Doer is the type that send the request, defaults to a http.DefaultClient

type ExtendedRequest

type ExtendedRequest struct {
	*Request
}

ExtendedRequest contain more advanced functionality. The purpose is to separate those from the visible methods in Request to make the package easier to use

func (*ExtendedRequest) Clone

func (req *ExtendedRequest) Clone() *Request

Clone clones the *Request to allow concurrent usage of the same base configuration

func (*ExtendedRequest) Do

func (req *ExtendedRequest) Do() (*http.Response, error)

Do execute do the request. Caller must close resp.Body in case of non-nil error

func (*ExtendedRequest) NewRequest

func (req *ExtendedRequest) NewRequest() (*http.Request, error)

NewRequest builds a *http.Request

func (*ExtendedRequest) Write

func (req *ExtendedRequest) Write(w io.Writer) error

Write writes the request into w

type JSONResponse

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

JSONResponse is holding the JSON specific response

func (*JSONResponse) Get

func (r *JSONResponse) Get(keys ...string) string

Get string from JSON body

func (*JSONResponse) GetArray

func (r *JSONResponse) GetArray(keys ...string) []*fastjson.Value

GetArray gets array from JSON body

func (*JSONResponse) GetInt

func (r *JSONResponse) GetInt(keys ...string) int

GetInt gets int from JSON body

type Request

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

Request hold the request builder data

func New

func New(url interface{}) *Request

New creates a new *Request

func NewGet

func NewGet(url interface{}) *Request

NewGet prepares a new *Request with method=GET

func NewPost

func NewPost(url interface{}) *Request

NewPost prepares a new *Request with method=POST

func (*Request) BasicAuth

func (req *Request) BasicAuth(user, password string) *Request

BasicAuth add basic authorization

func (*Request) Body

func (req *Request) Body(contentType string, value interface{}) *Request

Body set the http body

func (*Request) ContentType

func (req *Request) ContentType(contentType string) *Request

ContentType is a helper to set content type

func (*Request) ExecJSON

func (req *Request) ExecJSON() (*JSONResponse, error)

ExecJSON executes the request and return a *JSONResponse

func (*Request) Extended

func (req *Request) Extended() *ExtendedRequest

Extended returns the *ExtendedRequest

func (*Request) Header

func (req *Request) Header(key string, value interface{}) *Request

Header sets a http header

func (*Request) JSONBody

func (req *Request) JSONBody(value interface{}) *Request

JSONBody set content-type and provide a jit marshaler to construct the body when the request is prepared

func (*Request) Method

func (req *Request) Method(method interface{}) *Request

Method sets the http method

func (*Request) Query

func (req *Request) Query(key string, value interface{}) *Request

Query sets a http query

func (*Request) Url

func (req *Request) Url(url interface{}) *Request

Url sets the http url

Jump to

Keyboard shortcuts

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