Skip to content

Commit

Permalink
Merge branch 'release/v1.0.0'
Browse files Browse the repository at this point in the history
  • Loading branch information
pvankouteren committed Feb 7, 2023
2 parents 2b3a938 + dfab764 commit d462a1d
Show file tree
Hide file tree
Showing 9 changed files with 151 additions and 15 deletions.
7 changes: 4 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,10 @@
"WeDesignIt"
],
"require": {
"guzzlehttp/guzzle": "^7",
"php": "^8.0",
"ext-json": "*"
"ext-json": "*",
"guzzlehttp/guzzle": "^7",
"psr/log": "^2.0||^3.0"
},
"license": "MIT",
"authors": [
Expand All @@ -31,4 +32,4 @@
"sort-packages": true,
"optimize-autoloader": true
}
}
}
2 changes: 1 addition & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
```

28 changes: 23 additions & 5 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@

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
{
/**
* @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
Expand All @@ -33,20 +37,33 @@ 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,
'Accept' => 'application/json',
'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);
}

/**
Expand All @@ -65,6 +82,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
Expand Down
97 changes: 97 additions & 0 deletions src/Endpoints/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}

}
6 changes: 4 additions & 2 deletions src/Endpoints/Supplier/Order/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => $this->getQuery() + ['status' => $status]]);
}

public function get(string $orderIdentifier): array|string
Expand All @@ -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,
],
]);
}

Expand Down
2 changes: 1 addition & 1 deletion src/Endpoints/Supplier/Order/Shipment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}
}
4 changes: 2 additions & 2 deletions src/Endpoints/Supplier/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 18 additions & 0 deletions src/Endpoints/Supplier/Supplier.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
}

}
2 changes: 1 addition & 1 deletion src/Resources/Supplier/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit d462a1d

Please sign in to comment.