2017 © Pedro Peláez
 

library hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

image

jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  • Thursday, June 14, 2018
  • by jsor
  • Repository
  • 2 Watchers
  • 12 Stars
  • 5,531 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 4 Forks
  • 0 Open issues
  • 12 Versions
  • 5 % Grown

The README.md

HalClient

A lightweight PHP client for consuming and manipulating Hypertext Application Language (HAL) resources., (*1)

Build Status Coverage Status, (*2)

Installation

Install the latest version with Composer., (*3)

composer require jsor/hal-client

Check the Packagist page for all available versions., (*4)

HTTP Client dependency

The Hal client requires a HttpClientInterface implementation which can handle PSR-7 requests and responses., (*5)

To use the default implementations shipped with this library, you need to install Guzzle 7, 6 or 5., (*6)

composer require guzzlehttp/guzzle:"^5.0||^6.0||^7.0"

Usage

We will use Propilex as an example API endpoint., (*7)

Create the client

At a first step, we setup a HalClient instance., (*8)

use Jsor\HalClient\HalClient;

$client = new HalClient('http://propilex.herokuapp.com');

We can now set additional headers (eg. an Authorization header) which are sent with every request., (*9)

$client = $client->withHeader('Authorization', 'Bearer 12345');

Note, that a client instance is immutable, which means, any call to change the state of the instance returns a new instance leaving the original instance unchanged., (*10)

// Wrong!
$client->withHeader('Authorization', '...');
$resource = $client->get('/protected');

// Correct!
$client = $client->withHeader('Authorization', '...');
$resource = $client->get('/protected');

Browse the API

To start browsing through the API, we first get the root resource., (*11)

/** @var \Jsor\HalClient\HalResource $rootResource */
$rootResource = $client->root();

We now follow the p:documents link., (*12)

/** @var \Jsor\HalClient\HalLink $documentsLink */
$documentsLink = $rootResource->getFirstLink('documents');

$documentsResource = $documentsLink->get();

$totalDocuments = $documentsResource->getProperty('total');

foreach ($resource->getResource('documents') as $document) {
    echo $document->getProperty('title') . PHP_EOL;
}

If there is a second page with more documents, we can follow the next link., (*13)

if ($documentsResource->hasLink('next')) {
    $nextDocumentsResource = $documentsResource->getFirstLink('next')->get();
}

Ok, let's create a new document., (*14)

$newDocument = $documentsResource->post([
    'body' => [
        'title' => 'Sampl document',
        'body'  => 'Lorem ipsum'
    ]
]);

Oh noes! A typo in the document title. Let's fix it., (*15)

$changedDocument = $newDocument->put([
    'body' => [
        'title' => 'Sampe document',
        'body'  => $newDocument->getProperty('body')
    ]
]);

Damn, we give up., (*16)

$changedDocument->delete();

License

Copyright (c) 2015-2021 Jan Sorgalla. Released under the MIT License., (*17)

The Versions

14/06 2018

dev-master

9999999-dev https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

10/03 2017

v3.4.2

3.4.2.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

18/10 2016

v3.4.1

3.4.1.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

06/05 2016

v3.4.0

3.4.0.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

14/03 2016

v3.3.0

3.3.0.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

02/02 2016

v3.2.0

3.2.0.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

01/02 2016

v3.1.0

3.1.0.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

16/01 2016

v3.0.0

3.0.0.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

28/10 2015

v2.0.2

2.0.2.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

28/10 2015

v2.0.1

2.0.1.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

12/10 2015

v2.0.0

2.0.0.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client

09/10 2015

v1.0.0

1.0.0.0 https://github.com/jsor/hal-client

A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

  Sources   Download

MIT

The Requires

 

The Development Requires

api json rest http hal client