requests

package
v0.0.5 Latest Latest
Warning

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

Go to latest
Published: Aug 8, 2018 License: MIT Imports: 12 Imported by: 0

README

Requests

A wrapper around net/http with less public global variables

Originally from xephon-b

Usage

TODO:

Changelog

  • use same config as DefaultTransport in net/http
  • add client struct and default client which wraps http.Client
  • support socks5 proxy https://github.com/dyweb/gommon/issues/18
  • rename GetJSON to get GetJSONStringMap, this should break old Xephon-B code, which is now in tsdb-proxy

Documentation

Overview

Package requests is a wrapper around net/http with less public global variables

Package requests wrap net/http like requests did for python it is easy to use, but not very efficient

Index

Constants

View Source
const (
	ShadowSocksLocal = "127.0.0.1:1080"
	ContentJSON      = "application/json"
)
View Source
const AuthorizationHeader = "Authorization"

NOTE: net/http already implemented it https://golang.org/pkg/net/http/#Request.BasicAuth

Variables

View Source
var TransportBuilder transportBuilder

TODO: might switch to functional options https://dave.cheney.net/2014/10/17/functional-options-for-friendly-apis TODO: http proxy? might just use environment variable? https://stackoverflow.com/questions/14661511/setting-up-proxy-for-http-client TransportBuilder is the initial builder, its method use value receiver and return a new copy for chaining and keep itself unchanged

Functions

func ExtractBasicAuth

func ExtractBasicAuth(val string) (username string, password string, err error)

func ExtractBasicAuthFromRequest

func ExtractBasicAuthFromRequest(r *http.Request) (string, string, error)

func GenerateBasicAuth

func GenerateBasicAuth(username string, password string) string

func GetJSON

func GetJSON(url string, data interface{}) error

func GetJSONStringMap

func GetJSONStringMap(url string) (map[string]string, error)

func NewDefaultClient added in v0.0.5

func NewDefaultClient() *http.Client

NewDefaultClient returns a client using NewDefaultTransport

func NewDefaultTransport added in v0.0.5

func NewDefaultTransport() *http.Transport

NewDefaultTransport is copied from net/http/transport.go

Types

type Client

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

func NewClient

func NewClient(options ...func(h *http.Client)) *Client

func (*Client) Get

func (c *Client) Get(url string) (*Response, error)

func (*Client) GetJSON

func (c *Client) GetJSON(url string, data interface{}) error

func (*Client) GetJSONStringMap

func (c *Client) GetJSONStringMap(url string) (map[string]string, error)

func (*Client) Post added in v0.0.5

func (c *Client) Post(url string, data io.Reader) (*Response, error)

func (*Client) PostBytes added in v0.0.5

func (c *Client) PostBytes(url string, data []byte) (*Response, error)

func (*Client) PostString added in v0.0.5

func (c *Client) PostString(url string, data string) (*Response, error)

type Response

type Response struct {
	Res        *http.Response
	StatusCode int
	Data       []byte
}

func Get

func Get(url string) (*Response, error)

func Post added in v0.0.5

func Post(url string, data io.Reader) (*Response, error)

TODO: this defaultish wrapping methods should be generated by gommon generator

func PostBytes added in v0.0.5

func PostBytes(url string, data []byte) (*Response, error)

func PostString added in v0.0.5

func PostString(url string, data string) (*Response, error)

func (*Response) JSON

func (res *Response) JSON(data interface{}) error

func (*Response) JSONStringMap

func (res *Response) JSONStringMap() (map[string]string, error)

Jump to

Keyboard shortcuts

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