From 7698af02fbcd477e1d23e0092f1ed645addbb81c Mon Sep 17 00:00:00 2001 From: tim Date: Sat, 17 Apr 2021 01:53:56 +0200 Subject: [PATCH] Expand api objects and improve some --- src/Client.php | 95 +++++++++++++++++++++++++ src/ClientBuilder.php | 69 ------------------- src/Objects/ApiListTransformer.php | 1 + src/Objects/Delivery.php | 107 +++++++++++++++++++++++++++++ src/Objects/Order.php | 6 +- src/Objects/OrderArticle.php | 10 ++- src/Objects/OrderLine.php | 1 + src/PicnicClient.php | 44 +++++------- 8 files changed, 233 insertions(+), 100 deletions(-) create mode 100644 src/Client.php delete mode 100644 src/ClientBuilder.php create mode 100644 src/Objects/Delivery.php diff --git a/src/Client.php b/src/Client.php new file mode 100644 index 0000000..291b65e --- /dev/null +++ b/src/Client.php @@ -0,0 +1,95 @@ +client = new GuzzleClient([ + 'base_uri' => $this->getApiUri(), + 'headers' => [ + 'x-picnic-auth' => $this->authenticate($username, $password), + ], + ]); + } + + private function authenticate(string $username, string $password): string + { + $client = new GuzzleClient([ + 'base_uri' => $this->getApiUri(), + ]); + $data = [ + 'key' => $username, // username + 'secret' => md5($password), // password + 'client_id' => 1, + ]; + $result = $client->post('user/login', [ + RequestOptions::JSON => $data, + ]); +// $body = json_decode($result->getBody()->getContents(), true); +// $userId = $body['user_id']; +// $meta = json_decode(base64_decode($result->getHeader('picnic-meta-info')[0])) + + return $result->getHeader('x-picnic-auth')[0]; + } + + private static function getBaseUri(): string + { + return sprintf('https://storefront-prod.%s.picnicinternational.com', + self::DEFAULT_COUNTRY_CODE + ); + } + + private static function getApiUri(): string + { + return sprintf('%s/api/%s/', + self::getBaseUri(), + self::DEFAULT_API_VERSION + ); + } + + /** + * @param string $imageId + * @param string $size one of tiny/small/medium/large/extra-large + * + * @return string + */ + public static function getImageUri(string $imageId, string $size): string + { + return sprintf('%s/static/images/%s/%s.png', + self::getBaseUri(), + $imageId, + $size + ); + } + + public function post(string $url, array $data): array + { + $data = $this->client->post($url, [ + RequestOptions::JSON => $data, + ]); + + return json_decode($data->getBody()->getContents(), true); + } + + public function get(string $url): array + { + $data = $this->client->get($url); + + return json_decode($data->getBody()->getContents(), true); + } +} \ No newline at end of file diff --git a/src/ClientBuilder.php b/src/ClientBuilder.php deleted file mode 100644 index 7c8c310..0000000 --- a/src/ClientBuilder.php +++ /dev/null @@ -1,69 +0,0 @@ -username = $username; - $this->password = $password; - } - - public function getClient(): Client - { - return new Client([ - 'base_uri' => $this->getBaseUri(), - 'headers' => $this->getHeaders(), - ]); - } - - private function authenticate(): string - { - $client = new Client([ - 'base_uri' => $this->getBaseUri(), - ]); - $data = [ - 'key' => $this->username, // username - 'secret' => md5($this->password), // password - 'client_id' => 1, - ]; - $result = $client->post('user/login', [ - RequestOptions::JSON => $data, - ]); -// $body = json_decode($result->getBody()->getContents(), true); -// $userId = $body['user_id']; -// $meta = json_decode(base64_decode($result->getHeader('picnic-meta-info')[0])) - - return $result->getHeader('x-picnic-auth')[0]; - } - - private function getBaseUri(): string - { - return sprintf('https://storefront-prod.%s.picnicinternational.com/api/%s/', - self::DEFAULT_COUNTRY_CODE, - self::DEFAULT_API_VERSION - ); - } - - private function getHeaders(): array - { - return [ - 'x-picnic-auth' => $this->authenticate(), - ]; - } -} \ No newline at end of file diff --git a/src/Objects/ApiListTransformer.php b/src/Objects/ApiListTransformer.php index 561a6c5..2f5e6e6 100644 --- a/src/Objects/ApiListTransformer.php +++ b/src/Objects/ApiListTransformer.php @@ -14,6 +14,7 @@ class ApiListTransformer Order::getType() => Order::class, OrderLine::getType() => OrderLine::class, OrderArticle::getType() => OrderArticle::class, + Delivery::getType() => Delivery::class, default => null, }; } diff --git a/src/Objects/Delivery.php b/src/Objects/Delivery.php new file mode 100644 index 0000000..b67e498 --- /dev/null +++ b/src/Objects/Delivery.php @@ -0,0 +1,107 @@ +id; + } + + /** + * @return string + */ + public function getDeliveryId(): string + { + return $this->deliveryId; + } + + /** + * @return string + */ + public function getCreationTime(): string + { + return $this->creationTime; + } + + /** + * @return array + */ + public function getSlot(): array + { + return $this->slot; + } + + /** + * @return array + */ + public function getEta2(): array + { + return $this->eta2; + } + + /** + * @return string + */ + public function getStatus(): string + { + return $this->status; + } + + /** + * @return array + */ + public function getDeliveryTime(): array + { + return $this->deliveryTime; + } + + /** + * @return Order[] + */ + public function getOrders(): array + { + return $this->orders; + } +} \ No newline at end of file diff --git a/src/Objects/Order.php b/src/Objects/Order.php index 7c29b90..ae514b6 100644 --- a/src/Objects/Order.php +++ b/src/Objects/Order.php @@ -31,9 +31,9 @@ class Order extends AbstractApiObject return new self( $apiObject['id'], ApiListTransformer::getObjects($apiObject['items']), - [], - [], - $apiObject['total_count'], + $apiObject['delivery_slots'] ?? [], + $apiObject['selected_slot'] ?? [], + $apiObject['total_count'] ?? -1, $apiObject['total_price'], $apiObject['checkout_total_price'], ); diff --git a/src/Objects/OrderArticle.php b/src/Objects/OrderArticle.php index d0c891c..b79f203 100644 --- a/src/Objects/OrderArticle.php +++ b/src/Objects/OrderArticle.php @@ -4,6 +4,8 @@ namespace Ardent\PicnicApi\Objects; +use Ardent\PicnicApi\Client; + class OrderArticle extends AbstractApiObject { /** @@ -54,11 +56,15 @@ class OrderArticle extends AbstractApiObject } /** + * @param string $size one of tiny/small/medium/large/extra-large + * * @return string[] */ - public function getImageIds(): array + public function getImageUrls(string $size = 'medium'): array { - return $this->imageIds; + return array_map(function ($id) use ($size) { + return Client::getImageUri($id, $size); + }, $this->imageIds); } /** diff --git a/src/Objects/OrderLine.php b/src/Objects/OrderLine.php index 07909ba..fd18202 100644 --- a/src/Objects/OrderLine.php +++ b/src/Objects/OrderLine.php @@ -25,6 +25,7 @@ class OrderLine extends AbstractApiObject static public function fromApi(array $apiObject): AbstractApiObject { + dump($apiObject); return new self( $apiObject['id'], ApiListTransformer::getObjects($apiObject['items']), diff --git a/src/PicnicClient.php b/src/PicnicClient.php index fc241c2..3395001 100644 --- a/src/PicnicClient.php +++ b/src/PicnicClient.php @@ -5,8 +5,9 @@ namespace Ardent\PicnicApi; use Ardent\PicnicApi\Objects\ApiListTransformer; -use GuzzleHttp\Client; -use GuzzleHttp\RequestOptions; +use Ardent\PicnicApi\Objects\Category; +use Ardent\PicnicApi\Objects\Delivery; +use Ardent\PicnicApi\Objects\Order; class PicnicClient { @@ -14,18 +15,17 @@ class PicnicClient public function __construct(string $username, string $password) { - $cb = new ClientBuilder($username, $password); - $this->client = $cb->getClient(); + $this->client = new Client($username, $password); } public function getUser() { - return $this->get('user'); + return $this->client->get('user'); } - public function getCart() + public function getCart(): Order { - return ApiListTransformer::getObject($this->get('cart')); + return ApiListTransformer::getObject($this->client->get('cart')); } /** @@ -33,31 +33,23 @@ class PicnicClient * * @param bool $summary Return a summary (less data). * @param array $filters An array with the statusses of the deliveries. For example; ['COMPLETED'] will only get completed deliveries. Possible options include CURRENT, COMPLETED and CANCELLED. + * + * @return Delivery[] */ public function getDeliveries(bool $summary = false, array $filters = []): array { $summary = $summary ? '/summary' : ''; - return $this->post('deliveries' . $summary, $filters); + $data = $this->client->post('deliveries' . $summary, $filters); + return ApiListTransformer::getObjects($data); } - public function search(string $term) + /** + * @param string $term + * + * @return Category[] + */ + public function search(string $term): array { - return ApiListTransformer::getObjects($this->get(sprintf('search?search_term=%s', urlencode($term)))); - } - - private function post(string $url, array $data): array - { - $data = $this->client->post($url, [ - RequestOptions::JSON => $data, - ]); - - return json_decode($data->getBody()->getContents(), true); - } - - private function get(string $url): array - { - $data = $this->client->get($url); - - return json_decode($data->getBody()->getContents(), true); + return ApiListTransformer::getObjects($this->client->get(sprintf('search?search_term=%s', urlencode($term)))); } } \ No newline at end of file