44
55use Exception ;
66use GuzzleHttp \Exception \GuzzleException ;
7+ use Psr \Log \LoggerInterface ;
78use Typesense \Lib \Node ;
89use Typesense \Lib \Configuration ;
910use GuzzleHttp \Exception \ClientException ;
@@ -55,6 +56,11 @@ class ApiCall
5556 */
5657 private int $ nodeIndex ;
5758
59+ /**
60+ * @var LoggerInterface
61+ */
62+ public LoggerInterface $ logger ;
63+
5864 /**
5965 * ApiCall constructor.
6066 *
@@ -63,6 +69,7 @@ class ApiCall
6369 public function __construct (Configuration $ config )
6470 {
6571 $ this ->config = $ config ;
72+ $ this ->logger = $ config ->getLogger ();
6673 $ this ->client = new \GuzzleHttp \Client ();
6774 self ::$ nodes = $ this ->config ->getNodes ();
6875 self ::$ nearestNode = $ this ->config ->getNearestNode ();
@@ -96,7 +103,7 @@ private function initializeNodes(): void
96103 public function get (string $ endPoint , array $ params , bool $ asJson = true )
97104 {
98105 return $ this ->makeRequest ('get ' , $ endPoint , $ asJson , [
99- 'data ' => $ params ?? [],
106+ 'query ' => $ params ?? [],
100107 ]);
101108 }
102109
@@ -105,41 +112,70 @@ public function get(string $endPoint, array $params, bool $asJson = true)
105112 * @param mixed $body
106113 *
107114 * @param bool $asJson
115+ * @param array $queryParameters
108116 *
109117 * @return array|string
110118 * @throws TypesenseClientError
111119 * @throws GuzzleException
112120 */
113- public function post (string $ endPoint , $ body , bool $ asJson = true )
121+ public function post (string $ endPoint , $ body , bool $ asJson = true , array $ queryParameters = [] )
114122 {
115123 return $ this ->makeRequest ('post ' , $ endPoint , $ asJson , [
116124 'data ' => $ body ?? [],
125+ 'query ' => $ queryParameters ?? []
117126 ]);
118127 }
119128
120129 /**
121130 * @param string $endPoint
122131 * @param array $body
123132 *
133+ * @param bool $asJson
134+ * @param array $queryParameters
135+ *
124136 * @return array
125137 * @throws TypesenseClientError|GuzzleException
126138 */
127- public function put (string $ endPoint , array $ body ): array
139+ public function put (string $ endPoint , array $ body, bool $ asJson = true , array $ queryParameters = [] ): array
128140 {
129- return $ this ->makeRequest ('put ' , $ endPoint , true , [
141+ return $ this ->makeRequest ('put ' , $ endPoint , $ asJson , [
130142 'data ' => $ body ?? [],
143+ 'query ' => $ queryParameters ?? []
131144 ]);
132145 }
133146
134147 /**
135148 * @param string $endPoint
149+ * @param array $body
150+ *
151+ * @param bool $asJson
152+ * @param array $queryParameters
136153 *
137154 * @return array
138155 * @throws TypesenseClientError|GuzzleException
139156 */
140- public function delete (string $ endPoint ): array
157+ public function patch (string $ endPoint, array $ body , bool $ asJson = true , array $ queryParameters = [] ): array
141158 {
142- return $ this ->makeRequest ('delete ' , $ endPoint , true , []);
159+ return $ this ->makeRequest ('patch ' , $ endPoint , $ asJson , [
160+ 'data ' => $ body ?? [],
161+ 'query ' => $ queryParameters ?? []
162+ ]);
163+ }
164+
165+ /**
166+ * @param string $endPoint
167+ *
168+ * @param bool $asJson
169+ * @param array $queryParameters
170+ *
171+ * @return array
172+ * @throws TypesenseClientError|GuzzleException
173+ */
174+ public function delete (string $ endPoint , bool $ asJson = true , array $ queryParameters = []): array
175+ {
176+ return $ this ->makeRequest ('delete ' , $ endPoint , $ asJson , [
177+ 'query ' => $ queryParameters ?? []
178+ ]);
143179 }
144180
145181 /**
@@ -166,15 +202,22 @@ private function makeRequest(string $method, string $endPoint, bool $asJson, arr
166202 $ url = $ node ->url () . $ endPoint ;
167203 $ reqOp = $ this ->getRequestOptions ();
168204 if (isset ($ options ['data ' ])) {
169- if ($ method === 'get ' ) {
170- $ reqOp ['query ' ] = http_build_query ($ options ['data ' ]);
171- } elseif (is_string ($ options ['data ' ])) {
205+ if (is_string ($ options ['data ' ])) {
172206 $ reqOp ['body ' ] = $ options ['data ' ];
173207 } else {
174208 $ reqOp ['json ' ] = $ options ['data ' ];
175209 }
176210 }
177211
212+ if (isset ($ options ['query ' ])) {
213+ foreach ($ options ['query ' ] as $ key => $ value ) :
214+ if (is_bool ($ value )) {
215+ $ options ['query ' ][$ key ] = ($ value ) ? 'true ' : 'false ' ;
216+ }
217+ endforeach ;
218+ $ reqOp ['query ' ] = http_build_query ($ options ['query ' ]);
219+ }
220+
178221 $ response = $ this ->client ->request ($ method , $ url , $ reqOp );
179222
180223 $ statusCode = $ response ->getStatusCode ();
@@ -315,4 +358,12 @@ public function getException(int $httpCode): TypesenseClientError
315358 return new TypesenseClientError ();
316359 }
317360 }
361+
362+ /**
363+ * @return LoggerInterface
364+ */
365+ public function getLogger ()
366+ {
367+ return $ this ->logger ;
368+ }
318369}
0 commit comments