httpclient

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 17, 2024 License: BSD-3-Clause Imports: 10 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/utils/log"
)

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

	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()
	}

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

	defer log.Closer(ctx, resp.Body)

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

	if err = clientJSON.Decode(ctx, 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/utils/log"
)

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

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

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

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

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

	defer log.Closer(ctx, resp.Body)

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

	if err = clientJSON.Decode(ctx, 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 (
	"log/slog"
	"net/http"

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

func main() {
	transport := httpclient.RoundTripperSequencer(
		http.DefaultTransport,
		httpclient.DumpHttp(slog.LevelDebug),
		httpclient.Timer(slog.LevelInfo),
		httpclient.Tracer,
		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(level slog.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(level slog.Level) func(http.RoundTripper) http.RoundTripper

Timer measures the time taken by http.RoundTripper.

func Tracer added in v0.2.0

func Tracer(next http.RoundTripper) http.RoundTripper

Tracer adds trace ID to the request context.

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