项目作者: xp-forge

项目描述 :
REST Client
高级语言: PHP
项目地址: git://github.com/xp-forge/rest-client.git
创建时间: 2018-08-30T11:51:35Z
项目社区:https://github.com/xp-forge/rest-client

开源协议:

下载


Rest Client

Build status on GitHub
XP Framework Module
BSD Licence
Requires PHP 7.0+
Supports PHP 8.0+
Latest Stable Version

REST client

Usage

The Endpoint 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 resource() method to work with the resources.

Creating: post

  1. use webservices\rest\Endpoint;
  2. $api= new Endpoint('https://api.example.com/');
  3. $result= $api->resource('users')->post(['name' => 'Test'], 'application/json');
  4. // Get location from created response, raising an UnexpectedStatus
  5. // exception for any statuscode outside of the range 200-299.
  6. $url= $result->location();
  7. // Same as above, but handle 201 *AND* 200 status codes - see
  8. // https://stackoverflow.com/questions/1860645
  9. $id= $result->match([
  10. 200 => fn($r) => $r->value()['id'],
  11. 201 => fn($r) => (int)basename($r->location())
  12. ]);

Reading: get / head

  1. use webservices\rest\Endpoint;
  2. $api= new Endpoint('https://api.example.com/');
  3. // Test for existance with HEAD, raising UnexpectedStatus exceptions
  4. // for any status code other than 200 and 404.
  5. $exists= $api->resource('users/1549')->head()->match([
  6. 200 => true,
  7. 404 => false
  8. ]);
  9. // Return user object, raising an UnexpectedStatus exception for any
  10. // statuscode outside of the range 200-299.
  11. $user= $api->resource('users/self')->get()->value();
  12. // Same as above, but returns NULL for 404s instead of an exception
  13. $user= $api->resource('users/{0}', [$id])->get()->optional();
  14. // Pass parameters
  15. $list= $api->resource('user')->get(['page' => 1, 'per_page' => 50])->value();
  16. // Access pagination via `Link: <...>; rel="next"` header
  17. $resource= 'groups';
  18. do {
  19. $result= $this->endpoint->resource($resource)->get(['per_page' => 200]);
  20. foreach ($result->value() as $group) {
  21. yield $group['id'] => $group;
  22. }
  23. } while ($resource= $result->link('next'));

Updating: put / patch

  1. use webservices\rest\Endpoint;
  2. $api= new Endpoint('https://api.example.com/');
  3. $resource= $api->resource('users/self')
  4. ->sending('application/json')
  5. ->accepting('application/json')
  6. ;
  7. // Default content type and accept types set on resource used
  8. $updated= $resource->put(['name' => 'Tested', 'login' => $mail])->value();
  9. // Resources can be reused!
  10. $updated= $resource->patch(['name' => 'Changed'])->value();

Deleting: delete

  1. use webservices\rest\Endpoint;
  2. $api= new Endpoint('https://api.example.com/');
  3. // Pass segments, map 204 to true, 404 to null, raise UnexpectedStatus
  4. // exception otherwise
  5. $api->resource('users/{id}', $user)->delete()->match([
  6. 204 => true,
  7. 404 => null
  8. ]);

Uploads

Multipart file uploads are initiated by the upload() method, may include parameters and can upload from any input stream.

  1. use io\File;
  2. use io\streams\MemoryInputStream;
  3. use webservices\rest\Endpoint;
  4. $stream= new MemoryInputStream('Hello');
  5. $file= new File(...);
  6. $endpoint= new Endpoint($url);
  7. $result= $endpoint->resource('files')->upload()
  8. ->pass('tc', 'accepted')
  9. ->transfer('letter', $stream, 'letter.txt', 'text/plain')
  10. ->transfer('cv', $file->in(), $file->filename)
  11. ->finish()
  12. ;

Deserialization

Automatic result deserialization is supported by passing a type to the value() method.

  1. use com\example\api\types\User;
  2. $result= $api->resource('users/{0}', [$id])->get();
  3. // If a type is passed, the result will be unmarshalled to an object
  4. $map= $result->value();
  5. $object= $result->value(User::class);
  6. // Same for optional, but map and object will be NULL for 404s
  7. $map= $result->optional();
  8. $object= $result->optional(User::class);
  9. // Works with any type from the XP typesystem, e.g. arrays of objects
  10. $list= $api->resource('users')->get()->value('org.example.User[]');

Error handling

Operations on the Result class raise UnexpectedStatus exceptions. Here’s how to access their status and reason:

  1. use webservices\rest\UnexpectedStatus;
  2. use util\cmd\Console;
  3. // In unexpected cases
  4. try {
  5. $user= $api->resource('users/self')->get()->value();
  6. } catch (UnexpectedStatus $e) {
  7. Console::writeLine('Unexpected ', $e->status(), ': ', $e->reason());
  8. }
  9. // More graceful handling
  10. $result= $api->resource('users/self')->get();
  11. if ($error= $result->error()) {
  12. Console::writeLine('Unexpected ', $result->status(), ': ', $error);
  13. } else {
  14. $user= $result->value();
  15. }

Authentication

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

  1. use webservices\rest\Endpoint;
  2. $api= new Endpoint('https://user:pass@api.example.com/');

Bearer tokens can also be embedded in the endpoint URL:

  1. use webservices\rest\Endpoint;
  2. $api= new Endpoint('https://token@api.example.com/');

Other header-based authentication values can be passed along as follows:

  1. use webservices\rest\Endpoint;
  2. $api= (new Endpoint('https://api.example.com/'))->with(['X-API-Key' => $key]);

Compression

This library handlees compressed data transparently, sending an Accept-Encoding header containing compression algorithms supported in the PHP setup (based on loaded extensions like e.g. zlib) and using the Content-Encoding response header to determine which algorithm to select.

  1. use webservices\rest\Endpoint;
  2. use io\streams\Compression;
  3. // Detect supported compression algorithms and set "Accept-Encoding" accordingly
  4. $endpoint= new Endpoint($api);
  5. // Send "Accept-Encoding: identity", indicating the server should not compress
  6. $endpoint= (new Endpoint($api))->compressing(Compression::$NONE);
  7. // Send "Accept-Encoding: gzip, br"
  8. $endpoint= (new Endpoint($api))->compressing(['gzip', 'br']);
  9. // Do not send an "Accept-Encoding" header, i.e. no preference is expressed
  10. $endpoint= (new Endpoint($api))->compressing(null);

Testability

This library also includes facilities to ease writing unittests for code making REST API calls. By using the TestEndpoint class and supplying it with routes it should respond to, various scenarios can be easily tested without the need for HTTP protocol and I/O overhead.

  1. use webservices\rest\TestEndpoint;
  2. $endpoint= new TestEndpoint([
  3. '/users/6100' => function($call) {
  4. return $call->respond(200, 'OK', ['Content-Type' => 'application/json'], '{
  5. "id": 6100,
  6. "username": "binford"
  7. }');
  8. },
  9. 'POST /users' => function($call) {
  10. return $call->respond(201, 'Created', ['Location' => '/users/6100']);
  11. },
  12. ]);
  13. $response= $endpoint->resource('/users/me')->get();
  14. // Responds with HTTP status 200 and the above JSON payload