Popularity
3.6
Growing
Activity
2.1
-
71
3
3

Programming language: Go
License: Apache License 2.0

mediary alternatives and similar packages

Based on the "Libraries for creating HTTP middlewares" category.
Alternatively, view mediary alternatives based on common mentions on social networks and blogs.

  • rye

    Tiny Go middleware library (with canned Middlewares) that supports JWT, CORS, Statsd, and Go 1.7 context
  • renderer

    Simple, lightweight and faster response (JSON, JSONP, XML, YAML, HTML, File) rendering package for Go.

Do you think we are missing an alternative of mediary or a related project?

Add another 'Libraries for creating HTTP middlewares' Package

README

mediary

Go Report Card Coverage Status Build Status Go package dev

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

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

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

yourClient := &http.Client{}
yourClientWithInterceptor := mediary.Init().
    WithPreconfiguredClient(yourClient).
    AddInterceptors(your interceptor).
    Build()

Read this for more information