/rest

REST Client and Server APIs for the XP Framework

Primary LanguagePHP

REST Client and Server APIs for the XP Framework

Build Status on TravisCI XP Framework Module BSD Licence Required PHP 5.4+ Supports PHP 7.0+ Required HHVM 3.4+ Latest Stable Version

Client

Entry point

The RestClient class serves as the entry point to this API. Create a new instance of it with the REST service's endpoint URL and then invoke its execute() method to work with the resources.

Example

Here's an overview of the typical usage for working with the REST API.

use webservices\rest\RestClient;
use webservices\rest\RestRequest;
use peer\http\HttpConstants;

$client= new RestClient('http://api.example.com/');

$request= (new RestRequest('/resource/{id}'))
 ->withMethod(HttpConstants::GET)
 ->withSegment('id', 5000)
 ->withParameter('details', 'true')
 ->withHeader('X-Binford', '6100 (more power)'
;

$response= $client->execute($request);
$content= $response->content();            // Raw data as string
$value= $response->data();                 // Deserialize to map

Deserialization

The REST API supports automatic result deserialization by passing a lang.Type instance to the data() method.

$resource= $response->data('com.example.api.types.Person');

Authentication

Basic authentication is supported by embedding the credentials in the endpoint URL:

use webservices\rest\RestClient;

$client= new RestClient('http://user:pass@api.example.com/');