Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setle API Update #1

Merged
merged 12 commits into from
Mar 6, 2023
23 changes: 5 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,33 +1,20 @@
# Setle API

PHP client for the [Setle](https://www.setle.be) API. For information and terms of use, refer to the
[official documentation](https://api.setle.be).
[official documentation](https://public-api.setle.app/).

## Installation

`composer require fw4/setle-api`
`composer require fw4/setle-api "^2.0"`

## Usage

```php
// Instantiate the API using a broker token
$api = new Setle\Setle('0123456789abcdef');
// Instantiate the API using a client id and client secret
$api = new Setle\Setle('0123456789abcdef', '0123456789abcdef');

// Request a list of estates
$estates = $api->whise()->getEstates();
```

### Available endpoints

Use the following methods to access available endpoints:

```php
$api->whise()->getEstates();
$api->whise()->getEstate($id);
$api->skarabee()->getEstates();
$api->skarabee()->getEstate($id);
$api->sweepbright()->getEstates();
$api->sweepbright()->getEstate($id);
$estates = $api->getEstates();
```

## Access tokens
Expand Down
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@
"keywords": ["setle", "api", "sdk", "homeweb"],
"license": "MIT",
"require": {
"php": "^7.1|^8.0|^8.1",
fyrts marked this conversation as resolved.
Show resolved Hide resolved
"guzzlehttp/guzzle": "~6.0|~7.0",
"ocramius/package-versions": "^1.4|^2.1"
fyrts marked this conversation as resolved.
Show resolved Hide resolved
"php": "^7.1",
"guzzlehttp/guzzle": "^6.5",
"ocramius/package-versions": "^1.4"
},
"require-dev": {
"phpunit/phpunit": "^7.5|^8.4|^9.0"
"phpunit/phpunit": "^7.5",
"phpstan/phpstan": "^1.4",
"squizlabs/php_codesniffer": "^3.7"
},
"autoload": {
"psr-4": { "Setle\\": "src/" }
Expand Down
18 changes: 9 additions & 9 deletions src/ApiAdapter/ApiAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@

abstract class ApiAdapter implements ApiAdapterInterface
{
/** @var callable */
/** @var callable|null */
protected $debugCallable;

/**
* Send a request to the API and return the parsed response.
*
* @param Request $request
*
* @throws Exception\AuthException if access token is missing or invalid
* @throws Exception\NotFoundException if requested resource is unavailable
* @throws Exception\ApiException if a server-side error occurred
* @throws \Setle\Exception\AuthException if access token is missing or invalid
* @throws \Setle\Exception\NotFoundException if requested resource is unavailable
* @throws \Setle\Exception\ApiException if a server-side error occurred
*
* @return ResponseObject
*/
Expand All @@ -34,25 +34,25 @@ public function request(Request $request): ResponseObject

// Send response to debug callback
if (isset($this->debugCallable)) {
($this->debugCallable)($response_body, $request->getEndpoint(), $request->getBody());
($this->debugCallable)($response_body, $request->getBody());
fyrts marked this conversation as resolved.
Show resolved Hide resolved
}

$response = json_decode($response_body, false);

return new ResponseObject($response);
}

/**
* Set a callback for debugging API requests and responses.
*
* @param callable|null $callable Callback that accepts up to three
* arguments - respectively the response body, request endpoint, and the
* arguments - respectively the response body and the
* request body.
*
* @return self
* @return Self
*/
public function debugResponses(?callable $callable): void
public function debugResponses(?callable $callable): self
{
$this->debugCallable = $callable;
return $this;
}
}
15 changes: 10 additions & 5 deletions src/ApiAdapter/HttpApiAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,14 @@
*/
final class HttpApiAdapter extends ApiAdapter
{
private const BASE_URI = 'https://api.setle.be/v1/';
private const BASE_URI = 'https://public-api.setle.app/v1/';

/** @var Client */
private $client;

/**
* @param array<mixed> $http_client_options
*/
public function __construct(array $http_client_options = [])
{
$http_client_options['base_uri'] = self::BASE_URI;
Expand All @@ -41,17 +44,19 @@ public function __construct(array $http_client_options = [])
$http_client_options['headers']['Content-Type'] = 'application/json';

$this->client = new Client(array_merge([
'timeout' => 10.0,
'timeout' => 30.0,
'http_errors' => false,
], $http_client_options));
}

/**
* {@inheritdoc}
*
* @throws Exception\AuthException if access token is missing or invalid
* @throws Exception\NotFoundException if requested resource is unavailable
* @throws Exception\ApiException if a server-side error occurred
* @throws \Setle\Exception\AuthException if access token is missing or invalid
* @throws \Setle\Exception\NotFoundException if requested resource is unavailable
* @throws \Setle\Exception\ApiException if a server-side error occurred
*
* @return string
*/
public function requestBody(Request $request): string
{
Expand Down
3 changes: 1 addition & 2 deletions src/Exception/ApiException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Setle\Exception;

class ApiException extends \Exception
class ApiException extends \Exception implements \Throwable
fyrts marked this conversation as resolved.
Show resolved Hide resolved
{

}
3 changes: 1 addition & 2 deletions src/Exception/AuthException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Setle\Exception;

class AuthException extends \Exception
class AuthException extends \Exception implements \Throwable
fyrts marked this conversation as resolved.
Show resolved Hide resolved
{

}
3 changes: 1 addition & 2 deletions src/Exception/InvalidPropertyException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Setle\Exception;

class InvalidPropertyException extends \Exception
class InvalidPropertyException extends \Exception implements \Throwable
{

}
3 changes: 1 addition & 2 deletions src/Exception/NotFoundException.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace Setle\Exception;

class NotFoundException extends \Exception
class NotFoundException extends \Exception implements \Throwable
{

}
61 changes: 0 additions & 61 deletions src/IntegrationEndpoint.php

This file was deleted.

27 changes: 17 additions & 10 deletions src/Request/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,22 @@ class Request
/** @var string */
protected $endpoint;

/** @var array */
/** @var array<string> */
protected $parameters;

/** @var array */
/** @var array<string> */
protected $headers;

/** @var array|string|null */
/** @var array<mixed>|string|null */
protected $body;

/**
* @param string $method
* @param string $endpoint
* @param mixed $body
* @param array<mixed> $parameters
* @param array<mixed> $headers
*/
public function __construct(
string $method,
string $endpoint,
Expand Down Expand Up @@ -106,7 +113,7 @@ public function getEndpoint(): string
/**
* Set the HTTP query string parameters.
*
* @param array $parameters Associative array of parameter names and values
* @param array<string> $parameters Associative array of parameter names and values
*
* @return self
*/
Expand All @@ -119,7 +126,7 @@ public function setParameters(array $parameters): Request
/**
* Get the HTTP query string parameters.
*
* @return array Unencoded associative array of parameter names and values
* @return array<string> Unencoded associative array of parameter names and values
*/
public function getParameters(): array
{
Expand All @@ -129,7 +136,7 @@ public function getParameters(): array
/**
* Set additional HTTP headers.
*
* @param array $parameters Associative array of header names and values
* @param array<string> $headers Associative array of header names and values
*
* @return self
*/
Expand All @@ -142,7 +149,7 @@ public function setHeaders(array $headers): Request
/**
* Get additional HTTP headers.
*
* @return array Associative array of header names and values
* @return array<string> Associative array of header names and values
*/
public function getHeaders(): array
{
Expand All @@ -152,7 +159,7 @@ public function getHeaders(): array
/**
* Set the HTTP body to send.
*
* @param array|string $body Raw string or associative array to send as JSON
* @param array<mixed>|string $body Raw string or associative array to send as JSON
*
* @return self
*/
Expand All @@ -172,7 +179,7 @@ public function getBody(): ?string
if (is_null($this->body) || is_string($this->body)) {
return $this->body;
} else {
return json_encode($this->encode($this->body));
return json_encode($this->encode($this->body)) ?: '';
fyrts marked this conversation as resolved.
Show resolved Hide resolved
}
}

Expand All @@ -181,7 +188,7 @@ public function getBody(): ?string
*
* @param mixed $encodable
*
* @return self
* @return string
*/
protected function encode($encodable)
{
Expand Down
12 changes: 10 additions & 2 deletions src/Response/CollectionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,19 @@

namespace Setle\Response;

/**
* @implements \IteratorAggregate<int, string>
* @implements \ArrayAccess<int, string>
*/
class CollectionResponse implements \Countable, \IteratorAggregate, \ArrayAccess
{
/** @var array */
/** @var array<mixed> */
protected $data;

public function __construct(ResponseObject $data)
{
$this->data = array_values($data->getData());
$this->data = (array_key_exists('estates', $data->getData()) ?
array_values($data->getData()['estates']) : $data->getData());
}

/**
Expand All @@ -31,6 +36,9 @@ public function get(int $position)

/**
* @codeCoverageIgnore
*
* @return array<mixed>
*
*/
public function __debugInfo(): array
{
Expand Down
3 changes: 3 additions & 0 deletions src/Response/CollectionResponseIterator.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

namespace Setle\Response;

/**
* @implements \Iterator<int, string>
*/
class CollectionResponseIterator implements \Iterator
{
/** @var CollectionResponse */
Expand Down
2 changes: 1 addition & 1 deletion src/Response/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ class Response extends ResponseObject
{
public function __construct(ResponseObject $response)
{
$this->_data = $response->getData();
$this->data = $response->getData();
}
}
Loading