httpoet

package module
v0.0.0-...-31825af Latest Latest
Warning

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

Go to latest
Published: Mar 21, 2021 License: MIT Imports: 13 Imported by: 0

README

HttPoet

Build Status

A client tool for making http requests

Usage

Request
import (
	"github.com/khicago/httpoet"
)

func ExampleGet() {
	poet := httpoet.New("https://www.your_awsome_site.com")
	result := poet.Get("relative/path")
	body, err := result.Body()
	if err == nil {
		panic(err)
	}
	...
}

all http methods supported.

Create poet with default headers
Header
h := httpoet.H{ "key": "val" }
// or
h := httpoet.Hs{ "key": { "val", "val2"} } 
Header in action

set or update default header to the poet

h := httpoet.H{"key": "val"}
poet := httpoet.New("https://www.your_awsome_site.com").SetBaseH(h)
hs := httpoet.Hs{"key": {"v0", "v1"}}
poet.SetBaseH(hs)

temporary usage in request

hs := httpoet.Hs{"key": {"v0", "v1"}}
result := poet.Get("relative/path", httpoet.OSetHeaders(hs))
Header combination methods
var h httpoet.IHeader = httpoet.H {"k":"v"} // or httpoet.Hs{"k":{"v"}}

h.WithKV("k2", "v2")
h.WithKVAppend("k2", "v3")
h.WithH(httpoet.Hs{"k":{"v"}})
h.WithHAppend(httpoet.Hs{"k":{"v"}})
Result
func ExampleResult() {
	poet := httpoet.New("https://www.your_awsome_site.com")
	result := poet.Get("relative/path")
	v := httpoet.D{}
	if err := result.ParseJson(v); err == nil {
		panic(err)
	}
	fmt.Println(v)
}
Options

options can be used in the request, to modify the request contents before the msg has been sent.

options by defaults:

  • httpoet.OBackground()
  • httpoet.OTimeout(d time.Duration)
  • httpoet.OSetHeaders(headers IHeader)
  • httpoet.OAddHeaders(headers IHeader)
  • httpoet.OSetHeader(key string, value ...string)
  • httpoet.OAppendHeader(key string, value ...string)
  • httpoet.OAppendQuery(key string, val string)
  • httpoet.OAppendQueryH(queries IQuery)

to create a custom option, the httpoet.Option interface should be implemented

type Option func(req *RequestBuilder) (fnDefer func())
Query Tools

query could be used to create queries easily

q := httpoet.Q{"s": "some query contents"} // or httpoet.Qs{"chars": {"a","b","c"}}
pathName := "awesome"
url := q.WriteToPth("the/%s/path", pathName)
poet.Post(url, httpoet.D{"input_val":"some example"})

Documentation

Index

Constants

This section is empty.

Variables

View Source
var RequestClient = &http.Client{
	Transport: &http.Transport{},
	Timeout:   time.Second * 30,
}

Functions

This section is empty.

Types

type D

type D map[string]interface{}

type H

type H map[string]string

func (H) CountOf

func (h H) CountOf(key string) int

func (H) WithH

func (h H) WithH(headers IHeader) IHeader

func (H) WithHAppend

func (h H) WithHAppend(headers IHeader) IHeader

func (H) WithKV

func (h H) WithKV(key string, value ...string) IHeader

func (H) WithKVAppend

func (h H) WithKVAppend(key string, value ...string) IHeader

type Hs

type Hs map[string][]string

func (Hs) CountOf

func (hs Hs) CountOf(key string) int

func (Hs) WithH

func (hs Hs) WithH(headers IHeader) IHeader

func (Hs) WithHAppend

func (hs Hs) WithHAppend(headers IHeader) IHeader

func (Hs) WithKV

func (hs Hs) WithKV(key string, value ...string) IHeader

func (Hs) WithKVAppend

func (hs Hs) WithKVAppend(key string, values ...string) IHeader

type IHeader

type IHeader interface {
	WithKV(key string, value ...string) IHeader
	WithKVAppend(key string, value ...string) IHeader

	WithH(header IHeader) IHeader
	WithHAppend(header IHeader) IHeader

	CountOf(key string) int
	// contains filtered or unexported methods
}

type IQuery

type IQuery interface {
	Build() string
	Append(query IQuery) IQuery
	WriteTo(u *url.URL) error
	WriteToPth(u string, args ...interface{}) (string, error)
	// contains filtered or unexported methods
}

type IResult

type IResult interface {
	Body() ([]byte, error)
	ParseJson(resultObject interface{}) error
	ParseCustom(resultObject interface{}, method func(body []byte, target interface{}) error) error
}

func BuildNRun

func BuildNRun(req *RequestBuilder, options ...Option) IResult

type Option

type Option func(req *RequestBuilder) (fnDefer func())

func OAddCookie

func OAddCookie(cookie *http.Cookie) Option

func OAddHeaders

func OAddHeaders(headers IHeader) Option

func OAppendHeader

func OAppendHeader(key string, value ...string) Option

func OAppendQuery

func OAppendQuery(key string, val string) Option

func OAppendQueryH

func OAppendQueryH(queries IQuery) Option

func OBackground

func OBackground() Option

func OCustom

func OCustom(fn func(rb *RequestBuilder)) Option

func OHContentForm

func OHContentForm() Option

func OHContentJson

func OHContentJson() Option

func OHContentPB

func OHContentPB() Option

func OHContentString

func OHContentString() Option

func OHUserAgent

func OHUserAgent(us string) Option

func OSetHeader

func OSetHeader(key string, value ...string) Option

func OSetHeaders

func OSetHeaders(headers IHeader) Option

func OTimeout

func OTimeout(d time.Duration) Option

type Poet

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

func New

func New(host string, hosts ...string) *Poet

func (*Poet) CreateAbsoluteUrl

func (hp *Poet) CreateAbsoluteUrl(url string) string

func (*Poet) Delete

func (hp *Poet) Delete(url string, options ...Option) IResult

func (*Poet) Get

func (hp *Poet) Get(url string, options ...Option) IResult

func (*Poet) GetHost

func (hp *Poet) GetHost() string

func (*Poet) Patch

func (hp *Poet) Patch(url string, data interface{}, options ...Option) IResult

func (*Poet) Post

func (hp *Poet) Post(url string, data interface{}, options ...Option) IResult

func (*Poet) Put

func (hp *Poet) Put(url string, data interface{}, options ...Option) IResult

func (*Poet) Send

func (hp *Poet) Send(method string, url string, data interface{}, options ...Option) IResult

func (*Poet) SetBaseH

func (hp *Poet) SetBaseH(header IHeader) *Poet

func (*Poet) SpawnReq

func (hp *Poet) SpawnReq(url string) *RequestBuilder

type Q

type Q map[string]string

func (Q) Append

func (q Q) Append(query IQuery) IQuery

func (Q) Build

func (q Q) Build() string

func (Q) Set

func (q Q) Set(key string, value string) IQuery

func (Q) WriteTo

func (q Q) WriteTo(u *url.URL) error

func (Q) WriteToPth

func (q Q) WriteToPth(u string, args ...interface{}) (string, error)

type Qs

type Qs map[string][]string

func (Qs) Add

func (q Qs) Add(key string, value string) Qs

func (Qs) Append

func (q Qs) Append(query IQuery) IQuery

func (Qs) Build

func (q Qs) Build() string

func (Qs) WriteTo

func (q Qs) WriteTo(u *url.URL) error

func (Qs) WriteToPth

func (q Qs) WriteToPth(u string, args ...interface{}) (string, error)

type RequestBuilder

type RequestBuilder struct {
	Method string
	Url    string
	Header IHeader
	Query  IQuery
	Data   interface{}

	Context context.Context

	Cookies []*http.Cookie

	Error error
	// contains filtered or unexported fields
}

func NewReq

func NewReq() *RequestBuilder

func (*RequestBuilder) Build

func (rb *RequestBuilder) Build() (rbRet *RequestBuilder)

func (*RequestBuilder) Do

func (rb *RequestBuilder) Do() ([]byte, error)

func (*RequestBuilder) Request

func (rb *RequestBuilder) Request() *http.Request

func (*RequestBuilder) ResetErrorState

func (rb *RequestBuilder) ResetErrorState() *RequestBuilder

func (*RequestBuilder) XContext

func (rb *RequestBuilder) XContext(ctx context.Context) *RequestBuilder

func (*RequestBuilder) XData

func (rb *RequestBuilder) XData(d D) *RequestBuilder

func (*RequestBuilder) XDataCustom

func (rb *RequestBuilder) XDataCustom(d interface{}) *RequestBuilder

func (*RequestBuilder) XHeader

func (rb *RequestBuilder) XHeader(h IHeader) *RequestBuilder

func (*RequestBuilder) XMethod

func (rb *RequestBuilder) XMethod(m string) *RequestBuilder

func (*RequestBuilder) XQuery

func (rb *RequestBuilder) XQuery(q IQuery) *RequestBuilder

func (*RequestBuilder) XUrl

func (rb *RequestBuilder) XUrl(u string) *RequestBuilder

Jump to

Keyboard shortcuts

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