Download the PHP package petrelli/eloquent-consumer without Composer
On this page you can find all versions of the php package petrelli/eloquent-consumer. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package eloquent-consumer
About
Eloquent API Consumer will allow you to solve two main problems regarding API's in Laravel:
- Generate and execute API calls in a clear and simple way
- Process API responses and generate Eloquent like models and collections (including paginated ones)
You can see an example application here: Eloquent Consumer Example APP.
Development Note
This package is still on alpha state, and under heavy development. Things might change in the near future.
Motivation to create this package
Our CMS was designed to use Eloquent like models as the data source.
So we needed a way to integrate an API origin to build some listings in a seamless way, without having to modify the CMS.
This library creates models who's interface will be compatible with Eloquent, so building queries, pagination, scopes, filtering, and mostly everything related to Eloquent will be available.
With this package you will be able to manage your API endpoints, caching strategies, low level API query configuration, model attributes, scopes and functions, and basically every element involved in the process of generating a query, to getting the processed end result.
Table of contents
- Overview
-
Core Concepts
- Endpoint
- Consumer
- Grammar
- Installation
- Configuration
- Usage
- Extended Reference
- License
Overview
Let's imagine we have a Book
API model and we want to read some data from the API.
Let's perform some calls right now:
As you can see this syntax is very familiar.
Let's say you want to add your own Eloquent like scope to get only published elements. Just follow the same syntax as you usually do with Laravel:
This will pass a published=true
parameter when performing the API call (because we simply used the rawQuery function as we saw before)
To configure this model we have to create an Endpoint
class for our specific API. You can do that manually or using the following command:
Let's edit some options:
As you can see, we only defined here a $baseUri
and a default TTL for caching (200 seconds).
Now that we have the endpoint configured, we should create a the actual Book Model. This model must inherit from our faux Eloquent like class.
Here we just have to configure two things:
- Endpoint class to be used
- Actual URL's used by this type of resource
Collection and resource are the default endpoints that the package uses. Of course you can define your own and use them later, but these are enough.
This will be enough to get you started.
If you continue reading you will learn to modify how calls are performed, and how responses will be processed.
Core concepts
You will have a default configuration file for you project defining everything we will show here. But of course, you can override these values configuring each class separately.
It's recommended to use the default namespace ApiConsumer
when creating new elements.
We will mention the following entities:
- Endpoint
- Consumer
- Grammar
Endpoint
This one will be your main entity.
You can have as many as you want per project, allowing you to use multiple API's and/or multiple configurations within the same API.
Here you can define the following options:
Keep in mind these options:
- Consumer Class
- Grammar Class
Consumer
Consumers are the clients that will execute the actual API calls. This package includes a default client that uses Guzzle.
Usually using the default one with Guzzle will be more than enough.
Creating a new Consumer class could be useful if you had to add new options like authentication headers, timeout configurations, basically, any configuration you might want supported by Guzzle or your chosen client.
Grammar
A Grammar class basically a collector that transforms all the information you gave to the Query Builder into fields that will be sent in your API request.
It's a very simple class that you can extend and adjust to your API specs.
Let's see this Grammar Class example function:
Here you see that the function search('term')
will be transformed into a parameter named q
.
Please refer to our default grammar class code, and you will observe there how we process all options.
Installation
-
Install the package via composer:
- Export configuration file:
In there you can define default options and entities (endpoint, grammar and connection).
Configuration
Let's generate an Endpoint:
Or in case you want to use your own namespace:
Now let's add some configuration values to the endpoint:
Here you can see it's a very basic Endpoint. No grammar or consumer customized.
You can add a $grammarClass
variable in case you want to extend the default one:
And we will need to create that Grammar class:
So here, we redefine compilePage
which will send a page_number
parameter on the API query, instead of the page
default.
Usage
After everything is configured we can start using this model almost as if it was an eloquent entity.
As you can see this syntax is very familiar.
Let's say you want to add your own Eloquent like scope to get only published elements. Just follow the same syntax as you usually do with Laravel:
This will pass a published=true
parameter when performing the API call (because we simply used the rawQuery function as we saw before)
Relationships
Documentation to be completed.
Transformer classes
So we generated the query, and now we are getting a correct response.
We have now to refactor this response to fit the library standards so everything works as expected.
This will be done easily with a TransformerClass.
Transformer classes have only one function: transform()
.
There you have available $this->response
to use it and transform your responses to our proper format.
This will enable you to use any API transparently of the underline response format.
The expected format looks like the following:
So in order to be able to use full power, you will have to adapt your response to look like this one.
Extended Reference
TODO
License
The MIT License (MIT). Please see License File for more information.