Skip to content
47 changes: 19 additions & 28 deletions src/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Link0\Bunq;

use Assert\Assertion;
use GuzzleHttp\Client as GuzzleClient;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
Expand Down Expand Up @@ -33,31 +34,32 @@ final class Client
*/
private $handlerStack;

/**
* @param Environment $environment
*/
public function __construct(Environment $environment, Keypair $keypair, PublicKey $serverPublicKey = null, string $sessionToken = '')
public function __construct(Environment $environment, Keypair $keypair, PublicKey $serverPublicKey = null, string $sessionToken = '', $proxy = null)
{
Assertion::true(is_null($proxy) || is_string($proxy) || is_array($proxy), 'In case a proxy parameter is provided, it should be either a string or an array.');

$this->handlerStack = HandlerStack::create();

$this->addRequestIdMiddleware($sessionToken);
$this->addRequestSignatureMiddleware($keypair);
$this->addServerResponseMiddleware($serverPublicKey);
$this->addDebugMiddleware($environment);

$this->guzzle = new GuzzleClient([
$configuration = [
'base_uri' => $environment->endpoint(),
'handler' => $this->handlerStack,
'headers' => [
'User-Agent' => 'Link0 Bunq API Client'
]
]);
];

if (is_string($proxy) || is_array($proxy)) {
$configuration['proxy'] = $proxy;
}

$this->guzzle = new GuzzleClient($configuration);
}

/**
* @param string $endpoint
* @return array
*/
public function get(string $endpoint, array $headers = []): array
{
return $this->processResponse(
Expand All @@ -67,12 +69,6 @@ public function get(string $endpoint, array $headers = []): array
);
}

/**
* @param string $endpoint
* @param array $body
* @param array $headers
* @return array
*/
public function post(string $endpoint, array $body, array $headers = []): array
{
return $this->processResponse(
Expand All @@ -83,12 +79,6 @@ public function post(string $endpoint, array $body, array $headers = []): array
);
}

/**
* @param string $endpoint
* @param array $body
* @param array $headers
* @return array
*/
public function put(string $endpoint, array $body, array $headers = []): array
{
return $this->processResponse(
Expand All @@ -102,6 +92,7 @@ public function put(string $endpoint, array $body, array $headers = []): array
/**
* @param string $endpoint
* @param array $headers
*
* @return void
*/
public function delete(string $endpoint, array $headers = [])
Expand All @@ -111,13 +102,9 @@ public function delete(string $endpoint, array $headers = [])
]);
}

/**
* @param ResponseInterface $response
* @return array
*/
private function processResponse(ResponseInterface $response): array
{
$contents = (string) $response->getBody();
$contents = (string)$response->getBody();
$json = json_decode($contents, true)['Response'];

// Return empty responses
Expand Down Expand Up @@ -158,12 +145,13 @@ private function mapResponse(string $key, array $value)
case 'Token':
return Token::fromArray($value);
default:
throw new \Exception("Unknown struct type: " . $key);
throw new \Exception('Unknown struct type: ' . $key);
}
}

/**
* @param string $sessionToken
*
* @return void
*/
private function addRequestIdMiddleware(string $sessionToken)
Expand All @@ -176,6 +164,7 @@ private function addRequestIdMiddleware(string $sessionToken)

/**
* @param Keypair $keypair
*
* @return void
*/
private function addRequestSignatureMiddleware(Keypair $keypair)
Expand All @@ -189,6 +178,7 @@ private function addRequestSignatureMiddleware(Keypair $keypair)

/**
* @param PublicKey|null $serverPublicKey
*
* @return void
*/
private function addServerResponseMiddleware(PublicKey $serverPublicKey = null)
Expand All @@ -203,6 +193,7 @@ private function addServerResponseMiddleware(PublicKey $serverPublicKey = null)

/**
* @param Environment $environment
*
* @return void
*/
private function addDebugMiddleware(Environment $environment)
Expand Down
6 changes: 5 additions & 1 deletion src/Domain/NotificationFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ final class NotificationFilter
const DELIVERYMETHOD_PUSH = 'PUSH';
const DELIVERYMETHOD_CALLBACK = 'URL';

const CATEGORY_BANK_SWITCH_SERVICE = 'BANK_SWITCH_SERVICE';
const CATEGORY_BANK_SWITCH_SERVICE_PAYMENT = 'BANK_SWITCH_SERVICE_PAYMENT';
const CATEGORY_BILLING = 'BILLING';
const CATEGORY_CARD_TRANSACTION_FAILED = 'CARD_TRANSACTION_FAILED';
const CATEGORY_CARD_TRANSACTION_SUCCESSFUL = 'CARD_TRANSACTION_SUCCESSFUL';
Expand All @@ -27,7 +29,9 @@ final class NotificationFilter
const CATEGORY_TAB_RESULT = 'TAB_RESULT';
const CATEGORY_USER_APPROVAL = 'USER_APPROVAL';
const CATEGORY_USE_RESPONSE = 'USE_RESPONSE';

const CATEGORY_USE_RESPONSE_NATIVE_COMMENT = 'USE_RESPONSE_NATIVE_COMMENT';
const CATEGORY_USE_RESPONSE_NATIVE_TOPIC = 'USE_RESPONSE_NATIVE_TOPIC';

const CATEGORY_SLICE_CHAT = 'SLICE_CHAT';
const CATEGORY_SLICE_REGISTRY_ENTRY = 'SLICE_REGISTRY_ENTRY';
const CATEGORY_SLICE_REGISTRY_MEMBERSHIP = 'SLICE_REGISTRY_MEMBERSHIP';
Expand Down
22 changes: 7 additions & 15 deletions src/Middleware/DebugMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,9 @@

namespace Link0\Bunq\Middleware;

use Closure;
use GuzzleHttp\Middleware;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Promise\PromiseInterface;
use Psr\Http\Message\RequestInterface;
use Psr\Http\Message\ResponseInterface;

Expand All @@ -18,13 +19,10 @@
*/
final class DebugMiddleware
{
/**
* @return \Closure
*/
public static function request()
public static function request(): Closure
{
return function (RequestInterface $request) {
echo chr(27) . '[33m' . "REQUEST: " . $request->getMethod() . ' ' . $request->getRequestTarget() . chr(27) . "[0m\n";
echo chr(27) . '[33m' . 'REQUEST: ' . $request->getMethod() . ' ' . $request->getRequestTarget() . chr(27) . "[0m\n";

foreach ($request->getHeaders() as $key => $headers) {
foreach ($headers as $header) {
Expand All @@ -42,14 +40,11 @@ public static function request()
};
}

/**
* @return \Closure
*/
public static function response()
public static function response(): Closure
{
return function (RequestInterface $request, $options, FulfilledPromise $responsePromise) {
return function (RequestInterface $request, $options, PromiseInterface $responsePromise) {
$responsePromise->then(function (ResponseInterface $response) {
echo chr(27) . '[33m' . "RESPONSE: HTTP/" . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase() . chr(27) . "[0m\n";
echo chr(27) . '[33m' . 'RESPONSE: HTTP/' . $response->getProtocolVersion() . ' ' . $response->getStatusCode() . ' ' . $response->getReasonPhrase() . chr(27) . "[0m\n";

foreach ($response->getHeaders() as $key => $headers) {
foreach ($headers as $header) {
Expand All @@ -67,9 +62,6 @@ public static function response()
};
}

/**
* @return callable
*/
public static function tap(): callable
{
return Middleware::tap(
Expand Down