api

package module
v0.9.2 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2019 License: MIT Imports: 16 Imported by: 1

README

api, another http request wrapper

===

Simplified HTTP client library :D

Quick Start


	import "github.com/liujianping/api"

	//! create api request agent

	agent := api.URL("http://a.domain.com/")
	agent := api.HTTP("host:port")
	agent := api.HTTPs("host")
	
	agent := api.Get("http://a.domain.com/")
	// agent := api.URL("http://a.domain.com/").Method(api.GET)

	agent := api.Post("http://a.domain.com/")
	agent := api.Patch("http://a.domain.com/")
	agent := api.Put("http://a.domain.com/")
	agent := api.Head("http://a.domain.com/")

	//! set api request method & URI & headers & parameters & form-data
	agent.Transport(tr)

	agent.Method(api.POST)

	agent.URI("/cgi/token")

	agent.HeadSet("key", "value")
	agent.HeadDel("key", "value")

	agent.QuerySet("key", "value")
	agent.QueryAdd("key", "value")
	agent.QueryDel("key", "value")

	agent.FormData(form)
	agent.JSONData(obj)
	agent.XMLData(obj)

	//! multipart file
	fd, _ := api.NewFile("field", "/path/to/file")
	agent.FileData(fd)

	//! chain invoke
	var result Result{}

	if err := api.HTTP("api.demo.com:8080").URI("/a/b/c").QuerySet("x", "y").JSONData(map[string]interface{}{
		"aaa": "xxx",
		"bbb": 100,
		}).Method(api.POST).JSON(&result).Error; err != nil {
			//! do something
		}


	//! do api request
	resp, err := agent.Do()

	code, []byte, err := agent.Bytes()

	code, string, err := agent.Text()

	code, err := agent.JSON(&json)

	code, err := agent.XML(&xml)	

Documentation

Index

Constants

View Source
const (
	POST   = "POST"
	GET    = "GET"
	HEAD   = "HEAD"
	PUT    = "PUT"
	DELETE = "DELETE"
	PATCH  = "PATCH"
)
View Source
const CIPHER_HEADER = "X-CIPHER-ENCODED"

Variables

This section is empty.

Functions

func JSONMarshal

func JSONMarshal(v interface{}, unescape bool) ([]byte, error)

Types

type Agent

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

func Get

func Get(aurl string) *Agent

func HTTP

func HTTP(host string) *Agent

func HTTPs

func HTTPs(host string) *Agent
func Head(aurl string) *Agent

func Patch

func Patch(aurl string) *Agent

func Post

func Post(aurl string) *Agent

func Put

func Put(aurl string) *Agent

func URL

func URL(aurl string) *Agent

func (*Agent) BasicAuthDel

func (a *Agent) BasicAuthDel() *Agent

func (*Agent) BasicAuthSet

func (a *Agent) BasicAuthSet(user, password string) *Agent

func (*Agent) Bytes

func (a *Agent) Bytes() (int, []byte, error)

func (*Agent) ClearError added in v0.9.2

func (a *Agent) ClearError() *Agent

func (*Agent) ContentType

func (a *Agent) ContentType(t string) *Agent

func (*Agent) ContextBytes

func (a *Agent) ContextBytes(ctx context.Context) (int, []byte, error)

func (*Agent) ContextJSON

func (a *Agent) ContextJSON(ctx context.Context, obj interface{}) (int, error)

func (*Agent) ContextJSONPB

func (a *Agent) ContextJSONPB(ctx context.Context, obj proto.Message) (int, error)

func (*Agent) ContextStatus

func (a *Agent) ContextStatus(ctx context.Context) (int, string, error)

func (*Agent) ContextText

func (a *Agent) ContextText(ctx context.Context) (int, string, error)

func (*Agent) ContextXML

func (a *Agent) ContextXML(ctx context.Context, obj interface{}) (int, error)

func (*Agent) CookiesAdd

func (a *Agent) CookiesAdd(cookies ...*http.Cookie) *Agent

func (*Agent) Debug

func (a *Agent) Debug(flag bool) *Agent

func (*Agent) Do

func (a *Agent) Do(ctx context.Context) (*http.Response, error)

func (*Agent) FileData

func (a *Agent) FileData(files ...*File) *Agent

func (*Agent) FormData

func (a *Agent) FormData(form map[string][]string) *Agent

func (*Agent) Fragment

func (a *Agent) Fragment(value string) *Agent

func (*Agent) GetHeadIn

func (a *Agent) GetHeadIn() http.Header

func (*Agent) GetHeadOut

func (a *Agent) GetHeadOut() http.Header

func (*Agent) HeadAdd

func (a *Agent) HeadAdd(key string, value string) *Agent

func (*Agent) HeadDel

func (a *Agent) HeadDel(key string) *Agent

func (*Agent) HeadSet

func (a *Agent) HeadSet(key string, value string) *Agent

func (*Agent) JSON

func (a *Agent) JSON(obj interface{}) (int, error)

func (*Agent) JSONData

func (a *Agent) JSONData(args ...interface{}) *Agent

func (*Agent) JSONPB

func (a *Agent) JSONPB(obj proto.Message) (int, error)

func (*Agent) Method

func (a *Agent) Method(m string) *Agent

func (*Agent) PBData

func (a *Agent) PBData(obj proto.Message) *Agent

func (*Agent) Prefix

func (a *Agent) Prefix(prefix string) *Agent

func (*Agent) QueryAdd

func (a *Agent) QueryAdd(key string, value string) *Agent

func (*Agent) QueryDel

func (a *Agent) QueryDel(key string) *Agent

func (*Agent) QueryGet

func (a *Agent) QueryGet() url.Values

func (*Agent) QuerySet

func (a *Agent) QuerySet(key string, value string) *Agent

func (*Agent) RequestProcessor

func (a *Agent) RequestProcessor(processor RequestProcessor) *Agent

func (*Agent) ResponseProcessor

func (a *Agent) ResponseProcessor(processor ResponseProcessor) *Agent

func (*Agent) SetCipher

func (a *Agent) SetCipher(cipher Cipher) *Agent

func (*Agent) SetHead

func (a *Agent) SetHead(hdr http.Header) *Agent

func (*Agent) SetHttpClient

func (a *Agent) SetHttpClient(client *http.Client)

func (*Agent) Status

func (a *Agent) Status() (int, string, error)

func (*Agent) Text

func (a *Agent) Text() (int, string, error)

func (*Agent) Transport

func (a *Agent) Transport(tr http.RoundTripper) *Agent

func (*Agent) URI

func (a *Agent) URI(uri string) *Agent

func (*Agent) XML

func (a *Agent) XML(obj interface{}) (int, error)

func (*Agent) XMLData

func (a *Agent) XMLData(obj interface{}) *Agent

type Cipher

type Cipher interface {
	Encrypt([]byte) ([]byte, error)
	Decrypt([]byte) ([]byte, error)
}

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 RequestProcessorDeferHandler added in v0.9.1

type RequestProcessorDeferHandler func()

type ResponseProcessor

type ResponseProcessor func(*http.Response) (*http.Response, error)

Jump to

Keyboard shortcuts

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