From 2e93409ebb040e951ca4b324e568b515cabbd2d2 Mon Sep 17 00:00:00 2001 From: pvankouteren Date: Fri, 27 Jan 2023 11:10:57 +0100 Subject: [PATCH 1/9] doc improvement --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index b2d4a65..255c55c 100644 --- a/readme.md +++ b/readme.md @@ -10,6 +10,6 @@ composer require wedesignit/dropshiphub-php-api-client ```php $client = new \WeDesignIt\Dropshiphub\Client($apiToken); -$dropshiphub = new \WeDesignIt\Dropshiphub\Client($client); +$dropshiphub = new \WeDesignIt\Dropshiphub\Dropshiphub($client); ``` From f48017087ad4d785194a8abeb74dabad27b4fb22 Mon Sep 17 00:00:00 2001 From: pvankouteren Date: Tue, 31 Jan 2023 09:50:54 +0100 Subject: [PATCH 2/9] Added accessors for supplier related endpoints --- src/Endpoints/Supplier/Supplier.php | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/src/Endpoints/Supplier/Supplier.php b/src/Endpoints/Supplier/Supplier.php index 5c58d99..9760e35 100644 --- a/src/Endpoints/Supplier/Supplier.php +++ b/src/Endpoints/Supplier/Supplier.php @@ -3,6 +3,8 @@ namespace WeDesignIt\Dropshiphub\Endpoints\Supplier; use WeDesignIt\Dropshiphub\Endpoints\Endpoint; +use WeDesignIt\Dropshiphub\Endpoints\Supplier\Order\Order; +use WeDesignIt\Dropshiphub\Endpoints\Supplier\Order\Shipment; class Supplier extends Endpoint { @@ -11,4 +13,20 @@ public function status() { return $this->client->request('get', 'suppliers'); } + + public function order(): Order + { + return new Order($this->client); + } + + public function deliveryTime(): DeliveryTime + { + return new DeliveryTime($this->client); + } + + public function product(): Product + { + return new Product($this->client); + } + } \ No newline at end of file From 4f1bf4184f1dd565848c03bee3f5103998d05277 Mon Sep 17 00:00:00 2001 From: pvankouteren Date: Tue, 31 Jan 2023 10:06:29 +0100 Subject: [PATCH 3/9] Updatd order confirm body and status filter --- src/Endpoints/Supplier/Order/Order.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/Endpoints/Supplier/Order/Order.php b/src/Endpoints/Supplier/Order/Order.php index f754e57..bc3b74b 100644 --- a/src/Endpoints/Supplier/Order/Order.php +++ b/src/Endpoints/Supplier/Order/Order.php @@ -12,7 +12,7 @@ class Order extends Endpoint public function list(string $status = self::STATUS_OPEN): array|string { - return $this->client->request('get', 'suppliers/orders'); + return $this->client->request('get', 'suppliers/orders', ['query' => ['status' => $status]]); } public function get(string $orderIdentifier): array|string @@ -23,7 +23,9 @@ public function get(string $orderIdentifier): array|string public function confirm(string $dshOrderIdentifier, string $yourOrderNumber): array|string { return $this->client->request('post', 'suppliers/orders/' . $dshOrderIdentifier . '/confirm', [ - 'order' => $yourOrderNumber, + 'json' => [ + 'order' => $yourOrderNumber, + ], ]); } From d4b8a5e32fe6f82792c13934ae1404d96ed7f253 Mon Sep 17 00:00:00 2001 From: pvankouteren Date: Tue, 31 Jan 2023 11:29:11 +0100 Subject: [PATCH 4/9] Updated Endpoint for pagination --- src/Endpoints/Endpoint.php | 97 ++++++++++++++++++++++++++ src/Endpoints/Supplier/Order/Order.php | 2 +- 2 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/Endpoints/Endpoint.php b/src/Endpoints/Endpoint.php index 3bcb7d0..7085089 100644 --- a/src/Endpoints/Endpoint.php +++ b/src/Endpoints/Endpoint.php @@ -9,6 +9,27 @@ abstract class Endpoint protected Client $client; + /** + * @var int + */ + protected int $page = 1; + + /** + * @var int + */ + protected int $per_page = 100; + + /** + * @var array + */ + protected array $sorts = []; + + /** + * @var array + */ + protected array $filters = []; + + /** * Resource constructor. * @@ -19,4 +40,80 @@ public function __construct(Client $client) $this->client = $client; } + /** + * @return int + */ + public function getPage() : int + { + return $this->page; + } + + /** + * @param int $page + * @return $this + */ + public function setPage(int $page) : self + { + $this->page = $page; + return $this; + } + + /** + * @return int + */ + public function getPerPage(): int + { + return $this->per_page; + } + + /** + * @param int $per_page + * @return $this + */ + public function setPerPage(int $per_page) : self + { + $this->per_page = $per_page; + return $this; + } + + /** + * @param string $column + * @param $operation + * @param null $value + * @return $this + */ + public function filter(string $column, $operation, $value = null) + { + if (is_null($value)) { + $value = $operation; + $operation = 'eq'; + } + + $this->filters[$column][$operation] = $value; + return $this; + } + + /** + * @param string $column + * @param string $direction + * @return $this + */ + public function sort(string $column, string $direction = 'asc') + { + $this->sorts[$column] = $direction == 'asc' ? "$column" : "-$column"; + return $this; + } + + /** + * @return array + */ + protected function getQuery() + { + $query = $this->filters; + $query['sort'] = array_values($this->sorts); + $query['page'] = $this->getPage(); + $query['per_page'] = $this->getPerPage(); + return $query; + } + } diff --git a/src/Endpoints/Supplier/Order/Order.php b/src/Endpoints/Supplier/Order/Order.php index bc3b74b..9444bbc 100644 --- a/src/Endpoints/Supplier/Order/Order.php +++ b/src/Endpoints/Supplier/Order/Order.php @@ -12,7 +12,7 @@ class Order extends Endpoint public function list(string $status = self::STATUS_OPEN): array|string { - return $this->client->request('get', 'suppliers/orders', ['query' => ['status' => $status]]); + return $this->client->request('get', 'suppliers/orders', ['query' => $this->getQuery() + ['status' => $status]]); } public function get(string $orderIdentifier): array|string From c803ca95351f1e581cc77024be38ca35f8e645cb Mon Sep 17 00:00:00 2001 From: pvankouteren Date: Thu, 2 Feb 2023 10:45:24 +0100 Subject: [PATCH 5/9] Convert to array already for correct conversion of product later on. --- src/Resources/Supplier/Product.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Resources/Supplier/Product.php b/src/Resources/Supplier/Product.php index d220f33..35e3fef 100644 --- a/src/Resources/Supplier/Product.php +++ b/src/Resources/Supplier/Product.php @@ -177,7 +177,7 @@ public function image(string $image): self public function condition(Condition $condition): self { - $this->offsetSet('condition', $condition); + $this->offsetSet('condition', $condition->toArray()); return $this; } From d6eb61a63ac6c35715accd945138762d031b007f Mon Sep 17 00:00:00 2001 From: pvankouteren Date: Thu, 2 Feb 2023 10:49:49 +0100 Subject: [PATCH 6/9] Payloads in 'json' key --- src/Endpoints/Supplier/Order/Shipment.php | 2 +- src/Endpoints/Supplier/Product.php | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Endpoints/Supplier/Order/Shipment.php b/src/Endpoints/Supplier/Order/Shipment.php index 7ab74e9..72c2107 100644 --- a/src/Endpoints/Supplier/Order/Shipment.php +++ b/src/Endpoints/Supplier/Order/Shipment.php @@ -14,6 +14,6 @@ public function list(string $orderIdentifier, string $orderStatus = Order::STATU public function create(string $orderIdentifier, array $data): array|string { - return $this->client->request('post', 'suppliers/orders/' . $orderIdentifier . '/shipments', $data); + return $this->client->request('post', 'suppliers/orders/' . $orderIdentifier . '/shipments', ['json' => $data]); } } \ No newline at end of file diff --git a/src/Endpoints/Supplier/Product.php b/src/Endpoints/Supplier/Product.php index f4dd4f6..6a92a8c 100644 --- a/src/Endpoints/Supplier/Product.php +++ b/src/Endpoints/Supplier/Product.php @@ -19,12 +19,12 @@ public function get(string $productIdentifier): array|string public function create(array $product): array|string { - return $this->client->request('post', 'suppliers/products', $product); + return $this->client->request('post', 'suppliers/products', ['json' => $product]); } public function update(string $productIdentifier, array $product): array|string { - return $this->client->request('put', 'suppliers/products/' . $productIdentifier, $product); + return $this->client->request('put', 'suppliers/products/' . $productIdentifier, ['json' => $product]); } public function delete(string $productIdentifier): array|string From 3f4cd1bb441bd66576dbccacf39ec333f4c12bbf Mon Sep 17 00:00:00 2001 From: pvankouteren Date: Fri, 3 Feb 2023 13:06:48 +0100 Subject: [PATCH 7/9] Psr\Log LoggerInterface can be supplied for collecting the debug messages (request / response / error) --- composer.json | 7 ++++--- src/Client.php | 25 ++++++++++++++++++++++--- 2 files changed, 26 insertions(+), 6 deletions(-) diff --git a/composer.json b/composer.json index dc2fea2..0c36a7a 100644 --- a/composer.json +++ b/composer.json @@ -9,9 +9,10 @@ "WeDesignIt" ], "require": { - "guzzlehttp/guzzle": "^7", "php": "^8.0", - "ext-json": "*" + "ext-json": "*", + "guzzlehttp/guzzle": "^7", + "psr/log": "^3.0" }, "license": "MIT", "authors": [ @@ -31,4 +32,4 @@ "sort-packages": true, "optimize-autoloader": true } -} \ No newline at end of file +} diff --git a/src/Client.php b/src/Client.php index 030e726..0c202d3 100644 --- a/src/Client.php +++ b/src/Client.php @@ -4,7 +4,12 @@ use GuzzleHttp\Client as GuzzleClient; use GuzzleHttp\Exception\GuzzleException; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\MessageFormatter; +use GuzzleHttp\Middleware; use Psr\Http\Message\ResponseInterface; +use Psr\Log\LoggerInterface; +use Psr\Log\LogLevel; class Client { @@ -33,12 +38,12 @@ class Client * * @param string $token */ - public function __construct(string $token, string $companyId) + public function __construct(string $token, string $companyId, ?LoggerInterface $logger = null) { $this->token = $token; $this->companyId = $companyId; - $this->client = new GuzzleClient([ + $parameters = [ 'base_uri' => $this->baseUrl, 'headers' => [ 'Authorization' => 'Bearer ' . $this->token, @@ -46,7 +51,20 @@ public function __construct(string $token, string $companyId) 'X-Company-Id' => $this->companyId, 'User-Agent' => 'dropshiphub-php-api-client/1.0 (github.com/wedesignit/dropshiphub-php-api-client)', ], - ]); + ]; + if (!empty($logger)){ + $stack = HandlerStack::create(); + + $stack->push(Middleware::log( + $logger, + new MessageFormatter(MessageFormatter::DEBUG)), + LogLevel::DEBUG + ); + + $parameters['handler'] = $stack; + } + + $this->client = new GuzzleClient($parameters); } /** @@ -65,6 +83,7 @@ public function request( { $response = $this->rawRequest($method, $uri, $options); + $response->getBody()->seek(0); $contents = $response->getBody()->getContents(); // fallback to application/json as this is, the default return type From 8efcdf30eb9774787b8df56e4715de2681d0b58e Mon Sep 17 00:00:00 2001 From: pvankouteren Date: Fri, 3 Feb 2023 13:13:26 +0100 Subject: [PATCH 8/9] Psr\Log LoggerInterface can be supplied for collecting the debug messages (request / response / error) --- composer.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 0c36a7a..9d5d23d 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,7 @@ "php": "^8.0", "ext-json": "*", "guzzlehttp/guzzle": "^7", - "psr/log": "^3.0" + "psr/log": "^2.0||^3.0" }, "license": "MIT", "authors": [ From dfab7642a174b00ec04d4ccd1a012f040e26a3f2 Mon Sep 17 00:00:00 2001 From: pvankouteren Date: Tue, 7 Feb 2023 14:59:53 +0100 Subject: [PATCH 9/9] Ready for first tag --- src/Client.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Client.php b/src/Client.php index 0c202d3..3b48159 100644 --- a/src/Client.php +++ b/src/Client.php @@ -16,8 +16,7 @@ class Client /** * @var string */ - //protected string $baseUrl = 'https://dropshiphub.nl/api/v1/'; - protected string $baseUrl = 'https://backofficedropshiphub.live.wedesignit.nl/api/v1/'; + protected string $baseUrl = 'https://dropshiphub.nl/api/v1/'; /** * @var GuzzleClient