Skip to content
This repository was archived by the owner on Feb 10, 2025. It is now read-only.

Commit dcd6321

Browse files
committed
feat(Shop): add /shop/address route to interact with the 'La Poste' API
env: add LA_POSTE_KEY environment variable
1 parent f22cfea commit dcd6321

File tree

6 files changed

+77
-1
lines changed

6 files changed

+77
-1
lines changed

.env.example

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,6 @@ REDIS_PREFIX=retrobox:
5757
WEBSOCKET_SERVER_ENDPOINT=http://localhost:3008
5858

5959
SENTRY_DSN=https://[email protected]/XXX
60-
SENTRY_ENABLE=1
60+
SENTRY_ENABLE=1
61+
62+
LA_POSTE_KEY=XXX

App/App.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ public function __construct()
6767
})->add(new Middlewares\JWTMiddleware($this->getContainer()));
6868

6969
$this->group('/shop', function () {
70+
$this->get('/address', [Controllers\ShopController::class, 'getQueryAddress']);
7071
$this->get('/storage-prices', [Controllers\ShopController::class, 'getStoragePrices']);
7172
$this->get('/shipping-prices', [Controllers\ShopController::class, 'getShippingPrices']);
7273
$this->get('/{locale}/categories', [Controllers\ShopController::class, 'getCategories']);

App/Controllers/ShopController.php

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use App\Utils\CacheManager;
77
use App\Utils\Chronopost;
88
use App\Utils\Countries;
9+
use App\Utils\LaPoste;
910
use Illuminate\Database\Capsule\Manager;
1011
use Psr\Container\ContainerInterface;
1112
use Psr\Http\Message\ServerRequestInterface;
@@ -140,4 +141,31 @@ public function getShippingPrices(ServerRequestInterface $request, Response $res
140141
]
141142
]);
142143
}
144+
145+
public function getQueryAddress(ServerRequestInterface $request, Response $response, ContainerInterface $container)
146+
{
147+
$validator = new Validator($request->getQueryParams());
148+
$validator->required('query');
149+
$validator->notEmpty('query');
150+
if (!$validator->isValid())
151+
return $response->withJson([
152+
'success' => false,
153+
'errors' => $validator->getErrors(true)
154+
], 400);
155+
156+
$result = $container->get(LaPoste::class)->searchAddress($validator->getValue('query'));
157+
if (empty($result))
158+
return $response->withJson([
159+
'success' => false,
160+
'errors' => [
161+
['code' => 'unknown-address', 'message' => 'This address seems to be incorrect']
162+
]
163+
], 404);
164+
165+
return $response->withJson([
166+
'success' => true,
167+
'data' => $result
168+
]);
169+
}
170+
143171
}

App/Utils/LaPoste.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
<?php
2+
3+
namespace App\Utils;
4+
5+
use GuzzleHttp\Client;
6+
7+
class LaPoste
8+
{
9+
/**
10+
* @var Client
11+
*/
12+
private Client $client;
13+
14+
private string $endpoint = "https://api.laposte.fr/controladresse/v1";
15+
16+
public function __construct(string $apiKey)
17+
{
18+
$this->client = new Client(['headers' => ['X-Okapi-Key' => $apiKey]]);
19+
}
20+
21+
/**
22+
* Look for an address into the public laposte database
23+
* The address is compacted as a single string
24+
* The search result is a array of possible address (address object)
25+
* Each address object contain an 'address' key which is the valid address as a single string
26+
* It also contain an 'code' key which is the id of the address in the public laposte database
27+
*
28+
* @param string $query
29+
* @return array
30+
*/
31+
public function searchAddress(string $query): array
32+
{
33+
$res = $this->client->get($this->endpoint . '/adresses', ['query' => ['q' => $query]]);
34+
$content = json_decode($res->getBody()->getContents(), true);
35+
return array_map(fn ($a) => ['address' => $a['adresse'], 'code' => $a['code']], $content);
36+
}
37+
}

App/config/api.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,8 @@
3434
"account" => [
3535
"redirection_url_cookie" => "login_redirection_url",
3636
"jwt_cookie" => "user_token"
37+
],
38+
"la_poste" => [
39+
"key" => getenv('LA_POSTE_KEY')
3740
]
3841
];

App/config/containers.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
use App\NotFoundHandler;
44
use App\NotAllowedHandler;
5+
use App\Utils\LaPoste;
56
use App\Utils\MailChimp;
67
use App\Utils\WebSocketServerClient;
78
use DI\Container;
@@ -100,5 +101,9 @@
100101
);
101102
$client->createConnexion();
102103
return $client;
104+
},
105+
106+
LaPoste::class => function (ContainerInterface $container) {
107+
return new LaPoste($container->get('la_poste')['key']);
103108
}
104109
];

0 commit comments

Comments
 (0)