{"id":17473877,"url":"https://github.com/gocanto/http-client","last_synced_at":"2025-04-05T08:09:43.962Z","repository":{"id":56361841,"uuid":"194623705","full_name":"gocanto/http-client","owner":"gocanto","description":"Http client that handles retries, logging \u0026 dynamic headers.","archived":false,"fork":false,"pushed_at":"2024-09-04T04:08:04.000Z","size":48,"stargazers_count":143,"open_issues_count":0,"forks_count":4,"subscribers_count":7,"default_branch":"master","last_synced_at":"2024-10-18T20:46:53.684Z","etag":null,"topics":["client","http","php"],"latest_commit_sha":null,"homepage":"","language":"PHP","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/gocanto.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","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},"funding":{"github":"gocanto","custom":"https://github.com/sponsors/gocanto"}},"created_at":"2019-07-01T07:40:59.000Z","updated_at":"2024-09-04T04:08:01.000Z","dependencies_parsed_at":"2024-10-30T21:04:11.420Z","dependency_job_id":"205570d7-f6f1-4853-a3bb-a840e59c7e59","html_url":"https://github.com/gocanto/http-client","commit_stats":{"total_commits":56,"total_committers":6,"mean_commits":9.333333333333334,"dds":0.2321428571428571,"last_synced_commit":"5b566c5865b28e5ca9166e1dad41ebad7d37ecf9"},"previous_names":[],"tags_count":4,"template":false,"template_full_name":"gocanto/php-template","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gocanto%2Fhttp-client","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gocanto%2Fhttp-client/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gocanto%2Fhttp-client/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/gocanto%2Fhttp-client/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/gocanto","download_url":"https://codeload.github.com/gocanto/http-client/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247305935,"owners_count":20917208,"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":["client","http","php"],"created_at":"2024-10-18T18:07:33.164Z","updated_at":"2025-04-05T08:09:43.939Z","avatar_url":"https://github.com/gocanto.png","language":"PHP","readme":"## About it\n\n\u003ca href=\"https://packagist.org/packages/gocanto/http-client\"\u003e\u003cimg src=\"https://img.shields.io/packagist/dt/gocanto/http-client.svg?style=flat-square\" alt=\"Total Downloads\"\u003e\u003c/a\u003e\n\u003ca href=\"https://packagist.org/packages/gocanto/http-client\"\u003e\u003cimg src=\"https://img.shields.io/github/v/release/gocanto/http-client.svg?style=flat-square\" alt=\"Latest Stable Version\"\u003e\u003c/a\u003e\n\u003ca href=\"https://travis-ci.org/gocanto/http-client\"\u003e\u003cimg src=\"https://img.shields.io/travis/gocanto/http-client/master.svg?style=flat-square\" alt=\"Build status\"\u003e\u003c/a\u003e\n\nThis library is a wrapper around the famous Guzzle `HTTP` client with some goodies on top of it. \n\nThis client gives you the ability to perform `retries` and `log` any request information you may need. \n\n## Installation\n\nThis library uses [Composer](https://getcomposer.org) to manage its dependencies. So, before using it, make sure you have it installed in your machine. \nOnce you have done this, you will be able to pull this library in by typing the following command in your terminal.\n\n```\ncomposer require gocanto/http-client\n```\n\n## The reason behind\n\nSometimes you will need the ability to retry a HTTP request for some given reason. This can be either due to latency issues or some timeout errors. \n\nTo avoid this interruptions, we usually perform a retry action inside a given loop. Then, we either break the loop because we have a valid response to return with or because we need to handle possible errors. \n\nTherefore, I have created this small wrapper to handle retries actions and to log our requests/responses payload within the same action.\n\n## How does it work? \n\nThe way how this client works is exactly the same as the one you have been used to it. You will be able to call any known guzzle method and attach the `retry` action to them.\n\nLet's say you need to perform some HTTP request to fill seed your db with some data. To do so, you would have to write some code along these lines.\n\n```php\nuse GuzzleHttp\\Client;\n\n$response = (new Client)-\u003eget('http://foo.com'); \n```\n\nThis is a simple use case that we all have came across some other time. But, What would you do if there is an error and you need to handle some retries?\n\nWell, if you are anything like me, you would do something like:\n\n```php\nuse GuzzleHttp\\Client;\nuse GuzzleHttp\\Exception\\RequestException;\n\n$retry = 1;\n$response = null;\n\ndo {\n    try {\n        $response = (new Client)-\u003eget('http://foo.com');\n    } catch (RequestException $e) {\n        $retry++;\n    }\n} while ($response === null \u0026\u0026 $retry \u003c= 5);\n```\n\nThere you have a working code, but you will have to do the same procedure every time you need to perform some kind of HTTP request. \n\n\u003e We can do better!\n\nBy using the HTTP client shipped within this package, you will be able to call the retry mechanism within the same HTTP call. For Example:\"\n\n```php\nuse Gocanto\\HttpClient\\HttpClient;\nuse GuzzleHttp\\Exception\\RequestException;\n\ntry {\n    $response = (new HttpClient)-\u003eretry(5)-\u003eget('http://foo.com'); \n} catch (RequestException $e) {\n    //you need to still handle errors here!\n}\n```\n\nThis line of code does exactly the same as the ones above, but more efficient and elegant. This library also ships a different method `onRetry()` that performs the same retries, but it also gives the ability to hook into the retry call. \n\nYou would be able to use it like so: \n\n```php\nuse Gocanto\\HttpClient\\HttpClient;\nuse GuzzleHttp\\Exception\\RequestException;\n\ntry {\n    $response = (new HttpClient)-\u003eonRetry(function () {})-\u003eget('http://foo.com'); \n} catch (RequestException $e) {\n    //you need to still handle errors here!\n}\n```\n\nHere, you will be given the incoming request and response that you are handling in that particular moment. [see more](https://github.com/gocanto/http-client/blob/master/src/HttpClient.php#L82)\n\n## On-Demand Headers\n\nSometimes, we need to add headers to a given `client` instance based on dynamic data. \n\nSuch as requirement is not possible on the creation stage because we do not know what information we would be dealing with. Therefore, we need a mechanism to hook into and populate this data when needed. \n\nThis `client` supports this functionality through the `with Headers` to allow on-demand headers values whenever needed.\n\nFor instance, you would be able to do something like so:\n\n```php\n$client = new HttpClient;\n\n$client-\u003ewithHeaders([\n      'X-GUS-1' =\u003e 'testing testing',\n      'X-GUS-2' =\u003e 'testing testing',\n      'X-GUS-3' =\u003e 'testing testing',\n      'X-GUS-4' =\u003e 'testing testing',\n])-\u003erequest('GET', 'https://foo.com');\n```\n\nto populate as many headers as needed. \n\n\n## Contributing\n\nPlease feel free to fork this package and contribute by submitting a pull request to enhance its functionality.\n\n## License\n\nThe MIT License (MIT). Please see [License File](https://github.com/gocanto/http-client/blob/master/LICENSE.md) for more information.\n\n\n## How can I thank you?\n\nThere are many ways you would be able to support my open source work. There is not a right one to choose, so the choice is yours.\n\nNevertheless :grinning:, I would propose the following\n\n- :arrow_up: Follow me on [Twitter](https://twitter.com/gocanto).\n- :star: Star the repository.\n- :handshake: Open a pull request to fix/improve the codebase.\n- :writing_hand: Open a pull request to improve the documentation.\n- :coffee: Buy me a [coffee](https://github.com/sponsors/gocanto)?\n\n\u003e Thank you for reading this far. :blush:\n","funding_links":["https://github.com/sponsors/gocanto","https://github.com/sponsors/gocanto)?"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgocanto%2Fhttp-client","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fgocanto%2Fhttp-client","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fgocanto%2Fhttp-client/lists"}