Add api call to upload pictures

This commit is contained in:
Tim 2021-10-05 01:49:59 +02:00
parent 1795efbaf7
commit 136681b675
3 changed files with 44 additions and 7 deletions

View File

@ -48,13 +48,23 @@ class Client
return json_decode($data->getBody()->getContents(), true); return json_decode($data->getBody()->getContents(), true);
} }
public function put(string $url, array $data) public function put(string $url, array $data): void
{ {
$this->client->put($url, [ $this->client->put($url, [
RequestOptions::JSON => $data, RequestOptions::JSON => $data,
]); ]);
} }
public function putFile(string $url, string $image): void
{
$this->client->put($url, [
RequestOptions::BODY => $image,
RequestOptions::HEADERS => [
'Content-Type' => 'application/octet-stream'
],
]);
}
public function get(string $url, array $parameters = []): array public function get(string $url, array $parameters = []): array
{ {
$data = $this->client->get($url, [ $data = $this->client->get($url, [

View File

@ -87,6 +87,11 @@ class GrocyClient
]); ]);
} }
public function uploadPicture(string $fileName, string $image, string $group = 'productpictures')
{
$this->client->putFile(sprintf('files/%s/%s', $group, base64_encode($fileName)), $image);
}
public function getStockClient(): StockClient public function getStockClient(): StockClient
{ {
return new StockClient($this->client); return new StockClient($this->client);

View File

@ -8,7 +8,7 @@ class ProductsObject extends AbstractApiObject
{ {
private string $name; private string $name;
private string $description; private ?string $description;
private int $min_stock_amount = 0; private int $min_stock_amount = 0;
@ -20,8 +20,10 @@ class ProductsObject extends AbstractApiObject
private int $qu_factor_purchase_to_stock; private int $qu_factor_purchase_to_stock;
private ?string $picture_file_name;
#[Ignore] #[Ignore]
private array $userfields; private array $userfields = [];
public static function getEntity(): string public static function getEntity(): string
{ {
@ -48,19 +50,19 @@ class ProductsObject extends AbstractApiObject
} }
/** /**
* @return string * @return string|null
*/ */
public function getDescription(): string public function getDescription(): ?string
{ {
return $this->description; return $this->description;
} }
/** /**
* @param string $description * @param string|null $description
* *
* @return ProductsObject * @return ProductsObject
*/ */
public function setDescription(string $description): ProductsObject public function setDescription(?string $description): ProductsObject
{ {
$this->description = $description; $this->description = $description;
return $this; return $this;
@ -93,6 +95,7 @@ class ProductsObject extends AbstractApiObject
return $this->userfields; return $this->userfields;
} }
#[Ignore]
public function getPicnicId(): ?int public function getPicnicId(): ?int
{ {
return $this->getUserfields()['picnic'] ?? null; return $this->getUserfields()['picnic'] ?? null;
@ -184,4 +187,23 @@ class ProductsObject extends AbstractApiObject
$this->qu_factor_purchase_to_stock = $qu_factor_purchase_to_stock; $this->qu_factor_purchase_to_stock = $qu_factor_purchase_to_stock;
return $this; return $this;
} }
/**
* @return string|null
*/
public function getPictureFileName(): ?string
{
return $this->picture_file_name;
}
/**
* @param string|null $picture_file_name
*
* @return ProductsObject
*/
public function setPictureFileName(?string $picture_file_name): ProductsObject
{
$this->picture_file_name = $picture_file_name;
return $this;
}
} }