{"id":16002756,"url":"https://github.com/soreing/gent","last_synced_at":"2025-04-05T01:26:13.265Z","repository":{"id":186595906,"uuid":"675124024","full_name":"Soreing/gent","owner":"Soreing","description":"Golang HTTP Client wrapper with additional features, flexibility, modular design","archived":false,"fork":false,"pushed_at":"2024-05-26T22:56:21.000Z","size":77,"stargazers_count":0,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-02-10T09:43:42.767Z","etag":null,"topics":["go","golang","http","http-client","middleware","modular","retrier"],"latest_commit_sha":null,"homepage":"","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/Soreing.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":"2023-08-05T21:30:06.000Z","updated_at":"2024-05-26T22:55:00.000Z","dependencies_parsed_at":"2024-10-30T14:40:41.746Z","dependency_job_id":"198f48f6-32bc-4441-a2fe-3afaaf9d45ac","html_url":"https://github.com/Soreing/gent","commit_stats":null,"previous_names":["soreing/gent"],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fgent","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fgent/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fgent/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Soreing%2Fgent/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Soreing","download_url":"https://codeload.github.com/Soreing/gent/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247274093,"owners_count":20912048,"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","middleware","modular","retrier"],"created_at":"2024-10-08T10:03:37.162Z","updated_at":"2025-04-05T01:26:13.238Z","avatar_url":"https://github.com/Soreing.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Go HTTP Client\n\n![Build](https://github.com/soreing/gent/actions/workflows/build_status.yaml/badge.svg)\n![Coverage](https://img.shields.io/endpoint?url=https://gist.githubusercontent.com/Soreing/4b6f950f01f3e6e5b9ed17b268664538/raw/gent)\n[![Go Report Card](https://goreportcard.com/badge/github.com/Soreing/gent)](https://goreportcard.com/report/github.com/Soreing/gent)\n[![Go Reference](https://pkg.go.dev/badge/github.com/Soreing/gent.svg)](https://pkg.go.dev/github.com/Soreing/gent)\n\nGent is a Golang HTTP Client wrapper that provides additional features for flexibility and increased performance.\n\n## Usage\n\nCreate a Client that lets you to make requests. The constructor accepts a list of options to customize it.\n\n```golang\n// Default client\ncl := gent.NewClient()\n```\n\nTo make requests, use the functions named after HTTP methods. The following example sends a POST request to `http://localhost:8080/employees/create` with an application/json body containing an employee id and name, an Authorization header, and a query parameter set to true.\n\n```golang\nres, err := cl.Post(\n    context.Background(),\n    \"http://localhost:8080/{}/{}\",\n    map[string]string{\n        \"id\": \"4481e035-1711-419f-82bc-bfb72da06375\",\n        \"name\": \"John Smith\",\n    },\n    gent.NewJSONMarshaler(),\n    map[string]string{\n \t\t\"Authorization\": \"Bearer x.y.z\",\n\t},\n    map[string][]string{\n \t\t\"strict\":    {\"true\"},\n \t},\n    \"employees\",\n    \"create\",\n)\n```\n### Request Builder ⭒NEW⭒ \nRequests can be constructed gradually with a request builder. Request require \nat least an HTTP method and an endpoint, the other parameters are optional.\n```golang\nres, err := cl.NewRequest(\n    http.MethodGet, \"http://localhost:8080/{}/{}\",\n).WithBody(\n    map[string]string{\n        \"id\": \"4481e035-1711-419f-82bc-bfb72da06375\",\n        \"name\": \"John Smith\",\n    },\n    gent.NewJSONMarshaler(),\n).WithHeaders(\n    map[string]string{\"Authorization\": \"Bearer x.y.z\"},\n).WithQueryParameters(\n    map[string][]string{\"strict\": {\"true\"},},\n).WithPathParameters(\n    \"employees\", \"create\",\n).Run(context.Background())\n```\n\n\n### Placeholders\n\nThe request's endpoint supports placeholders in the form of `{}`. Placeholders will be populated by the trailing variadic path parameters that get escaped before replacing the placeholders in the order they were provided.\n\n### Request Body\n\nAny data can be provided as a request body, and it is up to the marshaler to transform it into a byte array for the request. By default the package supports JSON, XML and Form marshalers. If you do not require a request body, leave both the body and the marshaler nil.\n\nMarshalers must implement `Marshal(body any) (data []byte, content string, err error)`, which take any input and return a byte array data, a content type for the header and any error. The following is the implementation of the JSON Marshaler for reference.\n\n```golang\ntype jsonMarshaler struct{}\n\nfunc (m *jsonMarshaler) Marshal(\n    v any,\n) (dat []byte, t string, err error) {\n\tt = \"application/json\"\n\tdat, err = json.Marshal(v)\n\treturn\n}\n```\n\n### Query Parameters\n\nQuery parameters are constructed from a map and include a `?` if there is at least one query parameter provided. It is recommended to add query parameters via the map, as they get escaped. Parameters support arrays, which get added in the following format: \n\n```golang\nmap[string][]string{\n    // ?ids=123\u0026ids=456\u0026ids=789\n    \"ids\": {\"123\", \"456\", \"789\"}\n}\n```\n\n## Options and Modules\n\nThe Client can be configured during creation with a variety of options.\n```golang\ncl := gent.NewClient(\n    /* ... Option */\n)\n```\n\n### HTTP Client\n\nThe client internally uses the default HTTP Client to make requests. This can be changed with the `UseHttpClient` option. The default behavior is to reuse clients between requests. This can also be changed by providing a constructor function that returns a new client for each request.\n\n```golang\n// Client that uses a new HTTP client\ncl := gent.NewClient(\n    gent.UseHttpClient(\u0026http.Client{}),\n)\n```\n\n```golang\n// Client that creates a new HTTP client for each request\ncl := gent.NewClient(\n    gent.UseHttpClientConstructor(func() gent.HttpClient{\n        return \u0026http.Client{}\n    }),\n)\n```\n\n### Memory Pool\n\nEach client uses a memory pool internally to speed up string operations by reusing memory allocations. By default, each client creates its own memory pool with default page sizes and pool sizes. A pre-configured memory pool can be provided to and even shared between clients.\n\n```golang\ncl := gent.NewClient(\n    gent.UseMemoryPool(gent.NewMemPool(\n        512, // Page size in bytes\n        200, // Pool size in pages\n    )),\n)\n```\n\nYou can provide your own implementation of memory pool if it implements how to acquire byte arrays from the pool with `Acquire() []byte` and release byte arrays into the pool with `Release(...[]byte)`\n\n### Middlewares\n\nClients can be given middlewares that are executed for each request. Middlewares can be \nadded to two stages. BeforeBuild middlewares run before the http.Request object is created,\nwhile BeforeExecute middlewares run before the request is sent.\n\nTo add a middleware, use the Use function on the client.\n```golang\ncl := gent.NewClient()\n\n// Middleware that adds an auth header to the request\ncl.Use(\n    gent.MDW_BeforeBuild, \n    func(c context.Context, r *gent.Request) {\n        r.Headers[\"Authorization\"] = \"Bearer x.y.z\"\n        r.Next()\n    },\n)\n```","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoreing%2Fgent","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoreing%2Fgent","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoreing%2Fgent/lists"}