Skip to content

Commit 454dac5

Browse files
committed
Added label endpoint
1 parent cf70c7e commit 454dac5

3 files changed

Lines changed: 85 additions & 6 deletions

File tree

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,11 @@ $sendCloud->setPartnerId('3dd88a04-26e4-4959-af11-f5674491573e')
126126
- Get
127127
- List
128128

129+
### Labels
130+
- Get
131+
- Print
132+
- Get labels as PDF
133+
129134
### User
130135
- Get
131136

src/Endpoints/LabelEndpoint.php

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
<?php
2+
3+
namespace Imbue\SendCloud\Endpoints;
4+
5+
use Imbue\SendCloud\Exceptions\ApiException;
6+
use Imbue\SendCloud\Resources\Label;
7+
8+
class LabelEndpoint extends AbstractEndpoint
9+
{
10+
/** @var string */
11+
protected $resourcePath = 'labels';
12+
13+
/**
14+
* @return Label
15+
*/
16+
protected function getResourceObject(): Label
17+
{
18+
return new Label($this->client);
19+
}
20+
21+
/**
22+
* @param $parcelId
23+
* @return Label
24+
* @throws ApiException
25+
*/
26+
public function get($parcelId)
27+
{
28+
return $this->restRead($parcelId, []);
29+
}
30+
31+
/**
32+
* @param array $data
33+
* @return Label
34+
* @throws ApiException
35+
*/
36+
public function print(array $data)
37+
{
38+
return $this->restCreate($data);
39+
}
40+
41+
/**
42+
* @param $parcelId
43+
* @param string $printer
44+
* @return mixed|null
45+
* @throws ApiException
46+
*/
47+
public function getLabelAsPdf($parcelId, string $printer)
48+
{
49+
$id = urlencode($parcelId);
50+
51+
$result = $this->client->performHttpCall(
52+
self::REST_READ,
53+
"{$this->getResourcePath()}/{$printer}/{$id}"
54+
);
55+
56+
return $result;
57+
}
58+
}

src/SendCloudApiClient.php

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
use GuzzleHttp\ClientInterface;
77
use GuzzleHttp\Exception\GuzzleException;
88
use GuzzleHttp\Psr7\Request;
9-
use function GuzzleHttp\Psr7\stream_for;
109
use GuzzleHttp\RequestOptions;
1110
use Imbue\SendCloud\Endpoints\IntegrationEndpoint;
1211
use Imbue\SendCloud\Endpoints\IntegrationShipmentEndpoint;
1312
use Imbue\SendCloud\Endpoints\InvoiceEndpoint;
13+
use Imbue\SendCloud\Endpoints\LabelEndpoint;
1414
use Imbue\SendCloud\Endpoints\ParcelEndpoint;
1515
use Imbue\SendCloud\Endpoints\ParcelStatusEndpoint;
1616
use Imbue\SendCloud\Endpoints\SenderAddressEndpoint;
@@ -21,7 +21,7 @@
2121

2222
class SendCloudApiClient
2323
{
24-
public const CLIENT_VERSION = '0.1.0';
24+
public const CLIENT_VERSION = '0.2.0';
2525
public const API_ENDPOINT = 'https://panel.sendcloud.sc/api';
2626
public const API_VERSION = 'v2';
2727

@@ -30,6 +30,9 @@ class SendCloudApiClient
3030
public const HTTP_DELETE = 'DELETE';
3131
public const HTTP_PATCH = 'PATCH';
3232

33+
private const CONTENT_TYPE_JSON = 'application/json';
34+
private const CONTENT_TYPE_PDF = 'application/pdf';
35+
3336
/** @var int */
3437
private const TIMEOUT = 10;
3538
/** @var int */
@@ -55,7 +58,7 @@ class SendCloudApiClient
5558
public $parcelStatuses;
5659
/** @var ShippingMethodEndpoint */
5760
public $shippingMethods;
58-
/** @var UserEndpoint */
61+
/** @var LabelEndpoint */
5962
public $labels;
6063
/** @var InvoiceEndpoint */
6164
public $invoices;
@@ -96,7 +99,7 @@ public function initializeEndpoints()
9699
$this->parcelStatuses = new ParcelStatusEndpoint($this);
97100
$this->shippingMethods = new ShippingMethodEndpoint($this);
98101
$this->senderAddresses = new SenderAddressEndpoint($this);
99-
// $this->labels = new LabelEndpoint($this);
102+
$this->labels = new LabelEndpoint($this);
100103
$this->invoices = new InvoiceEndpoint($this);
101104
$this->user = new UserEndpoint($this);
102105
$this->integrations = new IntegrationEndpoint($this);
@@ -168,8 +171,8 @@ public function performHttpCallToFullUrl($httpMethod, $url, $httpBody = null)
168171
$userAgent = implode(' ', $this->versionStrings);
169172

170173
$headers = [
171-
'Accept' => 'application/json',
172-
'Content-Type' => 'application/json',
174+
'Accept' => self::CONTENT_TYPE_JSON,
175+
'Content-Type' => self::CONTENT_TYPE_JSON,
173176
'Authorization' => "Basic {$this->getBasicToken()}",
174177
'User-Agent' => $userAgent,
175178
];
@@ -214,6 +217,10 @@ private function parseResponseBody(ResponseInterface $response)
214217
throw new ApiException('No response body found.');
215218
}
216219

220+
if ($this->getContentTypeFromResponse($response) === self::CONTENT_TYPE_PDF) {
221+
return $body;
222+
}
223+
217224
$object = @json_decode($body);
218225

219226
if (json_last_error() !== JSON_ERROR_NONE) {
@@ -234,4 +241,13 @@ private function getBasicToken(): string
234241
{
235242
return base64_encode("{$this->apiKey}:{$this->apiSecret}");
236243
}
244+
245+
/**
246+
* @param ResponseInterface $response
247+
* @return string
248+
*/
249+
private function getContentTypeFromResponse(ResponseInterface $response)
250+
{
251+
return $response->getHeaderLine('Content-Type');
252+
}
237253
}

0 commit comments

Comments
 (0)