Download the PHP package zepgram/module-rest without Composer
On this page you can find all versions of the php package zepgram/module-rest. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download zepgram/module-rest
More information about zepgram/module-rest
Files in zepgram/module-rest
Package module-rest
Short Description Technical module to industrialize API REST call with dependency injection pattern using Guzzle library
License MIT
Informations about the package module-rest
Zepgram Rest
Overview
Zepgram Rest is a technical module designed to streamline the development of REST API integrations in Magento 2 projects. Utilizing the Guzzle HTTP client for dependency injection, this module offers a robust set of features aimed at reducing boilerplate code, improving performance, and enhancing debugging capabilities. By centralizing REST API interactions and leveraging Magento's built-in systems, Zepgram Rest simplifies the implementation process for developers.
Features
Zepgram Rest provides several key features to aid Magento developers in creating and managing RESTful services:
- Avoid Code Duplication: Minimize repetitive code with a straightforward setup in di.xml. Implement your REST API integrations with just one class creation, streamlining the development process.
- Centralized Configuration: Manage all your REST web services configurations in one place, ensuring consistency and ease of maintenance.
- Built-in Registry and Cache: Take advantage of Magento's native cache mechanisms and dedicated registry to boost your API's performance and security. This feature helps in efficiently managing data retrieval and storage, reducing the load on your server.
- Generic Logger: Debugging is made effortless with an inclusive logging system. Enable the debug mode to log detailed information about your API calls, including parameters, requests, and responses, facilitating easier troubleshooting.
- Data Serialization: Declare whether your requests and results should be JSON serialized or not. This flexibility prevents the need for multiple serializer implementations, accommodating various API requirements with ease.
Installation
Guideline with ApiPool
- Create a RequestAdapter class for your service extending abstract class
Zepgram\Rest\Model\RequestAdapter
, this class represent your service contract adapter:- public const SERVICE_ENDPOINT: define the service endpoint
- dispatch(DataObject $rawData): initialize data that you will adapt to request the web service
- getBody(): implement body request
- getHeaders(): implement headers
- getUri(): implement uri endpoint (used to handle dynamic values)
- getCacheKey(): implement cache key for your specific request (you must define a unique key)
- Create a system.xml, and a config.xml with a dedicated configName:
- section:
rest_api
- group_id:
$configName
- fields:
base_uri
timeout
is_debug
cache_ttl
- section:
- Declare your service in di.xml by implementing
Zepgram\Rest\Service\ApiProvider
as VirtualClass, you can configure it by following the ApiProviderConfig - Declare your RequestAdapter and ApiProvider in
Zepgram\Rest\Service\ApiPoolInterface
:- Add a new item in
apiProviders[]
:- The key is your custom RequestAdapter full namespace
- The value is your ApiProvider as a VirtualClass
- Add a new item in
- Inject ApiPoolInterface in the class that will consume your API and use
$this->apiPool->execute(RequestAdapter::class, $rawData)
where:- RequestAdapter::class represents the request adapter declared in
apiProviders[]
- $rawData is an array of dynamic data that will be dispatch in
dispatch()
method
- RequestAdapter::class represents the request adapter declared in
Basic guideline implementation
Instead of declaring your class in Zepgram\Rest\Service\ApiPoolInterface
you can also directly inject
your ApiProvider in a dedicated class:
Configuration
Store config
If you do not declare specific configuration, the request will fall back on default configuration.
To override the default config, you must follow this system config pattern: rest_api/%configName%/base_uri
XML config
You can configure your service with Zepgram\Rest\Service\ApiProvider
by creating a
VirtualClass and customize its injections for your needs by following the below configuration:
Variable name | Type | Default value | Is Optional | Description |
---|---|---|---|---|
configName | string | default | no | Value to retrieve group id from system config |
requestAdapter | object | RequestAdapter | no | Adapter class to build and customize the request |
validator | object | null | yes | Validate the service contract |
method | string | GET | yes | Request method |
isJsonRequest | boolean | true | yes | Parse request array to json |
isJsonResponse | boolean | true | yes | Parse response string to array |
isVerify | boolean | true | yes | Enable SSL certificate verification |
Implementation
Here is a simple implementation example with a service called Foxtrot using the order object as rawData:
FoxtrotOrderRequestAdapter.php
system.xml
config.xml
di.xml
OrderDataExample.php
Issues & Improvements
If you encountered an issue during installation or with usage, please report it on this github repository.
If you have good ideas to improve this module, feel free to contribute.
All versions of module-rest with dependencies
guzzlehttp/guzzle Version ^7.0
magento/framework Version ^103.0.4
zepgram/module-base Version ~0.0.1
zepgram/module-json-schema Version ~0.1.0