PHPackages                             zepgram/module-rest - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [API Development](/categories/api)
4. /
5. zepgram/module-rest

ActiveMagento2-module[API Development](/categories/api)

zepgram/module-rest
===================

Technical module to industrialize API REST call with dependency injection pattern using Guzzle library

3.0.0(2mo ago)1326.2k↓34.2%4MITPHPPHP ^8.2

Since Nov 5Pushed 2mo ago3 watchersCompare

[ Source](https://github.com/zepgram/module-rest)[ Packagist](https://packagist.org/packages/zepgram/module-rest)[ RSS](/packages/zepgram-module-rest/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (9)Versions (25)Used By (0)

Zepgram Rest
============

[](#zepgram-rest)

Overview
--------

[](#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
--------

[](#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
------------

[](#installation)

```
composer require zepgram/module-rest
bin/magento module:enable Zepgram_Rest
bin/magento setup:upgrade

```

Guideline with ApiPool
----------------------

[](#guideline-with-apipool)

1. 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)
2. 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`
3. Declare your service in di.xml by implementing `Zepgram\Rest\Service\ApiProvider` as VirtualClass, you can configure it by following the [ApiProviderConfig](#configuration)
4. 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
5. 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

Basic guideline implementation
------------------------------

[](#basic-guideline-implementation)

Instead of declaring your class in `Zepgram\Rest\Service\ApiPoolInterface` you can also directly inject your ApiProvider in a dedicated class:

```

            Zepgram\Sales\Rest\FoxtrotOrderRequestAdapter
            foxtrot

            CustomApiProvider

```

```

                Foxtrot

                    Base URI

                    Timeout

                    Cache TTL

                    Enable Debug
                    Magento\Config\Model\Config\Source\Yesno

```

**config.xml**

```

                https://foxtrot.service.io
                30
                7200
                1

```

**di.xml**

```

         Zepgram\Sales\Rest\FoxtrotOrderRequestAdapter
         foxtrot

            FoxtrotOrderApiProvider

```

**OrderDataExample.php**

```
