Mediary
Add interceptors to GO http.Client
Install / Use
/learn @HereMobilityDevelopers/MediaryREADME
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
var client *http.Client
client = mediary.Init().AddInterceptors(your interceptor).Build()
client.Get("https://golang.org")
Dump Example
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
<code>type Interceptor func(*http.Request, Handler) (*http.Response, error)</code>
Handler is just an alias to <code>http.Roundtripper</code> function called RoundTrip
<code>type Handler func(*http.Request) (*http.Response, error) </code>
Multiple interceptors
It's possible to chain interceptors
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
- Third Interceptor: Request
- Second Interceptor: Response
- Second Interceptor: Request
- First Interceptor: Response
Using custom client/transport
If you already have a pre-configured http.Client you can use it as well
yourClient := &http.Client{}
yourClientWithInterceptor := mediary.Init().
WithPreconfiguredClient(yourClient).
AddInterceptors(your interceptor).
Build()
Read this for more information
Related Skills
node-connect
342.0kDiagnose OpenClaw node connection and pairing failures for Android, iOS, and macOS companion apps
xurl
342.0kA CLI tool for making authenticated requests to the X (Twitter) API. Use this skill when you need to post tweets, reply, quote, search, read posts, manage followers, send DMs, upload media, or interact with any X API v2 endpoint.
frontend-design
84.7kCreate distinctive, production-grade frontend interfaces with high design quality. Use this skill when the user asks to build web components, pages, or applications. Generates creative, polished code that avoids generic AI aesthetics.
openai-whisper-api
342.0kTranscribe audio via OpenAI Audio Transcriptions API (Whisper).
