httpc

package module
v1.0.2 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2021 License: Apache-2.0 Imports: 15 Imported by: 1

README

A simple wrapper around the default Go http client optimized for ease-of-use

Github Release GoDoc Go Report Card Build/Test Status

This package wraps the Go standard http client, providing a simplified interaction model using method chaining and additional capabilities such as optional in-flow validation against an OpenAPI specification.

Features

  • Simple, method chaining based interface for HTTP client requests
  • Simulation of request delays (optional)
  • Validation of request + response against OpenAPI specification (optional)
  • Customization of HTTP client via functional parameter

Installation

go get -u github.com/fako1024/httpc

Examples

Perform simple HTTP GET request
err := httpc.New("GET", "http://example.org").Run()
if err != nil {
	log.Fatalf("Error performing GET request: %s", err)
}
Perform HTTPS POST request with a simple body, disabling certificate validation and parsing the response
err := httpc.New("POST", "https://example.org").
	SkipCertificateVerification().
	Body([]byte{0x1, 0x2}).
	ParseFn(func(resp *http.Response) error {

		// Read the binary data from the response body
		bodyBytes, err := ioutil.ReadAll(resp.Body)
		if err != nil {
			return fmt.Errorf("Failed to read response body: %s", err)
		}

		log.Printf("Read body content: %s\n", string(bodyBytes))

		return nil
	}).Run()

	if err != nil {
		log.Fatalf("Error performing POST request: %s", err)
	}
Perform HTTPS GET request (with query parameters + headers), validating request and response against OpenAPIv3 specification
openAPIFileData, err := ioutil.ReadFile("/tmp/openapi.json")
if err != nil {
	log.Fatalf("Error opening OpenAPI specification file: %s", err)
}

err = httpc.New("GET", "https://example.org").
	SkipCertificateVerification().
	QueryParams(httpc.Params{
		"param": "test",
	}).
	Headers(httpc.Params{
		"X-HEADER-TEST": "test",
	}).
	OpenAPIValidationFileData(openAPIFileData).
	Run()

if err != nil {
	log.Fatalf("Error performing POST request: %s", err)
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Copy added in v1.0.2

func Copy(w io.Writer) func(resp *http.Response) error

Copy copies the response body into any io.Writer

func ParseJSON added in v1.0.2

func ParseJSON(v interface{}) func(resp *http.Response) error

ParseJSON parses the response body as JSON into a struct

func ParseYAML added in v1.0.2

func ParseYAML(v interface{}) func(resp *http.Response) error

ParseYAML parses the response body as YAML into a struct

Types

type Params

type Params = map[string]string

Params is an alias for a map of string key / value pairs

type Request

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

Request represents a generic web request for quick execution, providing access to method, URL parameters, headers, the body and an optional 1st class function used to parse the result

func New

func New(method, uri string) *Request

New instantiates a new http client

func (*Request) AcceptedResponseCodes

func (r *Request) AcceptedResponseCodes(acceptedResponseCodes []int) *Request

AcceptedResponseCodes defines a set of accepted HTTP response codes for the client call

func (*Request) Body

func (r *Request) Body(body []byte) *Request

Body sets the body for the client call

func (*Request) ClientCertificates added in v1.0.1

func (r *Request) ClientCertificates(clientCert, clientKey, caCert []byte) (*Request, error)

ClientCertificates sets client certificates from memory

func (*Request) ClientCertificatesFromFiles added in v1.0.1

func (r *Request) ClientCertificatesFromFiles(certFile, keyFile, caFile string) (*Request, error)

ClientCertificatesFromFiles sets client certificates from files

func (*Request) Delay

func (r *Request) Delay(delay time.Duration) *Request

Delay sets an artificial delay for the client call

func (*Request) ErrorFn added in v1.0.2

func (r *Request) ErrorFn(errorFn func(resp *http.Response) error) *Request

ErrorFn sets a parsing function for results not handled by ParseFn

func (*Request) GetBody

func (r *Request) GetBody() []byte

GetBody returns the body of the request

func (*Request) GetMethod

func (r *Request) GetMethod() string

GetMethod returns the method of the request

func (*Request) GetURI

func (r *Request) GetURI() string

GetURI returns the URI of the request

func (*Request) Headers

func (r *Request) Headers(headers Params) *Request

Headers sets the headers for the client call

func (*Request) HostName

func (r *Request) HostName(host string) *Request

HostName sets an explicit hostname for the client call

func (*Request) ModifyHTTPClient

func (r *Request) ModifyHTTPClient(fn func(c *http.Client)) *Request

ModifyHTTPClient executes any function / allows setting parameters of the underlying HTTP client before the actual request is made

func (*Request) OpenAPIValidationFileData

func (r *Request) OpenAPIValidationFileData(fileData []byte) *Request

OpenAPIValidationFileData sets an OpenAPI validation file for the client call using a byte slice (containing the raw JSON file data)

func (*Request) ParseFn

func (r *Request) ParseFn(parseFn func(resp *http.Response) error) *Request

ParseFn sets a parsing function for the result of the client call

func (*Request) QueryParams

func (r *Request) QueryParams(queryParams Params) *Request

QueryParams sets the query parameters for the client call

func (*Request) Run

func (r *Request) Run() error

Run executes a request

func (*Request) SkipCertificateVerification

func (r *Request) SkipCertificateVerification() *Request

SkipCertificateVerification will accept any SSL certificate

func (*Request) Timeout

func (r *Request) Timeout(timeout time.Duration) *Request

Timeout sets timeout for the client call

func (*Request) Transport added in v1.0.2

func (r *Request) Transport(transport http.RoundTripper) *Request

Transport forces a specific transport for the HTTP client (e.g. http.DefaultTransport in order to support standard gock flows)

Jump to

Keyboard shortcuts

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