Coder Social home page Coder Social logo

harmony-http-client-php's Introduction

Http-Client

Latest Stable Version Build Status Build status Coverage Status License

Async caching aware http client

Requirements

  • PHP 7.3+
  • Redis (if wanting to use the Redis caching provider)

In addition for non-blocking context one of the following event libraries should be installed:

Installation

composer require harmonyio/http-client

Usage

Caching requests

The following example shows how to make a request and cache the result so consecutive calls will used the cached results instead of making a new external HTTP call.

<?php declare(strict_types=1);

namespace Foo;

use Amp\Http\Client\HttpClientBuilder;
use HarmonyIO\Cache\Provider\InMemory;
use HarmonyIO\Cache\Ttl;
use HarmonyIO\HttpClient\Client\HttpClient;
use HarmonyIO\HttpClient\Message\CachingRequest;
use function Amp\Promise\wait;

// create instance of the HTTP client
$httpClient = new HttpClient(
    HttpClientBuilder::buildDefault(),
    new InMemory()
);

// create a new request to be cached for 10 seconds
$request = new CachingRequest('TestRequestKey', 'https://httpbin.org/get', 'GET', new Ttl(10));

// make the request and cache it
$result = wait($httpClient->request($request));

// all consecutive requests will now used the cached result instead of calling the external service again
$result = wait($httpClient->request($request));

Non-caching requests

It is also possible to make non-caching requests.

<?php declare(strict_types=1);

namespace Foo;

use Amp\Http\Client\HttpClientBuilder;
use HarmonyIO\Cache\Provider\InMemory;
use HarmonyIO\HttpClient\Client\HttpClient;
use Amp\Http\Client\Request;
use function Amp\Promise\wait;

// create instance of the HTTP client
$httpClient = new HttpClient(
    HttpClientBuilder::buildDefault(),
    new InMemory()
);

// create a new request to be cached for 10 seconds
$request = new Request('https://httpbin.org/get');

// make the request (the results will NOT be cache)
$result = wait($httpClient->request($request));

// make the same request again
$result = wait($httpClient->request($request));

HarmonyIO\HttpClient\Message\CachingRequest

The constructor of the caching request class expects at least a key, a TTL, the URL to make the request to and optionally an HTTP method (defaults to GET).

Optional request parts can be set in setter method of the request class as defined in the previous section.

<?php declare(strict_types=1);

namespace Foo;

use HarmonyIO\HttpClient\Message\CachingRequest;

$cachingRequest = new CachingRequest('UniqueCachingKey', 'https://httpbin.org/get');
$request = $cachingRequest->getRequest();
$request->setProtocolVersions(['1.1', '2.0']);
$request->addHeader('foo', 'bar');
$request->addHeader('baz', 'qux');

harmony-http-client-php's People

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.