Download the PHP package blueink/snorlax without Composer
On this page you can find all versions of the php package blueink/snorlax. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package snorlax
Snorlax
Note: this repo is a fork from a snapshot of the github repo ezdeliveryco/snorlax, which no longer exists. Unfortunately, the previous git history / commits were lost.
A light-weight RESTful client built on top of Guzzle that gives you full control of your API's resources. Its based on method definitions and parameters for your URLs. See the usage below.
Installation
Install with Composer
Add the following requirement in your composer.json
Install:
Basic Usage
As you can see, each action on your resource is defined an array with two keys, method
, defining the HTTP method for the request and path
, which defines the path from the base URI returned by the getBaseUri
method. You can mock URLs using environment variables as you wish.
Snorlax
assume your API returns JSON, so it already returns an StdClass
object with the response, decoded by json_decode
. If you want to get the raw object returned by Guzzle
, use $client->resource->getLastResponse()
.
Amending the response
As noted above, Snorlax
returns an StdClass
object, however Resources may overwrite the ->parse()
method to manipulate the returned response. This is useful when an API returns a nested set of data such as {'pokemon': {'name':'Mew'}}
and you only want the actual data (in this case pokemon
). In this example we could use
This would return the actual pokemon
object. Another scario is that you may want to return a Laravel Collection (Illuminate\Support\Collection
) of objects, you could simply do
The $method
argument is the name of the method which was called to perform the request, such as 'all', or 'get'. This is useful to manipulate different response, such as
Another usage could be to cast certain fields are data types. In this example, we'll cast any fields called created_at
or updated_at
to Carbon isntances
Sending parameters and headers
As said before, Snorlax
is built on top of Guzzle
, so it works basically the same way on passing headers, query strings and request bodies.
Changing client options
If you want to set default headers for every request you send to the default guzzle client, just use the headers
options on your params
config key, just like the Guzzle docs.
Setting a base URI
If all your resources are under the same base URI, you can pass it on the constructor instead of declaring on the resource
class.
Using a custom client
If you don't want to use Guzzle
's default client (or want to mock one), Snorlax
accepts any class that implements GuzzleHttp\ClientInterface
, so just pass your custom client in the constructor. Can be an instance or a callable.
Using a custom cache strategy & driver
If you don't want to use the VolatileRuntime
cache driver or even want to change cache strategy, Snorlax
allow you to inject any of the stragies and storages provided by kevinrob/guzzle-cache-middleware.
Using parallel requests
The parallel pool is made on the top of Guzzle, because of that, you should be using Guzzle.
The result of $requests
is a StdClass
with postsByUserOne
and postsByUserTwo
as properties.
Reconnections
Is possible to reconnect when Guzzle throws a GuzzleHttp\Exception\ConnectException
. By default, Snorlax
tries to reconnect 3 times.
If you want, you can change the number of tries with the retries
parameter.
Contributing
Please see the CONTRIBUTING.md file for guidelines.
License
Please see the LICENSE file for License.
All versions of snorlax with dependencies
guzzlehttp/guzzle Version ~6.1
monolog/monolog Version ^1.21
kevinrob/guzzle-cache-middleware Version ^1.0
rtheunissen/guzzle-log-middleware Version ^1.0