GithubHelp home page GithubHelp logo

isabella232 / hal-client Goto Github PK

View Code? Open in Web Editor NEW

This project forked from masbug/hal-client

0.0 0.0 0.0 108 KB

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

License: MIT License

PHP 100.00%

hal-client's Introduction

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 6 or Guzzle 5.

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

URI template handling

In order to expand URI templates in HAL links, you must either provide a global \uri_template function (e.g. by installing the uri_template extension) or install the guzzlehttp/guzzle package (version ^5.0 or ^6.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-2020 Jan Sorgalla. Released under the MIT License.

hal-client's People

Contributors

jsor avatar habl avatar masbug 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.