1212use GuzzleHttp \Client ;
1313use GuzzleHttp \Exception \GuzzleException ;
1414use Psr \Http \Message \ResponseInterface ;
15+ use Signalise \PhpClient \enum \CallMethods ;
1516use Signalise \PhpClient \Exception \ResponseException ;
1617use Signalise \PhpClient \Traits \FailedResponse ;
1718
1819class 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}
0 commit comments