Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Logging HTTP Client #17

Open
moshloop opened this issue Aug 6, 2020 · 0 comments
Open

Logging HTTP Client #17

moshloop opened this issue Aug 6, 2020 · 0 comments

Comments

@moshloop
Copy link
Member

moshloop commented Aug 6, 2020

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 {}
@moshloop moshloop added the good first issue Good for newcomers label Aug 6, 2020
@moshloop moshloop removed the good first issue Good for newcomers label Sep 21, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant