Download the PHP package mapado/rest-client-sdk without Composer
On this page you can find all versions of the php package mapado/rest-client-sdk. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package rest-client-sdk
Rest Client Sdk
PHP Rest Client SDK for JSON API.
This client tries to avoid the complexity of implementing a custom SDK for every API you have. You just have to implements your model and a little configuration and it will hide the complexity for you.
installation
Usage
Imagine you have those API endpoints:
- /v2/carts
- /v2/carts/{id}
- /v2/cart_items
- /v2/cart_items/{id}
You will need to have two entities, let's say:
- Foo\Bar\Model\Cart
- Foo\Bar\Model\CartItem
You will need to declare one Mapping
containing your two ClassMetadata
Entity declarations
Configure an entity
Imagine the following entities:
Explanations
Entity
definitions:
key
must be the key of your API endpoint
Attributes definition:
name
the name of the key in the API return formattype
type of the attribute
Relations definition:
name
the name of the key in the API return formattargetEntity
class name of the target entity
UnitOfWork
EntityRepository has a UnitofWork used for PUT and POST requests. It sends only changed data not the full model by comparing with the entity stored on GET request.
Configuration
Using Symfony ?
There is a bundle to easily integrate this component: mapado/rest-client-sdk-bundle.
Once configured, you can get a client like this:
Not using Symfony
You need to configure client this way:
Optionnal: create a SdkClientRegistry
It can be easier to manager multiple clients. You can then call:
Accessing data
Fetching an entity / a list of entities
Creating a new instance
The persist
operation will send a POST
request with the serialized object to the API endpoint and return the newly created object
Updating an instance
The update
operation will send a PUT
request with the serialized object to the API endpoint (using the object Id
) and return the updated object.
Deleting an instance
The remove
operation will send a DELETE
request to the API endpoint using the object Id.
Custom serialization
By default, when serializing, the SDK return only the Id to existing relations (like one-to-many).
You may want to serialize OneToMany relations though (imagine you have a Cart
and you want to update linked cartItems
)
to do so, you can specify a $serializationContext
in the persist
and update
method:
Extending the repository
If you need to extend the EntityRepository, you can just do something like that:
Update your entity Rest
attribute to let the entity be aware of it's repository:
Handling exceptions
The SDK will throw an Exception if your API return something different than a 2xx or a 404 status code.
You can see all the exceptions in this folder.
If you need to access the body of the response from your API you can do something like this:
PHPStan
rest-client-sdk does work well with PHPStan. See the related documentation.
Hacking
This library is tested on multiple Symfony and PHP version thanks to composer-test-scenarios.
But there is a conflict between ocramius/proxy-manager, composer 1&2, and php version.
In order to sort this issue, it may be usefull to update scenarios with this command for scenario with php 7.4:
This should be resolved with ocramius/proxy-manager#2.9.0 but it requires composer 2 absolutely, and we (Mapado) are not ready for this.
The path to fix this is to bump a major version with "ocramius/proxy-manager": "^2.9.0"
that requires composer^2
All versions of rest-client-sdk with dependencies
guzzlehttp/guzzle Version ^6.2.2 || ^7.5.0
friendsofphp/proxy-manager-lts Version ^1.0
symfony/http-foundation Version ^4.4 || ^5.0 || ^6.0
symfony/property-access Version ^4.4 || ^5.0 || ^6.0
psr/cache Version ^1.0 || ^2.0 || ^3.0