request

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

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

Go to latest
Published: May 28, 2020 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type ClientRequest

type ClientRequest struct {
	Encoder  Encoder
	Method   string
	Path     string
	PathArgs []interface{}
	Body     io.Reader
	// contains filtered or unexported fields
}

ClientRequest make request by params method query

func NewGet

func NewGet(ctx context.Context, opts ...Option) ClientRequest

NewGet create new get request

func NewPost

func NewPost(ctx context.Context, opts ...Option) ClientRequest

NewPost create new post request

func NewRequest

func NewRequest(ctx context.Context, opts ...Option) ClientRequest

NewRequest create new request

func (ClientRequest) HTTP

func (r ClientRequest) HTTP() (httpRequest *http.Request, err error)

HTTP create http Request

func (ClientRequest) Header

func (r ClientRequest) Header(value ...RValue) ClientRequest

Header add values for the header

func (ClientRequest) Query

func (r ClientRequest) Query(value ...RValue) ClientRequest

Query add values for the qu

Example
ctx := context.TODO()
req, err := NewRequest(ctx, WithEncoder(clientEncoder)).
	Query(
		StringValue("q", "search"),
		Int64Value("id", 2),
		TimeValue("ts", time.Now(), time.RFC3339Nano),
	).
	HTTP()
if err != nil {
	log.Fatal(err)
}
res, err := http.DefaultClient.Do(req)
if err != nil {
	log.Fatal(err)
}
defer res.Body.Close()
fmt.Println(res.Body)
Output:

func (ClientRequest) SetBasicAuth

func (r ClientRequest) SetBasicAuth(username, password string) ClientRequest

SetBasicAuth set username and password basic auth

func (ClientRequest) SetBody

func (r ClientRequest) SetBody(data interface{}) ClientRequest

SetBody encode body and add to request

func (ClientRequest) URI

func (r ClientRequest) URI(path string, a ...interface{}) ClientRequest

URI set url and args it

type Encoder

type Encoder func(v interface{}) (io.Reader, error)

Encoder for the body

type Middleware

type Middleware func(ctx context.Context, cr *ClientRequest,
	n func(context.Context) (*http.Request, error)) (*http.Request, error)

Middleware handle middleware

type Option

type Option func(*ClientRequest)

Option configure client request

func WithEncoder

func WithEncoder(encoder Encoder) Option

WithEncoder set encoder request

func WithHeader

func WithHeader(values ...RValue) Option

WithHeader set header

func WithMethod

func WithMethod(method string) Option

WithMethod set method by default GET

func WithMiddleware

func WithMiddleware(mw ...Middleware) Option

WithMiddleware set middleware request

type RValue

type RValue func(Values)

RValue configure values

func Int64Value

func Int64Value(key string, val int64) RValue

Int64Value add int64

func StringValue

func StringValue(key string, val string) RValue

StringValue add string values

func TimeValue

func TimeValue(key string, val time.Time, format string) RValue

TimeValue add time by format

type Values

type Values interface {
	Add(key, val string)
}

Values interface for the add values

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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