httpclient

package
v0.1.4 Latest Latest
Warning

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

Go to latest
Published: Apr 12, 2024 License: BSD-3-Clause Imports: 8 Imported by: 0

README

httpclient

The httpclient package implements the core functionality of a coder-based http client.

Getting Started

package main

import (
	"context"
	"encoding/json"
	"net/http"
	"net/url"

	"github.com/easy-techno-lab/proton/coder"
	"github.com/easy-techno-lab/proton/httpclient"
	"github.com/easy-techno-lab/proton/logger"
)

func main() {
	cdrJSON := coder.NewCoder("application/json", json.Marshal, json.Unmarshal)

	clientJSON := httpclient.New(cdrJSON, http.DefaultClient)

	URL := "http://localhost:8080/example/"

	params := make(url.Values)
	params.Add("id", "1")

	// To add additional data to the request, use the optional function f(*http.Request)
	f := func(r *http.Request) {
		r.Header.Set("Accept", "application/json")
		r.URL.RawQuery = params.Encode()
	}

	resp, err := clientJSON.Request(context.TODO(), http.MethodGet, URL, nil, f)
	if err != nil {
		panic(err)
	}

	defer logger.Closer(resp.Body)

	res := &struct {
		// some fields
	}{}

	if err = clientJSON.Decode(resp.Body, res); err != nil {
		panic(err)
	}
}

package main

import (
	"context"
	"encoding/json"
	"net/http"

	"github.com/easy-techno-lab/proton/coder"
	"github.com/easy-techno-lab/proton/httpclient"
	"github.com/easy-techno-lab/proton/logger"
)

func main() {
	cdrJSON := coder.NewCoder("application/json", json.Marshal, json.Unmarshal)

	clientJSON := httpclient.New(cdrJSON, http.DefaultClient)

	URL := "http://localhost:8080/v1/example/"

	req := &struct {
		ID int `json:"id"`
	}{ID: 1}

	resp, err := clientJSON.Request(context.TODO(), http.MethodPost, URL, req, nil)
	if err != nil {
		panic(err)
	}

	defer logger.Closer(resp.Body)

	res := &struct {
		// some fields
	}{}

	if err = clientJSON.Decode(resp.Body, res); err != nil {
		panic(err)
	}
}

The httpclient package contains functions that are used as middleware on the http client side.

Getting Started

package main

import (
	"net/http"

	"github.com/easy-techno-lab/proton/httpclient"
	"github.com/easy-techno-lab/proton/logger"
)

func main() {
	transport := httpclient.RoundTripperSequencer(
		http.DefaultTransport,
		httpclient.DumpHttp(logger.LevelTrace),
		httpclient.Timer(logger.LevelInfo),
		httpclient.PanicCatcher,
	)

	hct := new(http.Client)
	hct.Transport = transport
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func DumpHttp

func DumpHttp(logLevel logger.Level) func(http.RoundTripper) http.RoundTripper

DumpHttp dumps the HTTP request and response, and prints out with logFunc.

func PanicCatcher

func PanicCatcher(next http.RoundTripper) http.RoundTripper

PanicCatcher handles panics in http.RoundTripper.

func RoundTripperSequencer

func RoundTripperSequencer(baseRoundTripper http.RoundTripper, rts ...func(http.RoundTripper) http.RoundTripper) http.RoundTripper

RoundTripperSequencer chains http.RoundTrippers in a chain.

func Timer

func Timer(logLevel logger.Level) func(http.RoundTripper) http.RoundTripper

Timer measures the time taken by http.RoundTripper.

Types

type Client

type Client interface {
	coder.Coder
	Request(ctx context.Context, method, url string, body any, f func(*http.Request)) (*http.Response, error)
}

func New

func New(coder coder.Coder, client *http.Client) Client

New returns a new Client.

type RoundTripper

type RoundTripper func(*http.Request) (*http.Response, error)

The RoundTripper type is an adapter to allow the use of ordinary functions as HTTP round trippers. If f is a function with the appropriate signature, Func(f) is a RoundTripper that calls f.

func (RoundTripper) RoundTrip

func (f RoundTripper) RoundTrip(r *http.Request) (*http.Response, error)

RoundTrip calls f(r).

Jump to

Keyboard shortcuts

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