Skip to content

Commit

Permalink
Merge pull request #571 from PoulainMaxime/feat/v9
Browse files Browse the repository at this point in the history
feat: compatibility ps_v9
  • Loading branch information
PoulainMaxime authored Jan 28, 2025
2 parents 2f91009 + 459bc22 commit a801704
Show file tree
Hide file tree
Showing 23 changed files with 568 additions and 492 deletions.
3 changes: 1 addition & 2 deletions _dev/src/lib/api/shopClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,10 @@ export const fetchShop = async (action: string, params?: { [key: string]: unknow
throw new Error(`Cannot call action ${action}, API is not initialized (missing shop URL)`);
}

const response = await fetch(`${options.shopUrl}&action=${action}`, {
const response = await fetch(`${options.shopUrl}&action=${action}&ajax=1`, {
method: 'POST',
headers: {'Content-Type': 'application/json', Accept: 'application/json'},
body: JSON.stringify({
ajax: 1,
action,
...params,
}),
Expand Down
33 changes: 6 additions & 27 deletions classes/API/Client/FacebookCategoryClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,16 @@

namespace PrestaShop\Module\PrestashopFacebook\API\Client;

use GuzzleHttp\Psr7\Request;
use PrestaShop\Module\PrestashopFacebook\API\ResponseListener;
use PrestaShop\Module\PrestashopFacebook\API\EventSubscriber\ApiErrorSubscriber;
use PrestaShop\Module\PrestashopFacebook\Exception\FacebookClientException;
use PrestaShop\Module\PrestashopFacebook\Factory\ApiClientFactoryInterface;
use PrestaShop\Module\PrestashopFacebook\Http\HttpClient;
use PrestaShop\Module\PrestashopFacebook\Repository\GoogleCategoryRepository;
use Prestashop\ModuleLibGuzzleAdapter\Interfaces\HttpClientInterface;

class FacebookCategoryClient
{
/**
* @var HttpClientInterface
* @var HttpClient
*/
private $client;

Expand All @@ -39,19 +38,12 @@ class FacebookCategoryClient
*/
private $googleCategoryRepository;

/**
* @var ResponseListener
*/
private $responseListener;

public function __construct(
ApiClientFactoryInterface $apiClientFactory,
GoogleCategoryRepository $googleCategoryRepository,
ResponseListener $responseListener
GoogleCategoryRepository $googleCategoryRepository
) {
$this->client = $apiClientFactory->createClient();
$this->googleCategoryRepository = $googleCategoryRepository;
$this->responseListener = $responseListener;
}

/**
Expand Down Expand Up @@ -86,23 +78,10 @@ protected function get($id, array $fields = [], array $query = [])
$query
);

$request = new Request(
'GET',
"/{$id}",
[
'query' => $query,
]
);

$response = $this->responseListener->handleResponse(
$this->client->sendRequest($request),
[
'exceptionClass' => FacebookClientException::class,
]
);
$response = $this->client->get("/{$id}" . http_build_query($query));

if (!$response->isSuccessful()) {
return false;
(new ApiErrorSubscriber())->onParsedResponse($response, ['exceptionClass' => FacebookClientException::class]);
}

return $response->getBody();
Expand Down
60 changes: 17 additions & 43 deletions classes/API/Client/FacebookClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,8 @@
namespace PrestaShop\Module\PrestashopFacebook\API\Client;

use Exception;
use GuzzleHttp\Psr7\Request;
use PrestaShop\Module\PrestashopFacebook\Adapter\ConfigurationAdapter;
use PrestaShop\Module\PrestashopFacebook\API\ResponseListener;
use PrestaShop\Module\PrestashopFacebook\API\EventSubscriber\ApiErrorSubscriber;
use PrestaShop\Module\PrestashopFacebook\Config\Config;
use PrestaShop\Module\PrestashopFacebook\DTO\Object\Ad;
use PrestaShop\Module\PrestashopFacebook\DTO\Object\FacebookBusinessManager;
Expand All @@ -33,8 +32,8 @@
use PrestaShop\Module\PrestashopFacebook\Exception\FacebookClientException;
use PrestaShop\Module\PrestashopFacebook\Factory\ApiClientFactoryInterface;
use PrestaShop\Module\PrestashopFacebook\Handler\ConfigurationHandler;
use PrestaShop\Module\PrestashopFacebook\Http\HttpClient;
use PrestaShop\Module\PrestashopFacebook\Provider\AccessTokenProvider;
use Prestashop\ModuleLibGuzzleAdapter\Interfaces\HttpClientInterface;

class FacebookClient
{
Expand All @@ -54,7 +53,7 @@ class FacebookClient
private $sdkVersion;

/**
* @var HttpClientInterface
* @var HttpClient
*/
private $client;

Expand All @@ -68,25 +67,18 @@ class FacebookClient
*/
private $configurationHandler;

/**
* @var ResponseListener
*/
private $responseListener;

public function __construct(
ApiClientFactoryInterface $apiClientFactory,
AccessTokenProvider $accessTokenProvider,
ConfigurationAdapter $configurationAdapter,
ConfigurationHandler $configurationHandler,
ResponseListener $responseListener
ConfigurationHandler $configurationHandler
) {
$this->accessToken = $accessTokenProvider->getUserAccessToken();
$this->systemToken = $accessTokenProvider->getSystemAccessToken();
$this->sdkVersion = Config::API_VERSION;
$this->client = $apiClientFactory->createClient();
$this->configurationAdapter = $configurationAdapter;
$this->configurationHandler = $configurationHandler;
$this->responseListener = $responseListener;
}

public function setAccessToken($accessToken)
Expand Down Expand Up @@ -216,6 +208,10 @@ public function getFbeAttribute($externalBusinessId)
]
);

if (!is_array($responseContent)) {
return [];
}

return reset($responseContent['data']);
}

Expand Down Expand Up @@ -316,7 +312,7 @@ public function addFbeAttributeIfMissing(array &$onboardingParams)
* @param array $fields
* @param array $query
*
* @return false|array
* @return array
*
* @throws Exception
*/
Expand All @@ -330,18 +326,7 @@ private function get($id, $callerFunction, array $fields = [], array $query = []
$query
);

$request = new Request(
'GET',
"/{$this->sdkVersion}/{$id}?" . http_build_query($query)
);

$response = $this->responseListener->handleResponse(
$this->client->sendRequest($request),
[
'exceptionClass' => FacebookClientException::class,
]
);

$response = $this->client->get("/{$this->sdkVersion}/{$id}", $query);
$responseContent = $response->getBody();

if (!$response->isSuccessful()) {
Expand All @@ -354,8 +339,7 @@ private function get($id, $callerFunction, array $fields = [], array $query = []
$this->disconnectFromFacebook();
$this->configurationAdapter->updateValue(Config::PS_FACEBOOK_FORCED_DISCONNECT, true);
}

return false;
(new ApiErrorSubscriber())->onParsedResponse($response, ['exceptionClass' => FacebookClientException::class]);
}

return $responseContent;
Expand All @@ -366,7 +350,7 @@ private function get($id, $callerFunction, array $fields = [], array $query = []
* @param array $headers
* @param array $body
*
* @return false|array
* @return array
*/
private function post($id, array $headers = [], array $body = [])
{
Expand All @@ -378,7 +362,7 @@ private function post($id, array $headers = [], array $body = [])
* @param array $headers
* @param array $body
*
* @return false|array
* @return array
*/
private function delete($id, array $headers = [], array $body = [])
{
Expand All @@ -391,7 +375,7 @@ private function delete($id, array $headers = [], array $body = [])
* @param array $body
* @param string $method
*
* @return false|array
* @return array
*/
private function sendRequest($id, array $headers, array $body, $method)
{
Expand All @@ -402,22 +386,12 @@ private function sendRequest($id, array $headers, array $body, $method)
$body
);

$request = new Request(
$method,
"/{$this->sdkVersion}/{$id}",
$headers,
json_encode($body)
);
$this->client->setHeaders($headers);

$response = $this->responseListener->handleResponse(
$this->client->sendRequest($request),
[
'exceptionClass' => FacebookClientException::class,
]
);
$response = $this->client->request($method, "/{$this->sdkVersion}/{$id}", $body);

if (!$response->isSuccessful()) {
return false;
(new ApiErrorSubscriber())->onParsedResponse($response, ['exceptionClass' => FacebookClientException::class]);
}

return $response->getBody();
Expand Down
8 changes: 4 additions & 4 deletions classes/API/EventSubscriber/AccountSuspendedSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@
namespace PrestaShop\Module\PrestashopFacebook\API\EventSubscriber;

use PrestaShop\Module\PrestashopFacebook\Adapter\ConfigurationAdapter;
use PrestaShop\Module\PrestashopFacebook\API\ParsedResponse;
use PrestaShop\Module\PrestashopFacebook\Config\Config;
use PrestaShop\Module\PrestashopFacebook\Domain\Http\Response;

class AccountSuspendedSubscriber implements SubscriberInterface
class AccountSuspendedSubscriber
{
/**
* @var ConfigurationAdapter
Expand All @@ -36,9 +36,9 @@ public function __construct(ConfigurationAdapter $configurationAdapter)
$this->configurationAdapter = $configurationAdapter;
}

public function onParsedResponse(ParsedResponse $response, array $options): void
public function onParsedResponse(Response $response): void
{
$suspension = $response->getResponse()->getHeader('X-Account-Suspended') ?: $response->getResponse()->getHeader('x-account-suspended');
$suspension = $response->getHeader('X-Account-Suspended') ?: $response->getHeader('x-account-suspended');
if (!empty($suspension)) {
$this->configurationAdapter->updateValue(Config::PS_FACEBOOK_SUSPENSION_REASON, $suspension);
}
Expand Down
29 changes: 7 additions & 22 deletions classes/API/EventSubscriber/ApiErrorSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,43 +21,28 @@
namespace PrestaShop\Module\PrestashopFacebook\API\EventSubscriber;

use Exception;
use PrestaShop\Module\PrestashopFacebook\API\ParsedResponse;
use PrestaShop\Module\PrestashopFacebook\Domain\Http\Response;
use PrestaShop\Module\PrestashopFacebook\Handler\ErrorHandler\ErrorHandler;

class ApiErrorSubscriber implements SubscriberInterface
class ApiErrorSubscriber
{
/**
* @var ErrorHandler
*/
private $errorHandler;

public function __construct(ErrorHandler $errorHandler)
{
$this->errorHandler = $errorHandler;
}

public function onParsedResponse(ParsedResponse $response, array $options): void
public function onParsedResponse(Response $response, array $options): void
{
if ($response->isSuccessful()) {
return;
}

$class = $options['exceptionClass'] ?: Exception::class;

// TODO: Error sent to the error handler can be improved from the response content
$this->errorHandler->handle(
(new ErrorHandler())->handle(
new $class(
$this->getMessage($response)
),
$response->getResponse()->getStatusCode(),
$response->getStatusCode(),
false,
[
'extra' => $response->getBody(),
]
);
}

private function getMessage(ParsedResponse $response)
private function getMessage(Response $response)
{
$body = $response->getBody();
// If there is a error object returned by the Facebook API, use their codes
Expand All @@ -68,6 +53,6 @@ private function getMessage(ParsedResponse $response)
return 'Facebook API errored with ' . $body['error']['type'] . ' (' . $body['error']['code'] . ')';
}

return 'API errored with HTTP ' . $response->getResponse()->getStatusCode();
return 'API errored with HTTP ' . $response->getStatusCode();
}
}
28 changes: 0 additions & 28 deletions classes/API/EventSubscriber/SubscriberInterface.php

This file was deleted.

Loading

0 comments on commit a801704

Please sign in to comment.