2017 © Pedro Peláez
 

library client

Various clients (curl-wrapper, rest-client, etc.)

image

corex/client

Various clients (curl-wrapper, rest-client, etc.)

  • Thursday, March 15, 2018
  • by corex
  • Repository
  • 0 Watchers
  • 0 Stars
  • 19 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 0 Forks
  • 0 Open issues
  • 7 Versions
  • 0 % Grown

The README.md

CoRex Client

Various clients for php (Http, Rest, etc.), (*1)

License Build Status codecov, (*2)

Supported methods: get, post, put, delete, patch, options., (*3)

A typical flow would be ... - Create and set properties on client. - Create and set properties on request. - Call client with request (properties on client and request are merged). - Use response to get data (various methods exists to get data)., (*4)

Please note that the client might throw exceptions., (*5)

Base client (abstract)

All clients extends base client which means there will be a common set of methods available. - Url can be specified on constructor. - baseUrl() - Specify base url (overwrite base url set through constructor). - timeout() - Specify timeout in seconds. - token() - Specify token. Tokens are used to specify {} in path i.e. a path could be "/user/{id}" and calling token('id', 4') will result in "/user/4". - param() - Specify parameter. Example: param('param', 'test') will be added as [?/&]param=test on url. All parameters are url encoded. - header() - Specify request header(). Example: header('Accept', 'application/json'). - userAgent() - Specify user agent. - getDebug() - Get debug information (response not returned)., (*6)

Normally, setting tokens, parameters and headers on request will override tokens, parameters and headers on client. However, it is possible to specify them as final on client., (*7)

Base request (abstract)

  • path() - Specify path. Will be added to url.
  • token() - Specify token (explained elsewhere). It will override client unless it is set as final.
  • param() - Specify parameter (explained elsewhere). It will override client unless it is set as final.
  • header() - Specify header (explained elsewhere). It will override client unless it is set as final.

Base response (abstract)

  • header() - Get response header.
  • headers() - Get response headers.
  • body() - Get body.
  • status() - Get status (http status). If request succeeded, it will return 200. Get messages through class Status.

Http\Client (extends base client)

Methods inherited from base client., (*8)

A few examples., (*9)

// Get 1 post.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response);

// Get 1 post by token on request.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/{id}');
$request = (new Http\Request())->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

// Get 1 post by path and token on request.
$client = new Http\Client('https://jsonplaceholder.typicode.com');
$request = (new Http\Request())->path('/posts/{id}')->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

Http\Request (extends base request)

Methods inherited from base request + following..., (*10)

  • body() - Set body.

A few examples., (*11)

// Create request with path and token on request.
$request = new Http\Request();
$request->path('/posts/{id}');
$request->token('id', 1);
var_dump($request);

// Create request with header.
$request = new Http\Request();
$request->header('Accept', 'application/json');
var_dump($request);

// Create request with query parameter fields.
$request = new Http\Request();
$request->param('fields', 'firstname,lastname');
var_dump($request);

// Create request with body set.
$request = new Http\Request();
$request->body('{"something":["test1","test2"]}');
var_dump($request);

Http\Response (extends base response)

Methods inherited from base response., (*12)

A few examples., (*13)

// Get body from response.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->body());

// Get status + status-message from response.
$client = new Http\Client('https://jsonplaceholder.typicode.com/unknown');
$response = $client->call(Method::GET);
if ($response->status() != 200) {
    var_dump($response->status());
    var_dump(Status::message($response->status()));
}

// Get response headers.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->headers());

// Get Content-Type from response headers.
$client = new Http\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->header('Content-Type'));

Rest\Client (extends base client)

Methods inherited from base client., (*14)

A few examples., (*15)

// Get 1 post.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response);

// Get 1 post by token on request.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/{id}');
$request = (new Rest\Request())->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

// Get 1 post by path and token on request.
$client = new Rest\Client('https://jsonplaceholder.typicode.com');
$request = (new Rest\Request())->path('/posts/{id}')->token('id', 1);
$response = $client->call(Method::GET, $request);
var_dump($response);

Rest\Request (extends base request)

Methods inherited from base request + following..., (*16)

  • field() - Set field i.e. field('firstname', 'Roger');

A few examples., (*17)

// Create request with path and token on request.
$request = new Rest\Request();
$request->path('/posts/{id}');
$request->token('id', 1);
var_dump($request);

// Create request with header.
$request = new Rest\Request();
$request->header('Accept', 'application/json');
var_dump($request);

// Create request with query parameter fields.
$request = new Rest\Request();
$request->param('fields', 'firstname,lastname');
var_dump($request);

// Create request with fields.
$request = new Rest\Request();
$request->field('firstname', 'Roger');
$request->field('lastname', 'Moore');
var_dump($request);

Rest\Response (extends base response)

Methods inherited from base response + following..., (*18)

  • value() - Get value from response. Uses dot notation i.e. value('actor.firstname');
  • toArray() - To array.

A few examples., (*19)

// Get body from response.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->body());

// Get status + status-message from response.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/unknown');
$response = $client->call(Method::GET);
if ($response->status() != 200) {
    var_dump($response->status());
    var_dump(Status::message($response->status()));
}

// Get response headers.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->headers());

// Get Content-Type from response headers.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->header('Content-Type'));

// Get title from response. Dot notation supported.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->value('title'));

// Get response as array.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/posts/1');
$response = $client->call(Method::GET);
var_dump($response->toArray());

Rest\Entity (abstract) / Rest\Collection (abstract)

There exist a lot of methods to extract data from collection. Look at corex/support to see available methods on Collection. Added following method(s)..., (*20)

  • toArray() - To array.

An example of using Entity and Collection., (*21)

class Album extends Entity
{
    public $userId;
    public $id;
    public $title;
}

class Albums extends Collection
{
    public function current()
    {
        return new Album(parent::current());
    }
}

// Using above classes will make you able to iterate over album/albums and have auto-completion.
$client = new Rest\Client('https://jsonplaceholder.typicode.com/albums');
$response = $client->call(Method::GET);
$albums = new Albums($response);
foreach ($albums as $album) {
    print($album->title . "\n");
    var_dump($album->toArray());
}

The Versions

15/03 2018

dev-develop

dev-develop

Various clients (curl-wrapper, rest-client, etc.)

  Sources   Download

MIT

The Requires

 

The Development Requires

curl rest client

19/07 2017

dev-master

9999999-dev

Various clients (curl-wrapper, rest-client, etc.)

  Sources   Download

MIT

The Requires

 

The Development Requires

curl rest client

19/07 2017

2.0.2

2.0.2.0

Various clients (curl-wrapper, rest-client, etc.)

  Sources   Download

MIT

The Requires

 

The Development Requires

curl rest client

19/06 2017

2.0.1

2.0.1.0

Various clients (curl-wrapper, rest-client, etc.)

  Sources   Download

MIT

The Requires

  • php >=5.6.4

 

The Development Requires

curl rest client

18/06 2017

2.0.0

2.0.0.0

Various clients (curl-wrapper, rest-client, etc.)

  Sources   Download

MIT

The Requires

  • php ^7.0

 

The Development Requires

curl rest client

26/09 2016

1.1.0

1.1.0.0

Various clients for php (curl-wrapper, rest-client, etc.)

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

curl rest client

11/09 2016

1.0.0

1.0.0.0

Various clients for php (curl-wrapper, rest-client, etc.)

  Sources   Download

MIT

The Requires

  • php >=5.5.9

 

curl rest client