All Projects → gocanto → Http Client

gocanto / Http Client

Licence: mit
Http client that handles retries, logging & dynamic headers.

Projects that are alternatives of or similar to Http Client

Dart Sip Ua
A dart-lang version of the SIP UA stack.
Stars: ✭ 132 (-8.33%)
Mutual labels:  client
Amqp
AMQP 1.0 client library for Go.
Stars: ✭ 135 (-6.25%)
Mutual labels:  client
Python Twitch Client
Python wrapper for Twitch API
Stars: ✭ 137 (-4.86%)
Mutual labels:  client
Jstp
Fast RPC for browser and Node.js based on TCP, WebSocket, and MDSF
Stars: ✭ 132 (-8.33%)
Mutual labels:  client
Foundationdb Rs
FoundationDB Rust client api
Stars: ✭ 134 (-6.94%)
Mutual labels:  client
Parity Zcash
Rust implementation of Zcash protocol
Stars: ✭ 136 (-5.56%)
Mutual labels:  client
Nakamoto
Bitcoin light-client implementation in Rust
Stars: ✭ 129 (-10.42%)
Mutual labels:  client
Faunadb Go
Go driver for FaunaDB
Stars: ✭ 140 (-2.78%)
Mutual labels:  client
Sengi
Mastodon & Pleroma Multi-account Desktop Client
Stars: ✭ 133 (-7.64%)
Mutual labels:  client
Amadeus Ws Client
PHP Amadeus SOAP Web Service client library
Stars: ✭ 136 (-5.56%)
Mutual labels:  client
Pyrogram
Telegram MTProto API Client Library and Framework in Pure Python for Users and Bots
Stars: ✭ 2,252 (+1463.89%)
Mutual labels:  client
Py3 Pinterest
Fully fledged Python Pinterest client
Stars: ✭ 133 (-7.64%)
Mutual labels:  client
Notion Js
🤯 Notion API
Stars: ✭ 136 (-5.56%)
Mutual labels:  client
Spreadsheet server
A python server harnessing the calculational ability of LibreOffice Calc (thanks to 'pyoo'). It provides 'instant' access to the cell ranges of a set of spreadsheets.
Stars: ✭ 132 (-8.33%)
Mutual labels:  client
Microsoft Todo Mac
🐜 Microsoft-ToDo macOS App. 微软Todo mac客户端.
Stars: ✭ 138 (-4.17%)
Mutual labels:  client
Madonctl
CLI client for the Mastodon social network API
Stars: ✭ 129 (-10.42%)
Mutual labels:  client
Fdroidcl
F-Droid desktop client
Stars: ✭ 135 (-6.25%)
Mutual labels:  client
Graphlient
Ruby GraphQL Client
Stars: ✭ 142 (-1.39%)
Mutual labels:  client
Pipedrive
Complete Pipedrive API client for PHP
Stars: ✭ 138 (-4.17%)
Mutual labels:  client
Libsmb2
SMB2/3 userspace client
Stars: ✭ 136 (-5.56%)
Mutual labels:  client

About it

Total Downloads Latest Stable Version Build status

This library is a wrapper around the famous Guzzle HTTP client with some goodies on top of it.

This client gives you the ability to perform retries and log any request information you may need.

Installation

This library uses Composer to manage its dependencies. So, before using it, make sure you have it installed in your machine. Once you have done this, you will be able to pull this library in by typing the following command in your terminal.

composer require gocanto/http-client

The reason behind

Sometimes 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.

To 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.

Therefore, I have created this small wrapper to handle retries actions and to log our requests/responses payload within the same action.

How does it work?

The 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.

Let'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.

use GuzzleHttp\Client;

$response = (new Client)->get('http://foo.com'); 

This 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?

Well, if you are anything like me, you would do something like:

use GuzzleHttp\Client;
use GuzzleHttp\Exception\RequestException;

$retry = 1;
$response = null;

do {
    try {
        $response = (new Client)->get('http://foo.com');
    } catch (RequestException $e) {
        $retry++;
    }
} while ($response === null && $retry <= 5);

There 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.

We can do better!

By using the HTTP client shipped within this package, you will be able to call the retry mechanism within the same HTTP call. For Example:"

use Gocanto\HttpClient\HttpClient;
use GuzzleHttp\Exception\RequestException;

try {
    $response = (new HttpClient)->retry(5)->get('http://foo.com'); 
} catch (RequestException $e) {
    //you need to still handle errors here!
}

This 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.

You would be able to use it like so:

use Gocanto\HttpClient\HttpClient;
use GuzzleHttp\Exception\RequestException;

try {
    $response = (new HttpClient)->onRetry(function () {})->get('http://foo.com'); 
} catch (RequestException $e) {
    //you need to still handle errors here!
}

Here, you will be given the incoming request and response that you are handling in that particular moment. see more

On-Demand Headers

Sometimes, we need to add headers to a given client instance based on dynamic data.

Such 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.

This client supports this functionality through the with Headers to allow on-demand headers values whenever needed.

For instance, you would be able to do something like so:

$client = new HttpClient;

$client->withHeaders([
      'X-GUS-1' => 'testing testing',
      'X-GUS-2' => 'testing testing',
      'X-GUS-3' => 'testing testing',
      'X-GUS-4' => 'testing testing',
])->request('GET', 'https://foo.com');

to populate as many headers as needed.

Contributing

Please feel free to fork this package and contribute by submitting a pull request to enhance its functionality.

License

The MIT License (MIT). Please see License File for more information.

How can I thank you?

Why not star the github repo and share the link for this repository on Twitter?

Don't forget to follow me on twitter!

Thanks!

Gustavo Ocanto.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].