All Projects → jsor → hal-client

jsor / hal-client

Licence: MIT license
A lightweight client for consuming and manipulating Hypertext Application Language (HAL) resources.

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to hal-client

ng-hal
A navigator for HAL documents in Angular
Stars: ✭ 24 (+14.29%)
Mutual labels:  hal, hypertext-application-language
php-serializer
Serialize PHP variables, including objects, in any format. Support to unserialize it too.
Stars: ✭ 47 (+123.81%)
Mutual labels:  hal
skynet robot control rtos ethercat
Realtime 6-axis robot controller, based on Qt C++ & OpenCascade & KDL kinematics & HAL
Stars: ✭ 41 (+95.24%)
Mutual labels:  hal
hal-api
Enhances your HATEOAS experience by automating common tasks.
Stars: ✭ 32 (+52.38%)
Mutual labels:  hal
HAL-Webinar
Webinar – Creating a Hardware Abstraction Layer in LabVIEW
Stars: ✭ 22 (+4.76%)
Mutual labels:  hal
Core
The server component of API Platform: hypermedia and GraphQL APIs in minutes
Stars: ✭ 2,004 (+9442.86%)
Mutual labels:  hal
bxcan
bxCAN peripheral driver for STM32 chips
Stars: ✭ 22 (+4.76%)
Mutual labels:  hal
jesi
Hypermedia API Accelerator
Stars: ✭ 19 (-9.52%)
Mutual labels:  hal
hal stm32
No description or website provided.
Stars: ✭ 56 (+166.67%)
Mutual labels:  hal
mezzio-hal
Hypertext Application Language implementation for PHP and PSR-7
Stars: ✭ 19 (-9.52%)
Mutual labels:  hal
halfred
A parser for application/hal+json
Stars: ✭ 44 (+109.52%)
Mutual labels:  hal
browser
A HAL browser middleware for node.js
Stars: ✭ 39 (+85.71%)
Mutual labels:  hal
STM32 HAL FREEMODBUS RTU
FreeMODBUS RTU port for STM32 HAL library
Stars: ✭ 111 (+428.57%)
Mutual labels:  hal
php-hal
HAL+JSON & HAL+XML API transformer outputting valid (PSR-7) API Responses.
Stars: ✭ 30 (+42.86%)
Mutual labels:  hal
bowman
A Java library for accessing a JSON+HAL REST API
Stars: ✭ 45 (+114.29%)
Mutual labels:  hal
console
HAL management console
Stars: ✭ 41 (+95.24%)
Mutual labels:  hal
Eqmac
macOS System-wide Audio Equalizer & Volume Mixer 🎧
Stars: ✭ 3,947 (+18695.24%)
Mutual labels:  hal
HX711
HX711 driver for STM32 HAL
Stars: ✭ 34 (+61.9%)
Mutual labels:  hal
akka-http-hal
HAL (Hypermedia Application Language) specification support for akka-http
Stars: ✭ 18 (-14.29%)
Mutual labels:  hal
machinekit-hal
Universal framework for machine control based on Hardware Abstraction Layer principle
Stars: ✭ 89 (+323.81%)
Mutual labels:  hal

HalClient

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

Build Status Coverage Status

Installation

Install the latest version with Composer.

composer require jsor/hal-client

Check the Packagist page for all available versions.

HTTP Client dependency

The Hal client requires a HttpClientInterface implementation which can handle PSR-7 requests and responses.

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

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

Usage

We will use Propilex as an example API endpoint.

Create the client

At a first step, we setup a HalClient instance.

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.

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

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

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

We now follow the p:documents link.

/** @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.

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

Ok, let's create a new document.

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

Oh noes! A typo in the document title. Let's fix it.

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

Damn, we give up.

$changedDocument->delete();

License

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

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