Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/HereMobilityDevelopers/mediary
Add interceptors to GO http.Client
https://github.com/HereMobilityDevelopers/mediary
golang http-client interceptor middleware
Last synced: about 1 month ago
JSON representation
Add interceptors to GO http.Client
- Host: GitHub
- URL: https://github.com/HereMobilityDevelopers/mediary
- Owner: HereMobilityDevelopers
- License: apache-2.0
- Created: 2020-03-23T18:54:56.000Z (over 4 years ago)
- Default Branch: master
- Last Pushed: 2020-06-24T14:38:59.000Z (over 4 years ago)
- Last Synced: 2024-08-03T19:10:01.954Z (5 months ago)
- Topics: golang, http-client, interceptor, middleware
- Language: Go
- Homepage: https://github.com/HereMobilityDevelopers/mediary/wiki/Reasoning
- Size: 17.6 KB
- Stars: 88
- Watchers: 4
- Forks: 6
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
- awesome-go-extra - mediary - 03-23T18:54:56Z|2020-06-24T14:38:59Z| (Web Frameworks / Fail injection)
README
# mediary
[![Go Report Card](https://goreportcard.com/badge/github.com/HereMobilityDevelopers/mediary)](https://goreportcard.com/report/github.com/HereMobilityDevelopers/mediary)
[![Coverage Status](https://coveralls.io/repos/github/HereMobilityDevelopers/mediary/badge.svg?branch=master)](https://coveralls.io/github/HereMobilityDevelopers/mediary?branch=master)
[![Build Status](https://travis-ci.com/HereMobilityDevelopers/mediary.svg?branch=master)](https://travis-ci.com/HereMobilityDevelopers/mediary)
[![Go package dev](https://godoc.org/github.com/HereMobilityDevelopers/mediary?status.svg)](https://godoc.org/github.com/HereMobilityDevelopers/mediary)Add interceptors to http.Client and you will be able to
* Dump request and/or response to a `Log`
* Alter your requests before they are sent or responses before they are returned
* Add Tracing information using `Opentracing`/`Jaeger`
* Send metrics to `statsd`All this and more while using a "regular" `http.Client`
## Usage
```go
var client *http.Client
client = mediary.Init().AddInterceptors(your interceptor).Build()
client.Get("https://golang.org")
```## Dump Example
```go
client := mediary.Init().AddInterceptors(dumpInterceptor).Build()
client.Get("https://golang.org")func dumpInterceptor(req *http.Request, handler mediary.Handler) (*http.Response, error) {
if bytes, err := httputil.DumpRequestOut(req, true); err == nil {
fmt.Printf("%s", bytes)//GET / HTTP/1.1
//Host: golang.org
//User-Agent: Go-http-client/1.1
//Accept-Encoding: gzip
}
return handler(req)
}
```## Interceptor
**Interceptor** is a function
type Interceptor func(*http.Request, Handler) (*http.Response, error)
**Handler** is just an alias to
http.Roundtripper
function called `RoundTrip`
type Handler func(*http.Request) (*http.Response, error)
## Multiple interceptors
It's possible to chain interceptors
```go
client := mediary.Init().
AddInterceptors(First Interceptor, Second Interceptor).
AddInterceptors(Third Interceptor).
Build()```
This is how it actually works
- First Intereptor: *Request*
- Second Interceptor: *Request*
- Third Interceptor: *Request*
- **Handler** <-- Actual http call
- Third Interceptor: *Response*
- Second Interceptor: *Response*
- First Interceptor: *Response*## Using custom client/transport
If you already have a pre-configured `http.Client` you can use it as well
```go
yourClient := &http.Client{}
yourClientWithInterceptor := mediary.Init().
WithPreconfiguredClient(yourClient).
AddInterceptors(your interceptor).
Build()
```Read [this](https://github.com/HereMobilityDevelopers/mediary/wiki/Reasoning) for more information