panggilhttp

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Mar 9, 2021 License: MIT Imports: 10 Imported by: 0

README

Panggil HTTP

GitHub tag (latest by date) GitHub

An enhanced HTTP client for Go with features likes:

  • Support call GET Method more than 1 URL and merged the response body.
  • Set which values from the response body to show with Whitelist or Blacklist.
  • HTTP retry if failed, with attempts and interval configuration.

Installation

go get github.com/KodepandaID/panggilhttp

Example

Basic GET
import "github.com/KodepandaID/panggilhttp"

func main() {
    client := panggilhttp.New()
    
    resp, e := client.
		Get("http://localhost:3000/hotels", nil, nil).
		Do()
	if e != nil {
		panic(e)
	}
}
Method GET with merging the response body
import "github.com/KodepandaID/panggilhttp"

func main() {
    client := panggilhttp.New()
    
    resp, e := client.
		Get("http://localhost:3000/hotels", []string{"id_hotel", "name"}, nil).
		Get("http://localhost:3000/hotel-destination", []string{"destination_id", "destinations"}, nil).
		Do()
	if e != nil {
		panic(e)
	}
}

API

For more detailed API, please read Godoc reference

License

Copyright Yudha Pratama Wicaksana, Licensed under MIT.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

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

Config to set the configuration to calling an HTTP.

func New

func New() *Config

New is an adapter to create new instance.

func (*Config) Delete

func (c *Config) Delete(url string) *Config

Delete to set HTTP DELETE method.

func (*Config) Do

func (c *Config) Do() (Response, error)

Do to running an HTTP call. Response header and cookies can be returned if only use 1 call HTTP.

func (*Config) Get

func (c *Config) Get(url string, whitelist, blacklist []string) *Config

Get to set HTTP GET method. For the GET method, you can call this function more than 1, to merging the response body. You can use whitelist and blacklist args to filtering the response body. Whitelist to get field value from response body. Blacklist to ignore the field from response body.

func (*Config) Patch

func (c *Config) Patch(url string) *Config

Patch to set HTTP PATCH method.

func (*Config) Post

func (c *Config) Post(url string) *Config

Post to set HTTP POST method.

func (*Config) Put

func (c *Config) Put(url string) *Config

Put to set HTTP PUT method.

func (*Config) SendFile

func (c *Config) SendFile(key, filename string, file []byte) *Config

SendFile to send file with POST, PUT or PATCH method.

func (*Config) SendFormData

func (c *Config) SendFormData(fd map[string]string) *Config

SendFormData to send multipart/form-data with POST, PUT or PATCH method.

func (*Config) SendJSON

func (c *Config) SendJSON(j map[string]interface{}) *Config

SendJSON to send json data with POST, PUT or PATCH method.

func (*Config) WithCookie

func (c *Config) WithCookie(cookies map[string]string) *Config

WithCookie to send HTTP cookies.

func (*Config) WithFailRetry

func (c *Config) WithFailRetry(interval time.Duration, attempt int) *Config

WithFailRetry to retrying if HTTP call fails. Use 2 argument interval and attempt. interval args in miliseconds. attempt args is int how much to retry HTTP calls.

func (*Config) WithHeader

func (c *Config) WithHeader(headers map[string]string) *Config

WithHeader to set HTTP headers.

func (*Config) WithTimeout

func (c *Config) WithTimeout(second time.Duration) *Config

WithTimeout to set HTTP timeout in seconds. The default value is 1 seconds.

type Response

type Response struct {
	StatusCode int
	Headers    map[string]string
	Cookies    map[string]string
	Body       []byte
}

Response is a response structure

Directories

Path Synopsis
pkg

Jump to

Keyboard shortcuts

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