{"id":15407376,"url":"https://github.com/izumin5210/hx","last_synced_at":"2025-04-18T03:20:05.204Z","repository":{"id":69098943,"uuid":"219305192","full_name":"izumin5210/hx","owner":"izumin5210","description":"🌏 Developer-friendly, Real-World-ready and extensible HTTP client for Go","archived":false,"fork":false,"pushed_at":"2019-12-01T23:17:49.000Z","size":121,"stargazers_count":12,"open_issues_count":1,"forks_count":2,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-05-21T11:11:53.408Z","etag":null,"topics":["go","golang","http","http-client"],"latest_commit_sha":null,"homepage":"https://godoc.org/github.com/izumin5210/hx","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/izumin5210.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,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2019-11-03T13:29:03.000Z","updated_at":"2024-06-19T03:59:42.819Z","dependencies_parsed_at":null,"dependency_job_id":"73824c6d-c1f9-4023-8858-68819eaf3568","html_url":"https://github.com/izumin5210/hx","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izumin5210%2Fhx","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izumin5210%2Fhx/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izumin5210%2Fhx/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/izumin5210%2Fhx/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/izumin5210","download_url":"https://codeload.github.com/izumin5210/hx/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":249419756,"owners_count":21268692,"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":["go","golang","http","http-client"],"created_at":"2024-10-01T16:28:36.147Z","updated_at":"2025-04-18T03:20:05.146Z","avatar_url":"https://github.com/izumin5210.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# hx\n[![CI](https://github.com/izumin5210/hx/workflows/CI/badge.svg)](https://github.com/izumin5210/hx/actions?workflow=CI)\n[![GoDoc](https://godoc.org/github.com/izumin5210/hx?status.svg)](https://godoc.org/github.com/izumin5210/hx)\n[![codecov](https://codecov.io/gh/izumin5210/hx/branch/master/graph/badge.svg)](https://codecov.io/gh/izumin5210/hx)\n[![License](https://img.shields.io/github/license/izumin5210/hx)](./LICENSE)\n\nDeveloper-friendly, Production-ready and extensible HTTP client for Go\n\n## Features\n\n...\n\n\n### Plugins\n\n- [hxlog](./plugins/hxlog) - Logging requests and responses with standard logger\n- [hxlog](./plugins/hxzap) - Logging requests and responses with [zap](https://github.com/uber-go/zap)\n- [pb](./plugins/pb) - Marshaling and Unmarshaling protocol buffers\n- [retry](./plugins/retry) - Retrying HTTP requests\n\n## Examples\n### Simple GET\n\n```go\ntype Content struct {\n\tBody string `json:\"body\"`\n}\n\nvar cont Content\n\nctx := context.Background()\nerr := hx.Get(ctx, \"https://api.example.com/contents/1\",\n\thx.WhenSuccess(hx.AsJSON(\u0026cont)),\n\thx.WhenFailure(hx.AsError()),\n)\n```\n\n### Real-world\n\n```go\nfunc init() {\n\tdefaultTransport := hxutil.CloneTransport(http.DefaultTransport.(*http.Transport))\n\n\t// Tweak keep-alive configuration\n\tdefaultTransport.MaxIdleConns = 500\n\tdefaultTransport.MaxIdleConnsPerHost = 100\n\n\t// Set global options\n\thx.DefaultOptions = append(\n\t\thx.DefaultOptions,\n\t\thx.UserAgent(fmt.Sprintf(\"yourapp (%s)\", hx.DefaultUserAgent)),\n\t\thx.Transport(defaultTransport),\n\t\thx.TransportFrom(func(rt http.RoundTripper) http.RoundTripper {\n\t\t\treturn \u0026ochttp.Transport{Base: rt}\n\t\t}),\n\t)\n}\n\nfunc NewContentAPI() *hx.Client {\n\t// Set common options for API ciient\n\treturn \u0026ContentAPI{\n\t\tclient: hx.NewClient(\n\t\t\thx.BaseURL(\"https://api.example.com\"),\n\t\t),\n\t}\n}\n\ntype ContentAPI struct {\n\tclient *hx.Client\n}\n\nfunc (a *ContentAPI) GetContent(ctx context.Context, id int) (*Content, error) {\n\tvar cont Content\n\n\terr := a.client.Get(ctx, hx.Path(\"api\", \"contents\", id),\n\t\thx.WhenSuccess(hx.AsJSON(\u0026cont)),\n\t\thx.WhenFailure(hx.AsError()),\n\t)\n\n\tif err != nil {\n\t\t// ...\n\t}\n\n\treturn \u0026cont, nil\n}\n\nfunc (a *ContentAPI) CreateContent(ctx context.Context, in *Content) (*Content, error) {\n\tvar out Content\n\n\terr := a.client.Post(ctx, \"/api/contents\",\n\t\thx.JSON(in),\n\t\thx.WhenSuccess(hx.AsJSON(\u0026out)),\n\t\thx.WhenStatus(hx.AsJSONError(\u0026InvalidArgument{}), http.StatusBadRequest),\n\t\thx.WhenFailure(hx.AsError()),\n\t)\n\n\tif err != nil {\n\t\tvar (\n\t\t\tinvalidArgErr *InvalidArgument\n\t\t\trespErr       *hx.ResponseError\n\t\t)\n\t\tif errors.As(err, \u0026invalidArgErr) {\n\t\t\t// handle known error\n\t\t} else if errors.As(err, \u0026respErr) {\n\t\t\t// handle unknown response error\n\t\t} else {\n\t\t\terr := errors.Unwrap(err)\n\t\t\t// handle unknown error\n\t\t}\n\t}\n\n\treturn \u0026out, nil\n}\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizumin5210%2Fhx","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fizumin5210%2Fhx","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fizumin5210%2Fhx/lists"}