diff --git a/VERSION b/VERSION index 95909b1..0f5c099 100644 --- a/VERSION +++ b/VERSION @@ -1 +1 @@ -2.1.2 +2.1.3 diff --git a/lib/HTTP/GuzzleClient.php b/lib/HTTP/GuzzleClient.php index cd5acb7..582c180 100644 --- a/lib/HTTP/GuzzleClient.php +++ b/lib/HTTP/GuzzleClient.php @@ -4,7 +4,6 @@ use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; -use Psr\Http\Message\ResponseInterface; class GuzzleClient implements HttpClientInterface { private static function hasRequestBody($method): bool { @@ -14,6 +13,10 @@ private static function hasRequestBody($method): bool { } private static function handleResponse($response): array { + if ($response == null) { + return []; + } + $headers = array_map(function ($header) { return is_array($header) ? join($header) @@ -64,7 +67,7 @@ public function request(string $method, string $url, array $headers = [], array public function download(string $method, string $url, string $fileName, array $headers = [], array $params = []): array { $comparableMethod = strtoupper($method); - $stream = fopen($fileName, 'w'); + $stream = fopen($fileName, 'r+'); $options = [ 'headers' => $headers, diff --git a/lib/LabelUrls.php b/lib/LabelUrls.php index f7f2ea2..57bc214 100644 --- a/lib/LabelUrls.php +++ b/lib/LabelUrls.php @@ -2,6 +2,9 @@ namespace Trunkrs\SDK; +use Trunkrs\SDK\Enum\ShipmentLabelType; +use Trunkrs\SDK\Exception\NotSupportedException; + class LabelUrls { private static function applyV1(LabelUrls $labels, $json) { $labels->pdfUrl = $json->label; @@ -22,6 +25,29 @@ private static function applyV2(LabelUrls $labels, $json) { */ public $zplUrl; + /** + * Downloads the specified shipment label. + * @see ShipmentLabelType + * @param string $filename The filename of the file to download to. + * @param string $type The label type. Only pdf supported in API V1. + * @throws Exception\GeneralApiException + * @throws Exception\NotAuthorizedException + * @since 2.0.0 + */ + public function download(string $filename, $type = ShipmentLabelType::PDF) { + switch ($type) { + case ShipmentLabelType::PDF: + RequestHandler::downloadGet($this->pdfUrl, $filename); + break; + case ShipmentLabelType::ZPL: + if (Settings::$apiVersion == 1) { + throw new NotSupportedException("Please use the Label class to download ZPL labels."); + } + RequestHandler::downloadGet($this->zplUrl, $filename); + break; + } + } + public function __construct($json = null) { if ($json) { switch (Settings::$apiVersion) { diff --git a/lib/RequestHandler.php b/lib/RequestHandler.php index 3f58b5b..e75675a 100644 --- a/lib/RequestHandler.php +++ b/lib/RequestHandler.php @@ -11,6 +11,10 @@ class RequestHandler { private static $_httpClient; private static function createUrl(string $resource): string { + if (str_starts_with($resource, 'http')) { + return $resource; + } + return sprintf("%s/v%d/%s", Settings::$baseUrl, Settings::$apiVersion, ltrim($resource, "/")); } @@ -163,10 +167,10 @@ public static function delete(string $resource, array $query = []) { /** * Executes a GET request and downloads the contents into the specified file. * @param string $resource The resource to execute get on. - * @param string $fileName - * @return void - * @throws NotAuthorizedException When the credentials are invalid, not set or expired. + * @param string $fileName The name of the file to download to. * @throws GeneralApiException When the API responds with an unexpected answer. + * @throws NotAuthorizedException When the credentials are invalid, not set or expired. + * @throws ServerValidationException */ public static function downloadGet(string $resource, string $fileName) { $response = self::getClient()->download( @@ -184,7 +188,7 @@ public static function downloadGet(string $resource, string $fileName) { /** * Executes a PUT request with the specified body and downloads the contents into the specified file. * @param string $resource The resource to execute delete on. - * @param string $fileName The file name the download the result into. + * @param string $fileName The name of the file to download to. * @param array $body A optional associative array as the request body. * @return void * @throws NotAuthorizedException When the credentials are invalid, not set or expired. diff --git a/lib/Settings.php b/lib/Settings.php index f17b770..38bc2c7 100644 --- a/lib/Settings.php +++ b/lib/Settings.php @@ -38,7 +38,7 @@ class Settings { /** * @var string The current version of the SDK. */ - public static $sdkVersion = '2.1.2'; + public static $sdkVersion = '2.1.3'; /** * Sets the client credentials that will be used in subsequent requests. diff --git a/lib/Shipment.php b/lib/Shipment.php index de70a8a..e42903d 100644 --- a/lib/Shipment.php +++ b/lib/Shipment.php @@ -297,11 +297,17 @@ public function getState(): ShipmentState { /** * Downloads the label file in the specified format. + * @deprecated As of API version 2, please use download on the label property instead. * @param string $type The label format type. Either pdf or zpl. + * @see LabelUrls::download() * @see Trunkrs\SDK\Enum\ShipmentLabelType * @return Label The label wrapper class. */ public function downloadLabel(string $type): Label { + if (Settings::$apiVersion == 2) { + throw new NotSupportedException("Please use the download feature on the labels instead."); + } + return Label::download( $type, $this->trunkrsNr, diff --git a/tests/Integration/ShipmentLabelTest.php b/tests/Integration/ShipmentLabelTest.php new file mode 100644 index 0000000..f909ab6 --- /dev/null +++ b/tests/Integration/ShipmentLabelTest.php @@ -0,0 +1,71 @@ +fileName = tempnam(sys_get_temp_dir(), "TSDK-Test-Label-"); + var_dump($this->fileName); + + $sender = Mocks::getFakeAddress(); + $sender->addressLine = 'De Liesbosch 90'; + $sender->postal = '3439LC'; + $sender->city = 'Nieuwengein'; + + $recipient = Mocks::getFakeAddress(); + $recipient->addressLine = 'De Liesbosch 90'; + $recipient->postal = '3439LC'; + $recipient->city = 'Nieuwengein'; + + $details = new ShipmentDetails(); + $details->sender = $sender; + $details->recipient = $recipient; + $details->service = ShipmentService::SAME_DAY; + $timeslot = TimeSlot::retrieve($recipient->postal)[0]; + $details->timeSlotId = $timeslot->id; + + $parcel = new Parcel(); + $parcel->reference = Mocks::getGenerator()->uuid; + $details->parcels = [$parcel]; + + $this->sut = Shipment::create($details)[0]; + } + + public function testShouldDownloadAPDFLabel() { + $this->sut->label->download($this->fileName, ShipmentLabelType::PDF); + + $this->assertNotEquals(filesize($this->fileName), 0); + } + + public function testShouldDownloadAZPLabel() { + $this->sut->label->download($this->fileName, ShipmentLabelType::ZPL); + + $this->assertNotEquals(filesize($this->fileName), 0); + } + + protected function tearDown() + { + parent::tearDown(); + + @unlink($this->fileName); + if ($this->sut != null) { + $this->sut->cancel(); + } + } +} \ No newline at end of file diff --git a/tests/Integration/ShipmentManagementTest.php b/tests/Integration/ShipmentManagementTest.php index 4596a31..d57e60f 100644 --- a/tests/Integration/ShipmentManagementTest.php +++ b/tests/Integration/ShipmentManagementTest.php @@ -81,10 +81,10 @@ public function testShouldCreateMultiColli() { $this->shipments = Shipment::create($details); $this->assertCount(4, $this->shipments); - $this->assertEquals($parcel1->reference, $this->shipments[0]->parcels[0]->reference); - $this->assertEquals($parcel2->reference, $this->shipments[1]->parcels[0]->reference); - $this->assertEquals($parcel3->reference, $this->shipments[2]->parcels[0]->reference); - $this->assertEquals($parcel4->reference, $this->shipments[3]->parcels[0]->reference); + $this->assertStringEndsWith($parcel1->reference, $this->shipments[0]->parcels[0]->reference); + $this->assertStringEndsWith($parcel2->reference, $this->shipments[1]->parcels[0]->reference); + $this->assertStringEndsWith($parcel3->reference, $this->shipments[2]->parcels[0]->reference); + $this->assertStringEndsWith($parcel4->reference, $this->shipments[3]->parcels[0]->reference); } protected function tearDown()