{"id":13786712,"url":"https://github.com/HereMobilityDevelopers/mediary","last_synced_at":"2025-05-11T22:32:06.352Z","repository":{"id":57519525,"uuid":"249517788","full_name":"HereMobilityDevelopers/mediary","owner":"HereMobilityDevelopers","description":"Add interceptors to GO http.Client","archived":false,"fork":false,"pushed_at":"2020-06-24T14:38:59.000Z","size":18,"stargazers_count":88,"open_issues_count":0,"forks_count":6,"subscribers_count":4,"default_branch":"master","last_synced_at":"2024-11-17T22:36:28.055Z","etag":null,"topics":["golang","http-client","interceptor","middleware"],"latest_commit_sha":null,"homepage":"https://github.com/HereMobilityDevelopers/mediary/wiki/Reasoning","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/HereMobilityDevelopers.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-03-23T18:54:56.000Z","updated_at":"2024-06-12T13:26:01.000Z","dependencies_parsed_at":"2022-08-30T04:22:17.906Z","dependency_job_id":null,"html_url":"https://github.com/HereMobilityDevelopers/mediary","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HereMobilityDevelopers%2Fmediary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HereMobilityDevelopers%2Fmediary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HereMobilityDevelopers%2Fmediary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/HereMobilityDevelopers%2Fmediary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/HereMobilityDevelopers","download_url":"https://codeload.github.com/HereMobilityDevelopers/mediary/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":253645398,"owners_count":21941315,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","http-client","interceptor","middleware"],"created_at":"2024-08-03T19:01:30.126Z","updated_at":"2025-05-11T22:32:06.003Z","avatar_url":"https://github.com/HereMobilityDevelopers.png","language":"Go","readme":"# mediary\n\n[![Go Report Card](https://goreportcard.com/badge/github.com/HereMobilityDevelopers/mediary)](https://goreportcard.com/report/github.com/HereMobilityDevelopers/mediary)\n[![Coverage Status](https://coveralls.io/repos/github/HereMobilityDevelopers/mediary/badge.svg?branch=master)](https://coveralls.io/github/HereMobilityDevelopers/mediary?branch=master)\n[![Build Status](https://travis-ci.com/HereMobilityDevelopers/mediary.svg?branch=master)](https://travis-ci.com/HereMobilityDevelopers/mediary)\n[![Go package dev](https://godoc.org/github.com/HereMobilityDevelopers/mediary?status.svg)](https://godoc.org/github.com/HereMobilityDevelopers/mediary)\n\nAdd interceptors to http.Client and you will be able to\n* Dump request and/or response to a `Log`\n* Alter your requests before they are sent or responses before they are returned\n* Add Tracing information using `Opentracing`/`Jaeger`\n* Send metrics to `statsd`\n\nAll this and more while using a \"regular\" `http.Client` \n\n## Usage\n```go\nvar client *http.Client\nclient = mediary.Init().AddInterceptors(your interceptor).Build()\nclient.Get(\"https://golang.org\")\n```\n\n## Dump Example\n```go\nclient := mediary.Init().AddInterceptors(dumpInterceptor).Build()\nclient.Get(\"https://golang.org\")\n\nfunc dumpInterceptor(req *http.Request, handler mediary.Handler) (*http.Response, error) {\n\tif bytes, err := httputil.DumpRequestOut(req, true); err == nil {\n\t\tfmt.Printf(\"%s\", bytes)\n\n\t\t//GET / HTTP/1.1\n\t\t//Host: golang.org\n\t\t//User-Agent: Go-http-client/1.1\n\t\t//Accept-Encoding: gzip\n\t}\n\treturn handler(req)\n}\n```\n\n## Interceptor\n**Interceptor** is a function \n\n\u003ccode\u003etype Interceptor func(*http.Request, Handler) (*http.Response, error)\u003c/code\u003e\n\n**Handler** is just an alias to \u003ccode\u003ehttp.Roundtripper\u003c/code\u003e function called `RoundTrip`\n\n\u003ccode\u003etype Handler func(*http.Request) (*http.Response, error) \u003c/code\u003e\n\n## Multiple interceptors\n\nIt's possible to chain interceptors\n\n```go\nclient := mediary.Init().\n    AddInterceptors(First Interceptor, Second Interceptor).\n    AddInterceptors(Third Interceptor).\n    Build()\n\n```\nThis is how it actually works\n- First Intereptor: *Request*\n    - Second Interceptor: *Request*\n        - Third Interceptor: *Request*\n            - **Handler** \u003c-- Actual http call\n        - Third Interceptor: *Response*\n    - Second Interceptor: *Response*\n- First Interceptor: *Response*\n\n## Using custom client/transport\n\nIf you already have a pre-configured `http.Client` you can use it as well\n```go\nyourClient := \u0026http.Client{}\nyourClientWithInterceptor := mediary.Init().\n    WithPreconfiguredClient(yourClient).\n    AddInterceptors(your interceptor).\n    Build()\n```\n\nRead [this](https://github.com/HereMobilityDevelopers/mediary/wiki/Reasoning) for more information\n","funding_links":[],"categories":["Web Frameworks","Web框架","Libraries for creating HTTP middlewares","中间件### 中间件","中间件"],"sub_categories":["Middlewares","中间件","创建http中间件的代码库","Fail injection"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHereMobilityDevelopers%2Fmediary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2FHereMobilityDevelopers%2Fmediary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2FHereMobilityDevelopers%2Fmediary/lists"}