http_client_wrapper

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2023 License: MIT Imports: 6 Imported by: 0

README

Go http client wrapper

A convenience wrapper around go http client.

Install

go get github.com/AkselMeola/go-http-client-wrapper

Example usage

var responseData map[string]interface{}
handlerFunc := func(r *http.Response) error {
    return json.NewDecoder(r.Body).Decode(&responseData)
}

err := NewClient("https://api.domain", time.Second*15).
    Post(
        context.Background(),
        "/resource/path",
        strings.NewReader(`{"some": "json"}`),
        handlerFunc,
    )

Documentation

Overview

Example (BuildURL)
fmt.Println(buildUrlWoErr("http://www.example.com", "foo/bar"))
fmt.Println(buildUrlWoErr("http://www.example.com", "foo/bar?baz=1"))
fmt.Println(buildUrlWoErr("http://www.example.com", "/foo/bar"))
fmt.Println(buildUrlWoErr("http://www.example.com/", "/double-slash"))
Output:

http://www.example.com/foo/bar
http://www.example.com/foo/bar?baz=1
http://www.example.com/foo/bar
http://www.example.com/double-slash

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Error

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

func NewError

func NewError(msg string, original error) *Error

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type HttpClient

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

func NewClient

func NewClient(basePath string, timeout time.Duration) *HttpClient

func (*HttpClient) Get

func (c *HttpClient) Get(ctx context.Context, path string, handler ResponseHandler, opts ...RequestOption) error

func (*HttpClient) Post

func (c *HttpClient) Post(
	ctx context.Context,
	path string,
	body io.Reader,
	handler ResponseHandler,
	opts ...RequestOption,
) error

func (*HttpClient) PostForm

func (c *HttpClient) PostForm(
	ctx context.Context,
	path string,
	formValues map[string]string,
	handler ResponseHandler,
	opts ...RequestOption,
) error

type RequestOption

type RequestOption = func(r *http.Request)

type RequestOptionFunc

type RequestOptionFunc func(*http.Request)

func WithAcceptHeader

func WithAcceptHeader(contentType string) RequestOptionFunc

func WithContentType

func WithContentType(contentType string) RequestOptionFunc

func WithHeaders

func WithHeaders(headers map[string]string) RequestOptionFunc

func WithQueryParams

func WithQueryParams(params map[string]string) RequestOptionFunc

type ResponseHandler

type ResponseHandler func(r *http.Response) error

Jump to

Keyboard shortcuts

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