Skip to content

Commit

Permalink
Merge pull request #34 from Trunkrs/fix/2.1.3/add-label-url-download
Browse files Browse the repository at this point in the history
Resolve #33: Add label download action
  • Loading branch information
hiddestokvis committed Feb 22, 2021
2 parents b0bca84 + 9c54ee5 commit be5f059
Show file tree
Hide file tree
Showing 8 changed files with 122 additions and 12 deletions.
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.1.2
2.1.3
7 changes: 5 additions & 2 deletions lib/HTTP/GuzzleClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)
Expand Down Expand Up @@ -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,
Expand Down
26 changes: 26 additions & 0 deletions lib/LabelUrls.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down
12 changes: 8 additions & 4 deletions lib/RequestHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -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, "/"));
}

Expand Down Expand Up @@ -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(
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion lib/Settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions lib/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
71 changes: 71 additions & 0 deletions tests/Integration/ShipmentLabelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

namespace Trunkrs\SDK;

use Trunkrs\SDK\Enum\ShipmentLabelType;
use Trunkrs\SDK\Enum\ShipmentService;

class ShipmentLabelTest extends IntegrationTestCase
{
/**
* @var string|null
*/
private $fileName = null;
/**
* @var Shipment|null
*/
private $sut = null;

protected function setUp()
{
parent::setUp();

$this->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();
}
}
}
8 changes: 4 additions & 4 deletions tests/Integration/ShipmentManagementTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down

0 comments on commit be5f059

Please sign in to comment.