ajayvohra2005/hack-http-client

HTTP client implementation in Hack language

v0.9.0 2021-11-05 00:58 UTC

This package is auto-updated.

Last update: 2024-04-05 06:43:16 UTC


README

Overview

This project implements an HTTP Client in Hack.

  • Simple HTTP Client interface
  • Synchronous and asynchronous requests
  • Middleware support

Requirements

HHVM 4.132 and above.

Installation

  • Git clone this repository

  • Install composer

  • In the root directory of this repository, run the command

      composer install
    

To use this package,

    composer require ajayvohra2005/hack-http

Running Tests

Testing uses a mock HTTP server written in Node.js. The project was tested with Node.js version v17.0.1. After installation of Node.js and this project, run the following command in the root directory of this repository:

    ./vendor/bin/hacktest tests/

Quick Start Example

use namespace HackHttp\Message as HM;
use namespace HackHttp\Client as HC;

<<__EntryPoint>>
function quick_start(): void 
{
  require_once(__DIR__.'/../vendor/autoload.hack');
  \Facebook\AutoloadMap\initialize();

  $client = new HC\Client();
  // Send a synchronous request.
  $response = $client->request('GET', 'https://docs.hhvm.com/hack/');

  echo $response->getStatusCode(); // 200
  echo $response->getHeaderLine('content-type'); // 'text/html'
  echo $response->getBody()->__toString(); // <!DOCTYPE html><html>...

  // Send an asynchronous request.
  $request = new HM\Request('GET', 'http://httpbin.org');
  $promise = $client->sendAsync($request)->then( (mixed $response): void ==> {
      if($response is HM\Response) {
        echo 'I completed! ' . $response->getBody()->__toString();
      }
  });

  $promise->wait();  
}

License

Hack Http package is made available under the MIT License (MIT). Please see License File for more information.

Acknowledgements

The Guzzle, PHP HTTP Client and PSR-7 Message Implementation projects inspired this code.