Expand api objects and improve some

This commit is contained in:
Tim 2021-04-17 01:53:56 +02:00
parent 91d9a5a61f
commit 7698af02fb
8 changed files with 233 additions and 100 deletions

95
src/Client.php Normal file
View File

@ -0,0 +1,95 @@
<?php
namespace Ardent\PicnicApi;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\RequestOptions;
class Client
{
const DEFAULT_COUNTRY_CODE = "NL";
const DEFAULT_API_VERSION = "15";
private GuzzleClient $client;
/**
* Client constructor.
*/
public function __construct(string $username, string $password)
{
$this->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);
}
}

View File

@ -1,69 +0,0 @@
<?php
namespace Ardent\PicnicApi;
use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;
class ClientBuilder
{
const DEFAULT_COUNTRY_CODE = "NL";
const DEFAULT_API_VERSION = "15";
private string $username;
private string $password;
/**
* ClientBuilder constructor.
*/
public function __construct(string $username, string $password)
{
$this->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(),
];
}
}

View File

@ -14,6 +14,7 @@ class ApiListTransformer
Order::getType() => Order::class, Order::getType() => Order::class,
OrderLine::getType() => OrderLine::class, OrderLine::getType() => OrderLine::class,
OrderArticle::getType() => OrderArticle::class, OrderArticle::getType() => OrderArticle::class,
Delivery::getType() => Delivery::class,
default => null, default => null,
}; };
} }

107
src/Objects/Delivery.php Normal file
View File

@ -0,0 +1,107 @@
<?php
namespace Ardent\PicnicApi\Objects;
class Delivery extends AbstractApiObject
{
/**
* Delivery constructor.
*/
public function __construct(
private string $id,
private string $deliveryId,
private string $creationTime,
private array $slot,
private array $eta2,
private string $status,
private array $deliveryTime,
private array $orders,
)
{
}
static public function getType(): string
{
return 'DELIVERY';
}
static public function fromApi(array $apiObject): AbstractApiObject
{
return new self(
$apiObject['id'],
$apiObject['delivery_id'],
$apiObject['creation_time'],
$apiObject['slot'],
$apiObject['eta2'],
$apiObject['status'],
$apiObject['delivery_time'],
ApiListTransformer::getObjects($apiObject['orders']),
);
}
/**
* @return string
*/
public function getId(): string
{
return $this->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;
}
}

View File

@ -31,9 +31,9 @@ class Order extends AbstractApiObject
return new self( return new self(
$apiObject['id'], $apiObject['id'],
ApiListTransformer::getObjects($apiObject['items']), ApiListTransformer::getObjects($apiObject['items']),
[], $apiObject['delivery_slots'] ?? [],
[], $apiObject['selected_slot'] ?? [],
$apiObject['total_count'], $apiObject['total_count'] ?? -1,
$apiObject['total_price'], $apiObject['total_price'],
$apiObject['checkout_total_price'], $apiObject['checkout_total_price'],
); );

View File

@ -4,6 +4,8 @@
namespace Ardent\PicnicApi\Objects; namespace Ardent\PicnicApi\Objects;
use Ardent\PicnicApi\Client;
class OrderArticle extends AbstractApiObject 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[] * @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);
} }
/** /**

View File

@ -25,6 +25,7 @@ class OrderLine extends AbstractApiObject
static public function fromApi(array $apiObject): AbstractApiObject static public function fromApi(array $apiObject): AbstractApiObject
{ {
dump($apiObject);
return new self( return new self(
$apiObject['id'], $apiObject['id'],
ApiListTransformer::getObjects($apiObject['items']), ApiListTransformer::getObjects($apiObject['items']),

View File

@ -5,8 +5,9 @@ namespace Ardent\PicnicApi;
use Ardent\PicnicApi\Objects\ApiListTransformer; use Ardent\PicnicApi\Objects\ApiListTransformer;
use GuzzleHttp\Client; use Ardent\PicnicApi\Objects\Category;
use GuzzleHttp\RequestOptions; use Ardent\PicnicApi\Objects\Delivery;
use Ardent\PicnicApi\Objects\Order;
class PicnicClient class PicnicClient
{ {
@ -14,18 +15,17 @@ class PicnicClient
public function __construct(string $username, string $password) public function __construct(string $username, string $password)
{ {
$cb = new ClientBuilder($username, $password); $this->client = new Client($username, $password);
$this->client = $cb->getClient();
} }
public function getUser() 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 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. * @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 public function getDeliveries(bool $summary = false, array $filters = []): array
{ {
$summary = $summary ? '/summary' : ''; $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)))); return ApiListTransformer::getObjects($this->client->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);
} }
} }