Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes of service points integration #33

Merged
merged 6 commits into from
Feb 10, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,5 +73,32 @@ if ($webhookEvent->getType() === WebhookEvent::TYPE_PARCEL_STATUS_CHANGED) {
}
```

### Retieve a list of service points

```php
use JouwWeb\Sendcloud\Client;
use JouwWeb\Sendcloud\Exception\SendcloudRequestException;

// Sendcloud uses another api url for service points
// if you use the default one (i.e https://panel.sendcloud.sc/api/v2/), the API will return a 404 error
$client = new Client('your_public_key', 'your_secret_key', null, Client::SERVICE_POINTS_BASE_URL);
villermen marked this conversation as resolved.
Show resolved Hide resolved

try {
// Search for service points in Netherlands
$service_points = $client->searchServicePoints('NL');

// If we want sendcloud to calculate the distance between us and each service points, we need to give the latitude and longitude
$service_points_with_distance = $client->searchServicePoints(country: 'NL', latitude: 51.4350511, longitude: 5.4746339);

// $service_points[0]->getDistance() == null
// $service_points_with_distance[1]->getDistance() != null

// Search for a specific service point using his sendcloud ID
$service_point = $client->getServicePoint(1);
} catch (SendcloudRequestException $exception) {
echo $exception->getMessage();
}
```

## Installation
`composer require jouwweb/sendcloud`
7 changes: 4 additions & 3 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
class Client
{
protected const API_BASE_URL = 'https://panel.sendcloud.sc/api/v2/';
public const SERVICE_POINTS_BASE_URL = 'https://servicepoints.sendcloud.sc/api/v2/';
villermen marked this conversation as resolved.
Show resolved Hide resolved

protected \GuzzleHttp\Client $guzzleClient;

Expand Down Expand Up @@ -565,7 +566,7 @@ public function searchServicePoints(
try {
// Construct query array
$query = [];
$query['country_id'] = $country;
$query['country'] = $country;

if (isset($address)) {
$query['address'] = $address;
Expand Down Expand Up @@ -614,7 +615,7 @@ public function searchServicePoints(
}

// Send request
$response = $this->guzzleClient->get('service-point', [
$response = $this->guzzleClient->get('service-points', [
'query' => $query,
]);

Expand Down Expand Up @@ -644,7 +645,7 @@ public function getServicePoint(ServicePoint|int $servicePoint): ServicePoint
$servicePointId = $servicePoint instanceof ServicePoint ? $servicePoint->getId() : $servicePoint;

try {
$response = $this->guzzleClient->get('service-point/' . $servicePointId);
$response = $this->guzzleClient->get('service-points/' . $servicePointId);
return ServicePoint::fromData(json_decode((string)$response->getBody(), true));
} catch (TransferException $exception) {
throw $this->parseGuzzleException($exception, 'Could not retrieve service point.');
Expand Down
10 changes: 5 additions & 5 deletions src/Model/ServicePoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ public static function fromData(array $data): self
(array)$data['formatted_opening_times'],
(bool)$data['open_tomorrow'],
(bool)$data['open_upcoming_week'],
(int)$data['distance']
isset($data['distance']) ? (int) $data['distance'] : null
);
}

/**
* @param array<string, string> $extraData Can contain carrier specific data
* @param array<int, string[]> $formattedOpeningTimes
* @param int $distance Distance between the reference point and the service point in meters.
* @param ?int $distance Distance in meters OR null if latitude and longitude are not provided in the request
*/
public function __construct(
protected int $id,
Expand All @@ -62,7 +62,7 @@ public function __construct(
protected array $formattedOpeningTimes,
protected bool $openTomorrow,
protected bool $openUpcomingWeek,
protected int $distance,
protected ?int $distance,
) {
}

Expand Down Expand Up @@ -175,9 +175,9 @@ public function isOpenUpcomingWeek(): bool
}

/**
* Distance between the reference point and the service point in meters.
* @return ?int Distance in meters OR null if latitude and longitude are not provided in the request
*/
public function getDistance(): int
public function getDistance(): ?int
{
return $this->distance;
}
Expand Down
23 changes: 20 additions & 3 deletions test/ClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -485,18 +485,35 @@ public function testSearchServicePoint(): void
200,
[],
'[
{"id":1,"code":"217165","is_active":true,"shop_type":null,"extra_data":{"partner_name":"PostNL","sales_channel":"AFHAALPUNT","terminal_type":"NRS","retail_network_id":"PNPNL-01"},"name":"Media Markt Eindhoven Centrum B.V.","street":"Boschdijktunnel","house_number":"1","postal_code":"5611AG","city":"EINDHOVEN","latitude":"51.441444","longitude":"5.475185","email":"","phone":"","homepage":"","carrier":"postnl","country":"NL","formatted_opening_times":{"0":["10:00 - 20:00"],"1":["10:00 - 20:00"],"2":["10:00 - 20:00"],"3":["10:00 - 20:00"],"4":["10:00 - 20:00"],"5":["10:00 - 18:00"],"6":[]},"open_tomorrow":true,"open_upcoming_week":true,"distance":381},
{"id":2,"code":"217165","is_active":true,"shop_type":null,"extra_data":{"partner_name":"PostNL","sales_channel":"AFHAALPUNT","terminal_type":"NRS","retail_network_id":"PNPNL-01"},"name":"Media Markt Eindhoven Centrum B.V.","street":"Boschdijktunnel","house_number":"1","postal_code":"5611AG","city":"EINDHOVEN","latitude":"51.441444","longitude":"5.475185","email":"","phone":"","homepage":"","carrier":"postnl","country":"NL","formatted_opening_times":{"0":["10:00 - 20:00"],"1":["10:00 - 20:00"],"2":["10:00 - 20:00"],"3":["10:00 - 20:00"],"4":["10:00 - 20:00"],"5":["10:00 - 18:00"],"6":[]},"open_tomorrow":true,"open_upcoming_week":true,"distance":381}
{"id":1,"code":"217165","is_active":true,"shop_type":null,"extra_data":{"partner_name":"PostNL","sales_channel":"AFHAALPUNT","terminal_type":"NRS","retail_network_id":"PNPNL-01"},"name":"Media Markt Eindhoven Centrum B.V.","street":"Boschdijktunnel","house_number":"1","postal_code":"5611AG","city":"EINDHOVEN","latitude":"51.441444","longitude":"5.475185","email":"","phone":"","homepage":"","carrier":"postnl","country":"NL","formatted_opening_times":{"0":["10:00 - 20:00"],"1":["10:00 - 20:00"],"2":["10:00 - 20:00"],"3":["10:00 - 20:00"],"4":["10:00 - 20:00"],"5":["10:00 - 18:00"],"6":[]},"open_tomorrow":true,"open_upcoming_week":true},
{"id":2,"code":"217165","is_active":true,"shop_type":null,"extra_data":{"partner_name":"PostNL","sales_channel":"AFHAALPUNT","terminal_type":"NRS","retail_network_id":"PNPNL-01"},"name":"Media Markt Eindhoven Centrum B.V.","street":"Boschdijktunnel","house_number":"1","postal_code":"5611AG","city":"EINDHOVEN","latitude":"51.441444","longitude":"5.475185","email":"","phone":"","homepage":"","carrier":"postnl","country":"NL","formatted_opening_times":{"0":["10:00 - 20:00"],"1":["10:00 - 20:00"],"2":["10:00 - 20:00"],"3":["10:00 - 20:00"],"4":["10:00 - 20:00"],"5":["10:00 - 18:00"],"6":[]},"open_tomorrow":true,"open_upcoming_week":true}
]'
));

$servicePoints = $this->client->searchServicePoints('NL');
$servicePoints = $this->client->searchServicePoints(country: 'NL');

$this->assertCount(2, $servicePoints);
$this->assertEquals(1, $servicePoints[0]->getID());
$this->assertEquals(2, $servicePoints[1]->getID());
}

public function testSearchServicePointWithDistance(): void
{
$this->guzzleClientMock->expects($this->once())->method('request')->willReturn(new Response(
200,
[],
'[
{"id":1,"code":"217165","is_active":true,"shop_type":null,"extra_data":{"partner_name":"PostNL","sales_channel":"AFHAALPUNT","terminal_type":"NRS","retail_network_id":"PNPNL-01"},"name":"Media Markt Eindhoven Centrum B.V.","street":"Boschdijktunnel","house_number":"1","postal_code":"5611AG","city":"EINDHOVEN","latitude":"51.441444","longitude":"5.475185","email":"","phone":"","homepage":"","carrier":"postnl","country":"NL","formatted_opening_times":{"0":["10:00 - 20:00"],"1":["10:00 - 20:00"],"2":["10:00 - 20:00"],"3":["10:00 - 20:00"],"4":["10:00 - 20:00"],"5":["10:00 - 18:00"],"6":[]},"open_tomorrow":true,"open_upcoming_week":true,"distance":381}
]'
));

$servicePoints = $this->client->searchServicePoints(country: 'NL', latitude: 0, longitude: 0);

$this->assertCount(1, $servicePoints);
$this->assertEquals(1, $servicePoints[0]->getID());
$this->assertNotNull($servicePoints[0]->getDistance());
}

public function testGetServicePoint(): void
{
$this->guzzleClientMock->expects($this->once())->method('request')->willReturn(new Response(
Expand Down