jeudi 25 novembre 2021

Implementing an api integration with symfony5

I have a setup, but I can't decide how to do it in the most logical way. The issue is;

I want to use the APIs of many marketplaces in my current Symfony app. There is a structure as follows.

MarketPlace1 -> Products, Orders, Customers etc.
MarketPlace2 -> Products, Orders, Customers etc.
MarketPlace3 -> Products, Orders, Customers etc.

Since it is necessary to make authorization with different parameters for each marketplace, I store the authorization information in a single column in JSON format in order not to pollute the database. E.g;

MarketPlace1 -> apiKey: xxx ApiSecret: yyy
MarketPlace2 -> authToken: zzz
MarketPlace3 -> userName: xxx password: yyy

Here I thought of using Abstract Factory to set up the basic structure and separate the creation of objects, but I couldn't achieve this in Symfony. I feel like I'm doing something wrong.

Instead of fetching all the authorization information from the database each time, I think it would make more sense to store it in the current session when the user logs in to the website and use it throughout the session. Anyone have a more logical idea? (This will be a website that many different customers use by adding integrations.)

As a result, when the customer clicks on the MarketPlace1 integration and goes to the "Orders" page, only the orders from MarketPlace1 will be displayed. Below is the design I've tried;

interface BaseInterface {
    public function makeRequest();
}

class Base implements BaseInterface {
    private $client;
    private $auth;

    public function __construct()
    {
        $this->auth = [
            'auth' => [
                'appKey' => 'HARD CODED',
                'appSecret' => 'HARD CODED'
            ]
        ];
    }

    private function setUrl($url)
    {
        $this->client = new \SoapClient($url, [
            .......
            ))
        ]);
    }

    private function getClient()
    {
        return $this->client;
    }

    protected function makeRequest(string $requestMethod, array $parameters)
    {
        .....
    }
}
class Order extends Base implements OrderInterface
{
    public function getOrderDetails($id)
    {
        .... Some logic here.
        return $orderDetails;
    }

    public function getOrderList(): ?array
    {
        .... Some logic here.
        return $orderDetail;
    }
}

Aucun commentaire:

Enregistrer un commentaire