httpclient

package module
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: May 7, 2019 License: MIT Imports: 17 Imported by: 3

README

httpclient

smooth package for http client:

  • http request builder
  • http response processor
  • http transport provider
  • http client invoker

import "github.com/x-mod/httpclient"

client := httpclient.New(
    //Request Builder
    httpclient.Request(
        httpclient.NewRequestBuilder(
            httpclient.URL("https://url"),
            httpclient.Method("GET"),
        ),
    ),
    //Response Processor
    httpclient.Response(
        httpclient.NewDumpResponse(),
    ),
    //Client Transport
    httpclient.Transport(
        //...
    ),
)
//Do 
rsp, err := client.Do(context.TODO())

//or Execute http request with response processor
if err := client.Execute(context.TODO()); err != nil {
    ...
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewHTTPTransport added in v0.1.1

func NewHTTPTransport(opts ...RoundTripperOpt) http.RoundTripper

NewHTTPTransport new http transport

Types

type Body

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

Body struct

func (*Body) ContentType

func (b *Body) ContentType() string

ContentType Body Content-Type

func (*Body) Get

func (b *Body) Get() (io.Reader, error)

Get Body io.Reader

type BodyOpt

type BodyOpt func(*bodyConfig)

BodyOpt type

func Binary

func Binary(bytes []byte) BodyOpt

Binary opt

func Form

func Form(obj url.Values) BodyOpt

Form opt

func JSON

func JSON(obj map[string]interface{}) BodyOpt

JSON opt

func PB

func PB(obj proto.Message) BodyOpt

PB opt

func PBJSON

func PBJSON(obj proto.Message) BodyOpt

PBJSON opt

func Text

func Text(txt string) BodyOpt

Text opt

func XML

func XML(obj map[string]interface{}) BodyOpt

XML opt

type Client

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

Client struct

func New

func New(opts ...Opt) *Client

New client

func (*Client) Do

func (c *Client) Do(ctx context.Context) (*http.Response, error)

Do client

func (*Client) Execute

func (c *Client) Execute(ctx context.Context) error

Execute client

type CompareResponse

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

CompareResponse struct

func NewCompareResponse

func NewCompareResponse(status int, opts ...BodyOpt) *CompareResponse

NewCompareResponse new

func (*CompareResponse) Process

func (cmp *CompareResponse) Process(rsp *http.Response) error

Process of CompareResponse

type DiscardResponse

type DiscardResponse struct {
}

DiscardResponse struct

func NewDiscardResponse

func NewDiscardResponse() *DiscardResponse

NewDiscardResponse new

func (*DiscardResponse) Process

func (d *DiscardResponse) Process(rsp *http.Response) error

Process of DiscardResponse

type DumpResponse

type DumpResponse struct {
}

DumpResponse struct

func NewDumpResponse

func NewDumpResponse() *DumpResponse

NewDumpResponse new

func (*DumpResponse) Process

func (d *DumpResponse) Process(rsp *http.Response) error

Process of DumpResponse

type File

type File struct {
	Filename  string
	Fieldname string
	Data      []byte
}

func NewFile

func NewFile(field string, filename string) (*File, error)

func NewFileByBytes

func NewFileByBytes(field string, filename string, data []byte) (*File, error)

func NewFileByReader

func NewFileByReader(field string, filename string, rd io.Reader) (*File, error)

type Opt

type Opt func(*config)

Opt for client

func Connection added in v0.1.2

func Connection(client *http.Client) Opt

Connection opt

func Request

func Request(builder *RequestBuilder) Opt

Request opt

func Response

func Response(processor ResponseProcessor) Opt

Response opt

func Timeout added in v0.1.1

func Timeout(duration time.Duration) Opt

Timeout opt

func Transport

func Transport(transport http.RoundTripper) Opt

Transport opt

type ReqOpt

type ReqOpt func(*requestConfig)

ReqOpt opt

func Content

func Content(opts ...BodyOpt) ReqOpt

Content opt

func Fragment

func Fragment(name string) ReqOpt

Fragment opt

func Header(name string, value string) ReqOpt

Header opt

func Method

func Method(method string) ReqOpt

Method opt

func Query

func Query(name string, value string) ReqOpt

Query opt

func URL

func URL(url string) ReqOpt

URL opt

type RequestBuilder

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

RequestBuilder struct

func NewRequestBuilder

func NewRequestBuilder(opts ...ReqOpt) *RequestBuilder

NewRequestBuilder new

func (*RequestBuilder) Build

func (req *RequestBuilder) Build() (*http.Request, error)

Build http.Request

type ResponseProcessor

type ResponseProcessor interface {
	Process(*http.Response) error
}

ResponseProcessor interface

type RoundTripperOpt added in v0.1.1

type RoundTripperOpt func(*transportConfig)

RoundTripperOpt option

func MaxIdleConnections added in v0.1.1

func MaxIdleConnections(max int) RoundTripperOpt

MaxIdleConnections opt

func Retry added in v0.1.1

func Retry(retry int) RoundTripperOpt

Retry opt

Jump to

Keyboard shortcuts

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