Skip to content

Logging HTTP Client #17

@moshloop

Description

@moshloop

Create a new fluent http adapter that wraps net.http and adds logging and retry with backoff

import (
"github.com/flanksource/commons/logger"
"net/http"
)
type HttpClient struct {

InsecureSkipVerify bool
DialContext func(ctx context.Context, network, addr string) (net.Conn, error)
// Headers are automatically added to all requests
Headers map[string]string
// BaseURL is added as a prefix to all URL's
BaseURL string

// LOGGING

// Specify if request/response bodies should be logged
TraceBody bool
TraceResponse bool
Trace bool
// GET's are on TRACE, PUT/PATCH/POST are on Debug, and DELETE are on Info
Logger Logger

// RETRY HANDLING
// Cancel the request after this timeout
Timeout time.Duration
// Number of retries to attempt
Retries int
// Time to wait between retries
RetryWait time.Duration
// Amount to increase RetryWait with each failure, 2.0 is a good option for exponential backoff
Factor float32
}

type HttpResponse struct {
     *http.Response
}

func (resp *HttpResponse) IsOK() bool {}
func (resp *HttpResponse) AsError() error {}
func (resp *HttpResponse) AsString() (string,error) {}
func (resp *HttpResponse) AsReader() (io.Reader, error) {}
func (resp *HttpResponse) AsBytes() ([]byte,error) {}

func (client *HttpClient) Get(url string)  HttpResponse {}
func (client *HttpClient) Post(url string, body interface{})  HttpResponse {}
func (client *HttpClient) Delete(url string)  HttpResponse {}
func (client *HttpClient) Patch(url string,  body interface{})  HttpResponse {}
func (client *HttpClient) Put(url string, body interface{})  HttpResponse {}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions