httpc

package module
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Jul 19, 2023 License: Apache-2.0 Imports: 15 Imported by: 0

README

httpc

Go Report Card GitHub license codecov

A tiny HTTP Client wrapper based on Go http package.

Installing

WARNING

API is not yet stable and we might introduce breaking changes until we reached version v1.0.0. See Breaking Changes section for deprecation notes.

go get -u github.com/nbs-go/httpc

Breaking Changes

v0.7.0
  • Revert Go minimum version to 1.17
  • Remove built-in OpenTelemetry instrumentation, replaced with SetGlobalTransporterOverrider() function
v0.6.0
  • Upgrade Go minimum version to 1.19

Usage

TODO

Enable OpenTelemetry Instrumentation
package main

import (
	"github.com/nbs-go/httpc"
	"go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp"
	"net/http"
)

// On init() package, SetGlobalTransporterOverrider to enable OpenTelemetry instrumentation
// across all initiated httpc.Client
func init() {
	httpc.SetGlobalTransporterOverrider(func(existingTransporter http.RoundTripper) http.RoundTripper {
		// Wrap existingTransporter with otelhttp.Transporter to enable instrumentation
		return otelhttp.NewTransport(existingTransporter)
	})
}

Contributors

contributors

Documentation

Index

Constants

View Source
const (
	MimeTypeJson           = "application/json"
	MimeTypeUrlEncodedForm = "application/x-www-form-urlencoded"
)
View Source
const (
	ContextRequestId = iota + 1
)
View Source
const (
	HeaderContentType = "Content-Type"
)
View Source
const (
	InstrumentationOpenTelemetry = iota + 1
)

Variables

This section is empty.

Functions

func SetGlobalTransporterOverrider added in v0.7.0

func SetGlobalTransporterOverrider(fn TransporterOverrider)

SetGlobalTransporterOverrider set value to global overrideTransporter function. This function will override every transporter initiated afterward. Example use case is to wrap http.Client Transporter field with instrumentation such as OpenTelemetry otelhttp package (go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp)

Types

type Client

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

func NewClient

func NewClient(baseUrl string, args ...SetClientOptionsFn) *Client

func (*Client) DoRequest

func (c *Client) DoRequest(ctx context.Context, method Method, endpointPath string, args ...SetRequestOptionFn) (*http.Response, []byte, error)

type ContextKey

type ContextKey int8

type Instrumentation added in v0.6.0

type Instrumentation int8

type Method

type Method = string
const (
	MethodGet     Method = "GET"
	MethodHead    Method = "HEAD"
	MethodPost    Method = "POST"
	MethodPut     Method = "PUT"
	MethodPatch   Method = "PATCH" // RFC 5789
	MethodDelete  Method = "DELETE"
	MethodConnect Method = "CONNECT"
	MethodOptions Method = "OPTIONS"
	MethodTrace   Method = "TRACE"
)

Common HTTP methods.

Unless otherwise noted, these are defined in RFC 7231 section 4.3.

type PreRequestFn added in v0.3.0

type PreRequestFn func(req *http.Request, reqBody []byte)

type RESTRequest

type RESTRequest struct {
	Id string
	// contains filtered or unexported fields
}

func NewRESTRequest

func NewRESTRequest(c *Client, method Method, endpointPath string, args ...SetRequestOptionFn) *RESTRequest

NewRESTRequest creates a builder for REST API style request that use json as request and response body

func (*RESTRequest) AddHeader

func (rr *RESTRequest) AddHeader(args ...string) *RESTRequest

func (*RESTRequest) AddOption

func (rr *RESTRequest) AddOption(fn ...SetRequestOptionFn) *RESTRequest

func (*RESTRequest) AddQuery

func (rr *RESTRequest) AddQuery(args ...string) *RESTRequest

func (*RESTRequest) Body

func (rr *RESTRequest) Body(b interface{}) *RESTRequest

func (*RESTRequest) Do

func (rr *RESTRequest) Do(ctx context.Context, dst interface{}) (*http.Response, error)

Do prepare REST request, do and parse response body to JSON dst

func (*RESTRequest) PreRequest added in v0.3.0

func (rr *RESTRequest) PreRequest(fn PreRequestFn) *RESTRequest

type SetClientOptionsFn

type SetClientOptionsFn func(o *clientOptions)

func DisableHTTP2 added in v0.5.0

func DisableHTTP2() SetClientOptionsFn

DisableHTTP2 disable HTTP/2 alternate protocol

func LogDump added in v0.4.0

func LogDump(enable bool) SetClientOptionsFn

LogDump enable log HTTP request and response dump

func Namespace

func Namespace(n string) SetClientOptionsFn

Namespace override default Client namespace value

type SetRequestOptionFn

type SetRequestOptionFn func(o *requestOptions)

func AddHeader

func AddHeader(args ...string) SetRequestOptionFn

func AddQuery

func AddQuery(args ...string) SetRequestOptionFn

func DisableCanonicalHeader added in v0.5.0

func DisableCanonicalHeader() SetRequestOptionFn

func PreRequest added in v0.3.0

func PreRequest(fn PreRequestFn) SetRequestOptionFn

func SetBody

func SetBody(body interface{}) SetRequestOptionFn

func SetJsonBody

func SetJsonBody(body interface{}) SetRequestOptionFn

func SetUrlEncodedFormBody added in v0.2.0

func SetUrlEncodedFormBody(body url.Values) SetRequestOptionFn

func Timeout

func Timeout(ms int) SetRequestOptionFn

type TransporterOverrider added in v0.7.0

type TransporterOverrider func(existing http.RoundTripper) http.RoundTripper

TransporterOverrider is a function to override how http client Transporter value. Could be used to wrap existing client Transporter (rt) for instrumentation or override Transporter value

Jump to

Keyboard shortcuts

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