44
55use GuzzleHttp \Exception \GuzzleException ;
66use Devloops \Typesence \Lib \Configuration ;
7+ use GuzzleHttp \Exception \ClientException ;
78use GuzzleHttp \Exception \RequestException ;
89use Devloops \Typesence \Exceptions \ServerError ;
910use Devloops \Typesence \Exceptions \ObjectNotFound ;
@@ -95,23 +96,42 @@ public function get(string $endPoint, array $params, bool $asJson = true)
9596 foreach ($ this ->nodes () as $ node ) {
9697 $ url = $ node ->url () . $ endPoint ;
9798 try {
98- $ request = $ this ->client ->get ($ url , [
99- 'headers ' => [
100- self ::API_KEY_HEADER_NAME => $ node ->getApiKey (),
101- ],
102- 'query ' => http_build_query ($ params ),
103- 'connect_timeout ' => $ this ->config ->getTimeoutSeconds (),
104- ]);
99+ $ request = $ this ->client ->get (
100+ $ url ,
101+ [
102+ 'headers ' => [
103+ self ::API_KEY_HEADER_NAME => $ node ->getApiKey (),
104+ ],
105+ 'query ' => http_build_query ($ params ),
106+ 'connect_timeout ' => $ this ->config ->getTimeoutSeconds (),
107+ ]
108+ );
105109 if ($ request ->getStatusCode () !== 200 ) {
106- $ errorMessage = \GuzzleHttp \json_decode ($ request ->getBody (),
107- true )['message ' ] ?? 'API error ' ;
110+ $ errorMessage = \GuzzleHttp \json_decode (
111+ $ request ->getBody (),
112+ true
113+ )['message ' ] ?? 'API error ' ;
108114 throw $ this ->getException ($ request ->getStatusCode ())
109115 ->setMessage ($ errorMessage );
110116 }
111- return $ asJson ? \GuzzleHttp \json_decode ($ request ->getBody (),
112- true ) : $ request ->getBody ()->getContents ();
117+ return $ asJson ? \GuzzleHttp \json_decode (
118+ $ request ->getBody (),
119+ true
120+ ) : $ request ->getBody ()->getContents ();
121+ } catch (ClientException $ exception ) {
122+ if ($ exception ->getResponse ()->getStatusCode () === 408 ) {
123+ continue ;
124+ }
125+ throw $ this ->getException (
126+ $ exception ->getResponse ()->getStatusCode ()
127+ )->setMessage ($ exception ->getMessage ());
113128 } catch (RequestException $ exception ) {
114- continue ;
129+ if ($ exception ->getResponse ()->getStatusCode () === 408 ) {
130+ continue ;
131+ }
132+ throw $ this ->getException (
133+ $ exception ->getResponse ()->getStatusCode ()
134+ )->setMessage ($ exception ->getMessage ());
115135 } catch (GuzzleException $ e ) {
116136 continue ;
117137 } catch (TypesenseClientError $ exception ) {
@@ -136,20 +156,30 @@ public function post(string $endPoint, array $body): array
136156 {
137157 $ url = $ this ->config ->getMasterNode ()->url () . $ endPoint ;
138158 $ apiKey = $ this ->config ->getMasterNode ()->getApiKey ();
139-
140- $ request = $ this ->client ->post ($ url , [
141- 'headers ' => [
142- self ::API_KEY_HEADER_NAME => $ apiKey ,
143- ],
144- 'json ' => $ body ,
145- 'connect_timeout ' => $ this ->config ->getTimeoutSeconds (),
146- ]);
147- if ($ request ->getStatusCode () !== 201 ) {
148- $ errorMessage =
149- \GuzzleHttp \json_decode ($ request ->getBody (), true )['message ' ] ??
150- 'API error ' ;
151- throw $ this ->getException ($ request ->getStatusCode ())
152- ->setMessage ($ errorMessage );
159+ try {
160+ $ request = $ this ->client ->post (
161+ $ url ,
162+ [
163+ 'headers ' => [
164+ self ::API_KEY_HEADER_NAME => $ apiKey ,
165+ ],
166+ 'json ' => $ body ,
167+ 'connect_timeout ' => $ this ->config ->getTimeoutSeconds (),
168+ ]
169+ );
170+ if ($ request ->getStatusCode () !== 201 ) {
171+ $ errorMessage =
172+ \GuzzleHttp \json_decode ($ request ->getBody (), true )['message ' ]
173+ ?? 'API error ' ;
174+ throw $ this ->getException ($ request ->getStatusCode ())
175+ ->setMessage (
176+ $ errorMessage
177+ );
178+ }
179+ } catch (ClientException $ exception ) {
180+ throw $ this ->getException (
181+ $ exception ->getResponse ()->getStatusCode ()
182+ )->setMessage ($ exception ->getMessage ());
153183 }
154184
155185 return \GuzzleHttp \json_decode ($ request ->getBody (), true );
@@ -167,20 +197,30 @@ public function put(string $endPoint, array $body): array
167197 {
168198 $ url = $ this ->config ->getMasterNode ()->url () . $ endPoint ;
169199 $ apiKey = $ this ->config ->getMasterNode ()->getApiKey ();
170-
171- $ request = $ this ->client ->put ($ url , [
172- 'headers ' => [
173- self ::API_KEY_HEADER_NAME => $ apiKey ,
174- ],
175- 'json ' => $ body ,
176- 'connect_timeout ' => $ this ->config ->getTimeoutSeconds (),
177- ]);
178- if ($ request ->getStatusCode () !== 200 ) {
179- $ errorMessage =
180- \GuzzleHttp \json_decode ($ request ->getBody (), true )['message ' ] ??
181- 'API error ' ;
182- throw $ this ->getException ($ request ->getStatusCode ())
183- ->setMessage ($ errorMessage );
200+ try {
201+ $ request = $ this ->client ->put (
202+ $ url ,
203+ [
204+ 'headers ' => [
205+ self ::API_KEY_HEADER_NAME => $ apiKey ,
206+ ],
207+ 'json ' => $ body ,
208+ 'connect_timeout ' => $ this ->config ->getTimeoutSeconds (),
209+ ]
210+ );
211+ if ($ request ->getStatusCode () !== 200 ) {
212+ $ errorMessage =
213+ \GuzzleHttp \json_decode ($ request ->getBody (), true )['message ' ]
214+ ?? 'API error ' ;
215+ throw $ this ->getException ($ request ->getStatusCode ())
216+ ->setMessage (
217+ $ errorMessage
218+ );
219+ }
220+ } catch (ClientException $ exception ) {
221+ throw $ this ->getException (
222+ $ exception ->getResponse ()->getStatusCode ()
223+ )->setMessage ($ exception ->getMessage ());
184224 }
185225
186226 return \GuzzleHttp \json_decode ($ request ->getBody (), true );
@@ -197,21 +237,30 @@ public function delete(string $endPoint): array
197237 {
198238 $ url = $ this ->config ->getMasterNode ()->url () . $ endPoint ;
199239 $ apiKey = $ this ->config ->getMasterNode ()->getApiKey ();
200-
201- $ request = $ this ->client ->delete ($ url , [
202- 'headers ' => [
203- self ::API_KEY_HEADER_NAME => $ apiKey ,
204- ],
205- 'connect_timeout ' => $ this ->config ->getTimeoutSeconds (),
206- ]);
207- if ($ request ->getStatusCode () !== 200 ) {
208- $ errorMessage =
209- \GuzzleHttp \json_decode ($ request ->getBody (), true )['message ' ] ??
210- 'API error ' ;
211- throw $ this ->getException ($ request ->getStatusCode ())
212- ->setMessage ($ errorMessage );
240+ try {
241+ $ request = $ this ->client ->delete (
242+ $ url ,
243+ [
244+ 'headers ' => [
245+ self ::API_KEY_HEADER_NAME => $ apiKey ,
246+ ],
247+ 'connect_timeout ' => $ this ->config ->getTimeoutSeconds (),
248+ ]
249+ );
250+ if ($ request ->getStatusCode () !== 200 ) {
251+ $ errorMessage =
252+ \GuzzleHttp \json_decode ($ request ->getBody (), true )['message ' ]
253+ ?? 'API error ' ;
254+ throw $ this ->getException ($ request ->getStatusCode ())
255+ ->setMessage (
256+ $ errorMessage
257+ );
258+ }
259+ } catch (ClientException $ exception ) {
260+ throw $ this ->getException (
261+ $ exception ->getResponse ()->getStatusCode ()
262+ )->setMessage ($ exception ->getMessage ());
213263 }
214-
215264 return \GuzzleHttp \json_decode ($ request ->getBody (), true );
216265 }
217266
0 commit comments