Skip to content
Snippets Groups Projects
Commit 5bbc9b8b authored by aid_ua's avatar aid_ua Committed by Bohdan Artemchuk
Browse files

Issue #3337239 by AID_UA, bohart: Added allParcelLockers method in service to...

Issue #3337239 by AID_UA, bohart: Added allParcelLockers method in service to get a list of all postomates.
parent f678f45b
No related branches found
No related tags found
1 merge request!12Issue #3337239: Add allParcelLockers method in service to get a list of all postomates
services:
commerce_loko.client:
class: Drupal\commerce_loko\LokoClient
arguments: ['@http_client']
arguments: ['@config.factory', '@http_client']
......@@ -2,6 +2,9 @@
namespace Drupal\commerce_loko;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Config\ConfigFactoryInterface;
use Drupal\Core\Config\ImmutableConfig;
use GuzzleHttp\ClientInterface;
use GuzzleHttp\Exception\GuzzleException;
......@@ -10,18 +13,35 @@ use GuzzleHttp\Exception\GuzzleException;
*/
class LokoClient implements LokoClientInterface {
/**
* Parcel lockers endpoint.
*/
private const ALL_PARCEL_LOCKERS = '/api/ParcelLocker/AllParcelLockers';
/**
* The main Loko config.
*
* @var \Drupal\Core\Config\ImmutableConfig
*/
protected ImmutableConfig $lokoConfig;
/**
* Constructor for the LokoClient Service.
*
* @param \Drupal\Core\Config\ConfigFactoryInterface $config_factory
* Config Factory.
* @param \GuzzleHttp\ClientInterface $httpClient
* The HTTP client.
* @param string $endpointDomain
* The API domain endpoint.
*/
public function __construct(
ConfigFactoryInterface $config_factory,
protected ClientInterface $httpClient,
protected string $endpointDomain = 'https://api.mint-dispatch.com'
) {}
) {
$this->lokoConfig = $config_factory->get('commerce_loko.settings');
}
/**
* {@inheritdoc}
......@@ -45,4 +65,32 @@ class LokoClient implements LokoClientInterface {
return $response->getStatusCode() === 200;
}
/**
* {@inheritdoc}
*/
public function getAllParcelLockers(): array {
$token_key = $this->lokoConfig->get('loko_token');
try {
$response = $this->httpClient->get(
$this->endpointDomain . self::ALL_PARCEL_LOCKERS,
[
'headers' => [
'Authorization' => 'Bearer ' . $token_key,
'accept' => 'text/plain',
],
]
);
}
catch (GuzzleException) {
return [];
}
if ($response->getStatusCode() !== 200) {
return [];
}
return Json::decode((string) $response->getBody());
}
}
......@@ -26,7 +26,7 @@ interface LokoClientInterface {
* @return array|false
* Gets the all parcel terminals.
*/
// public function getAllParcelLockers(): ?array;
public function getAllParcelLockers(): ?array;
/**
* Method for gets the details for current parcel terminal.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment