requestor

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 2, 2021 License: MIT Imports: 8 Imported by: 0

README

Requestor

Build Status Go Report Card codecov GitHub repo size GitHub GoDoc

GitHub Logo1

Requestor is a simple HTTP library for Developers. The main idea behind this project is to make HTTP requests fun, simple and easy (Inspired by python requests).

Features

  • All the HTTP methods are supported
  • Proxy Support
  • Retry Support - You can set the max number of times you want to retry if the request fails
  • TLS Client Certificates support
  • Enable/Disable Keep-Alive
  • Timeouts
  • Built purely using the standard library
  • more coming soon

Installation

go get github.com/flannel-dev-lab/Requestor

Usage

package main

import (
    "fmt"
    "github.com/flannel-dev-lab/Requestor"
    "log"
)

func main() {
    client := requestor.New()
    headers := map[string][]string{
    	"Content-Type": {"application/json"},
    }
    
    queryParams := map[string][]string{
    	"arg1": {"test"},
    }

	response, err := client.Get("http://httpbin.org/get", headers, queryParams)
	if err != nil {
		log.Fatal(err)
	}

    fmt.Println("Do something with ", response)
}

The usage is similar for POST method as well, the only difference is that you send can send data in the request. For JSON requests, the data can be either a struct or map, by default Requestor assumes Content-Type to be application/json

For application/x-www-form-urlencoded requests, make sure the data is in the form map[string][]string

Note: Requestor does not support XML

package main

import (
    "fmt"
    "github.com/flannel-dev-lab/Requestor"
    "log"
)

func main() {
    client := requestor.New()
    headers := map[string][]string{
    	"Content-Type": {"application/json"},
    }
    
    queryParams := map[string][]string{
    	"arg1": {"test"},
    }

	response, err := client.Post("http://httpbin.org/get", headers, queryParams, map[string]string{"hello": "world"})
	if err != nil {
		log.Fatal(err)
	}

    fmt.Println("Do something with ", response)
}

Advanced Usage

Setting Timeouts
client := requestor.New()
client.SetTimeout(10)

Using Proxy

client := requestor.New()
client.SetHTTPProxy(proxyURL, username, password string)

or

client := requestor.New()
client.SetHTTPSProxy(proxyURL, username, password string)
Disabling Keep-Alive
client := requestor.New()
client.DisableKeepAlive(true)
Much-more settings can be found here GoDoc

Contributing

Requestor loves contributions. If you find a bug, want to add a feature, you can create a PR and we will take a look.

Documentation

Overview

Package requestor contains the methods to make HTTP requests to different endpoints

Package requestor contains the methods to make HTTP requests to different endpoints

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Client

type Client struct {
	// MaxRetriesOnError specifies how many times we should retry when a request to server fails
	MaxRetriesOnError uint8
	// TimeBetweenRetries specifies the amount of time between each retry
	TimeBetweenRetries int64
	// Timeout specifies a time limit for requests made by this
	// Client. The timeout includes connection time, any
	// redirects, and reading the response body. The timer remains
	// running after Get, Head, Post, or Do return and will
	// interrupt reading of the Response.Body.
	//
	// A Timeout of zero means no timeout.
	// The Client cancels requests to the underlying Transport
	// as if the Request's Context ended.
	Timeout time.Duration
	// IdleConnectionTimeout specifies how long an idle connection is kept in the connection pool
	// 0 means no timeout
	IdleConnectionTimeout time.Duration
	// MaxConnectionsPerHost specifies the max number of connection per host which include connections in dialing
	// active and idle states. When exceeded request gets cancelled with net/http: request canceled.
	// 0 means no limit
	MaxConnectionsPerHost int
	// MaxIdleConnectionsPerHost specifies the max number of keep-alive connections per host
	MaxIdleConnectionsPerHost int
	// MaxIdleConnections specifies the max number of keep-alive connections
	MaxIdleConnections int
	// DisableKeepAlives makes sure that the transport is used only once by disabling keep-alives
	// Default is true
	DisableKeepAlives bool
	// TLSClientConfig specifies the TLS config to use
	TLSClientConfig *tls.Config
	// contains filtered or unexported fields
}

Client here is a struct that holds different configurations to tune Requestor

func New

func New() (client *Client)

New creates a new Client object

func (*Client) Connect

func (c *Client) Connect(url string, headers, queryParams map[string][]string) (response *http.Response, err error)

Connect performs a HTTP CONNECT request. It takes in a URL, user specified headers, query params and returns Response error if exist

func (*Client) Delete

func (c *Client) Delete(url string, headers, queryParams map[string][]string, data interface{}) (response *http.Response, err error)

Delete performs a HTTP DELETE request. It takes in a URL, user specified headers, query params, data and returns Response and error if exist

func (*Client) DisableKeepAlive

func (c *Client) DisableKeepAlive(val bool)

DisableKeepAlive sets keep-alive to either true/false

func (*Client) Get

func (c *Client) Get(url string, headers, queryParams map[string][]string) (response *http.Response, err error)

Get performs a HTTP GET request. It takes in a URL, user specified headers, query params and returns Response and error if exist

func (*Client) Head

func (c *Client) Head(url string) (response *http.Response, err error)

Head performs a HTTP HEAD request. It takes in a URL, user specified headers, query params and returns Response and error if exist

func (*Client) Options

func (c *Client) Options(url string, headers, queryParams map[string][]string) (response *http.Response, err error)

Options performs a HTTP Options request. It takes in a URL, user specified headers, query params and returns Response and error if exist

func (*Client) Patch

func (c *Client) Patch(url string, headers, queryParams map[string][]string, data interface{}) (response *http.Response, err error)

Patch performs a HTTP PATCH request. It takes in a URL, user specified headers, query params, data and returns Response and error if exist

func (*Client) Post

func (c *Client) Post(url string, headers, queryParams map[string][]string, data interface{}) (response *http.Response, err error)

Post performs a HTTP POST request. It takes in a URL, user specified headers, query params, data and returns Response and error if exist

func (*Client) Put

func (c *Client) Put(url string, headers, queryParams map[string][]string, data interface{}) (response *http.Response, err error)

Put performs a HTTP PUT request. It takes in a URL, user specified headers, query params, data and returns Response and error if exist

func (*Client) SetHTTPProxy

func (c *Client) SetHTTPProxy(proxyURL, username, password string)

SetHTTPProxy sets a HTTP proxy to the transport, proxyURL is a required parameter, but username and password is optional parameters. Proxy URL should be of format IP:PORT or HOSTNAME:PORT

func (*Client) SetHTTPSProxy

func (c *Client) SetHTTPSProxy(proxyURL, username, password string)

SetHTTPSProxy sets a HTTP proxy to the transport, proxyURL is a required parameter, but username and password is optional parameters. Proxy URL should be of format IP:PORT or HOSTNAME:PORT

func (*Client) SetIdleConnectionTimeout

func (c *Client) SetIdleConnectionTimeout(timeout time.Duration)

SetIdleConnectionTimeout sets the idle connection timeout

func (*Client) SetMaxConnectionsPerHost

func (c *Client) SetMaxConnectionsPerHost(connectionCount int)

SetMaxConnectionsPerHost sets the max connections per host

func (*Client) SetMaxIdleConnections

func (c *Client) SetMaxIdleConnections(connectionCount int)

SetMaxIdleConnections sets the max idle connections

func (*Client) SetMaxIdleConnectionsPerHost

func (c *Client) SetMaxIdleConnectionsPerHost(connectionCount int)

SetMaxIdleConnectionsPerHost sets the max idle connections per host

func (*Client) SetMaxRetries

func (c *Client) SetMaxRetries(retries uint8, timeBetweenRetries int64)

SetMaxRetries sets the max amount of retries and the time between them in seconds

func (*Client) SetTLSClientConfig

func (c *Client) SetTLSClientConfig(tlsConfig *tls.Config)

SetTLSClientConfig attaches custom client tls config to Transport

func (*Client) SetTimeout

func (c *Client) SetTimeout(timeout time.Duration)

SetTimeout sets timeout to request

func (*Client) Trace

func (c *Client) Trace(url string, headers, queryParams map[string][]string) (response *http.Response, err error)

Trace performs a HTTP TRACE request. It takes in a URL, user specified headers, query params and returns Response and error if exist

Jump to

Keyboard shortcuts

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