Skip to content

Commit e3e1f2e

Browse files
committed
Merge branch 'feature/add-setup-endpoint' into 'master'
Add createConnect method See merge request signalise/signalise-php-client!7
2 parents cc22b09 + 1cfc16f commit e3e1f2e

File tree

3 files changed

+68
-16
lines changed

3 files changed

+68
-16
lines changed

src/Client/ApiClient.php

Lines changed: 51 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,23 +12,27 @@
1212
use GuzzleHttp\Client;
1313
use GuzzleHttp\Exception\GuzzleException;
1414
use Psr\Http\Message\ResponseInterface;
15+
use Signalise\PhpClient\enum\CallMethods;
1516
use Signalise\PhpClient\Exception\ResponseException;
1617
use Signalise\PhpClient\Traits\FailedResponse;
1718

1819
class ApiClient
1920
{
2021
use FailedResponse;
2122

23+
private const SIGNALISE_POST_CONNECTS = 'api/v1/connects';
24+
private const SIGNALISE_GET_CONNECTS = 'api/v1/connects';
25+
private const SIGNALISE_POST_ORDER_HISTORY = 'api/v1/connects/{{connectId}}/history';
26+
private const SIGNALISE_GET_HISTORY_STATUS = 'api/v1/connects/{{connectId}}/history/status';
27+
private const CREATE_CONNECT_CONTENT_TYPE = 'application/x-www-form-urlencoded';
28+
private const ORDER_HISTORY_CONTENT_TYPE = 'application/json';
29+
2230
private Client $client;
2331

2432
private string $apiKey;
2533

2634
private string $apiUrl;
2735

28-
private const SIGNALISE_GET_CONNECTS = 'api/v1/connects';
29-
private const SIGNALISE_POST_ORDER_HISTORY = 'api/v1/connects/{{connectId}}/history';
30-
private const SIGNALISE_GET_HISTORY_STATUS = 'api/v1/connects/{{connectId}}/history/status';
31-
3236
public function __construct(Client $client)
3337
{
3438
$this->client = $client;
@@ -40,12 +44,12 @@ private function setUp(string $apiUrl, string $apiKey): void
4044
$this->apiKey = $apiKey;
4145
}
4246

43-
private function getHeaders(): array
47+
private function getHeaders(string $contentType): array
4448
{
4549
return [
4650
'User-Agent' => 'ApiClient / PHP ' . phpversion(),
4751
'Accept' => 'application/json',
48-
'Content-Type' => 'application/json',
52+
'Content-Type' => $contentType,
4953
'Authorization' => sprintf('Bearer %s', $this->apiKey)
5054
];
5155
}
@@ -59,20 +63,25 @@ private function createConnectIdUri(string $connectId, string $param): string
5963
);
6064
}
6165

66+
private function createUrl(string $call): string
67+
{
68+
return sprintf(
69+
'%s/%s',
70+
rtrim($this->apiUrl, '/'),
71+
$call
72+
);
73+
}
74+
6275
/**
6376
* @throws GuzzleException
6477
*/
6578
private function get(string $call): ResponseInterface
6679
{
6780
return $this->client->request(
68-
'GET',
69-
sprintf(
70-
'%s/%s',
71-
rtrim($this->apiUrl, '/'),
72-
$call
73-
),
81+
CallMethods::GET_METHOD,
82+
$this->createUrl($call),
7483
[
75-
'headers' => $this->getHeaders()
84+
'headers' => $this->getHeaders(self::ORDER_HISTORY_CONTENT_TYPE)
7685
]
7786
);
7887
}
@@ -83,10 +92,10 @@ private function get(string $call): ResponseInterface
8392
private function post(string $serializedData, string $connectId): ResponseInterface
8493
{
8594
return $this->client->request(
86-
'POST',
95+
CallMethods::POST_METHOD,
8796
$this->createConnectIdUri($connectId, self::SIGNALISE_POST_ORDER_HISTORY),
8897
[
89-
'headers' => $this->getHeaders(),
98+
'headers' => $this->getHeaders(self::ORDER_HISTORY_CONTENT_TYPE),
9099
'body' => $serializedData
91100
]
92101
);
@@ -144,4 +153,31 @@ public function postOrderHistory(
144153

145154
return json_decode($response->getBody()->getContents(), true);
146155
}
156+
157+
/**
158+
* @throws GuzzleException|ResponseException
159+
*/
160+
public function createConnect(
161+
string $apiUrl,
162+
string $apiKey,
163+
array $formData
164+
): array {
165+
$this->setUp($apiUrl, $apiKey);
166+
167+
$response = $this->client->request(
168+
CallMethods::POST_METHOD,
169+
$this->createUrl(self::SIGNALISE_POST_CONNECTS),
170+
[
171+
'headers' => $this->getHeaders(self::CREATE_CONNECT_CONTENT_TYPE),
172+
'form_params' => [
173+
'name' => $formData['name'],
174+
'type' => $formData['type']
175+
]
176+
]
177+
);
178+
179+
self::unableProcessResponse($response);
180+
181+
return json_decode($response->getBody()->getContents(), true);
182+
}
147183
}

src/enum/CallMethods.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
/**
4+
* Copyright Elgentos BV. All rights reserved.
5+
* https://www.elgentos.nl/
6+
*/
7+
8+
declare(strict_types=1);
9+
10+
namespace Signalise\PhpClient\enum;
11+
12+
abstract class CallMethods
13+
{
14+
public const POST_METHOD = 'POST';
15+
public const GET_METHOD = 'GET';
16+
}

tests/Client/ApiClientTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ private function createPostClientMock(
173173
->invoke($subject, $connectId, self::SIGNALISE_POST_ORDER_HISTORY),
174174
[
175175
'headers' => $this->callPrivateFunction($subject, 'getHeaders')
176-
->invoke($subject),
176+
->invoke($subject, 'application/json'),
177177
'body' => $data
178178
]
179179
)

0 commit comments

Comments
 (0)