66use GuzzleHttp \ClientInterface ;
77use GuzzleHttp \Exception \GuzzleException ;
88use GuzzleHttp \Psr7 \Request ;
9- use function GuzzleHttp \Psr7 \stream_for ;
109use GuzzleHttp \RequestOptions ;
1110use Imbue \SendCloud \Endpoints \IntegrationEndpoint ;
1211use Imbue \SendCloud \Endpoints \IntegrationShipmentEndpoint ;
1312use Imbue \SendCloud \Endpoints \InvoiceEndpoint ;
13+ use Imbue \SendCloud \Endpoints \LabelEndpoint ;
1414use Imbue \SendCloud \Endpoints \ParcelEndpoint ;
1515use Imbue \SendCloud \Endpoints \ParcelStatusEndpoint ;
1616use Imbue \SendCloud \Endpoints \SenderAddressEndpoint ;
2121
2222class 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