esrest

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

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

Go to latest
Published: Jan 25, 2020 License: MIT Imports: 11 Imported by: 1

README

esrest

Easy, elegant, fluent HTTP client API for Go

Travis branch Codecov branch Go Report Card GoDoc

Features

  • Support basic HTTP GET/POST/PUT/DELETE/HEAD in a fluent style
  • Only use Body fluent function to send payload(JSON/string/slice/pointer/map)
  • Basic Authentication
  • Request timeout
  • Debug with customized Logger
  • Receive unmarshal JSON
  • Multipart request
  • Context
  • application/x-www-form-urlencoded
  • todo

Installation

$ go get -u github.com/easonlin404/esrest

Usage

GET/POST/PUT/DELETE


res, err := esrest.New().Get("http://httpbin.org/get").Do()

Add header (Default ContentType is "application/json")

res, err := esrest.New().
		    Get("http://httpbin.org/get").
		    Header("MyHader", "headvalue").
		    Do()

Sending JSON payload use Body chain method same as other:

//JSON struct
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(struct {
                 		Message string `json:"message"`
                 	}{"ok"}).
		    Do()
//pointer to JSON struct
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(&struct {
                 		Message string `json:"message"`
                 	}{"ok"}).
		    Do()		    
//slice
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body([]byte(`{"message":"ok"}`)).
		    Do()
		    
//string
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(string(`{"message":"ok"}`)).
		    Do()
		    
//map
m := map[string]interface{}{
		"message": "ok",
	}
	
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(m).
		    Do()

Add Query parameter:

res, err := esrest.New().
		    Get("http://httpbin.org/get").
		    Query("Param1", "value").
		    Do()

Receive unmarshal JSON:

json := &struct {
		Message string `json:"message"`
	}{}
res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    DoJson(json)

Basic Authentication:

res, err := esrest.New().
		    BasicAuth("user", "password").
		    Get("http://httpbin.org/get").
		    Do()

Debug:

Print http request and response debug payload at stdout, and you also can use your logger by using Logger chain

mylogger:=log.New(os.Stdout, "", log.LstdFlags)

res, err := esrest.New().
		    Debug(true).
		    Logger(mylogger).  //optional
		    Get("http://httpbin.org/get").
		    Do()

Documentation

Overview

Package esrest implements functions to wrapper around the HTTP client API.

Index

Constants

View Source
const DefaultContentType = "application/json"

DefaultContentType is default http Content-Type="application/json" header.

Variables

This section is empty.

Functions

This section is empty.

Types

type Builder

type Builder struct {
	Url       string
	Method    string
	Path      string
	Headers   map[string]string
	Querys    map[string]string
	DebugMode bool
	// contains filtered or unexported fields
}

Builder is a object that help to build fluent style API.

func New

func New() *Builder

New returns an new Builder object.

func (*Builder) BasicAuth

func (b *Builder) BasicAuth(username, password string) *Builder

func (*Builder) Body

func (b *Builder) Body(v interface{}) *Builder

Body is used to set the HTTP request body to send payload(JSON/string/slice/pointer) when "Do()" or "DoJson()" func is called.

For Example, Set JSON struct as the request body:

res, err := esrest.New().
			Post("http://httpbin.org/post").
			Body(struct {
				Message string `json:"message"`
			}{"ok"}).
			Do()

Set JSON struct pointer as the request body:

res, err := esrest.New().
			Post("http://httpbin.org/post").
			Body(&struct {
           	Message string `json:"message"`
			}{"ok"}).
			Do()

Set bytes slice as the request body:

res, err := esrest.New().
			Post("http://httpbin.org/post").
			Body([]byte(`{"message":"ok"}`)).
			Do()

Set HTTP request body as string:

	es, err := esrest.New().
    			Post("http://httpbin.org/post").
    			Body(string(`{"message":"ok"}`)).
     			Do()

Set HTTP request body as map:

m := map[string]interface{}{
		"message": "ok",
}

res, err := esrest.New().
		    Post("http://httpbin.org/post").
		    Body(m).
		    Do()

func (*Builder) Debug

func (b *Builder) Debug(debug bool) *Builder

func (*Builder) Delete

func (b *Builder) Delete(url string) *Builder

Delete uses the http DELETE method with provided url and returns Builder.

func (*Builder) Do

func (b *Builder) Do() (*http.Response, error)

Do executes the http request client and returns http.Response and error.

func (*Builder) DoJson

func (b *Builder) DoJson(v interface{}) (*http.Response, error)

DoJson executes the http request client and returns http.Response and error.

func (*Builder) Get

func (b *Builder) Get(url string) *Builder

Get uses the http GET method with provided url and returns Builder.

func (*Builder) Head

func (b *Builder) Head(url string) *Builder

Head uses the http HEAD method with provided url and returns Builder.

func (*Builder) Header

func (b *Builder) Header(key, value string) *Builder

Header sets http header key with value and returns Builder.

func (*Builder) Logger

func (b *Builder) Logger(log *log.Logger) *Builder

Logger sets the provided logger and returns Builder.

func (*Builder) Post

func (b *Builder) Post(url string) *Builder

Post uses the http POST method with provided url and returns Builder.

func (*Builder) Put

func (b *Builder) Put(url string) *Builder

Put uses the http PUT method with provided url and returns Builder.

func (*Builder) Query

func (b *Builder) Query(key, value string) *Builder

Query sets http QUERY parameter key with value and returns Builder.

func (*Builder) Timeout

func (b *Builder) Timeout(timeout time.Duration) *Builder

Jump to

Keyboard shortcuts

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