22namespace Noweh \TwitterApi ;
33
44use GuzzleHttp \Client ;
5+ use GuzzleHttp \Exception \GuzzleException ;
6+ use GuzzleHttp \Exception \RequestException ;
57use GuzzleHttp \Exception \ServerException ;
68use GuzzleHttp \Subscriber \Oauth \Oauth1 ;
79use GuzzleHttp \HandlerStack ;
@@ -86,10 +88,10 @@ private function getAPIBaseURI(): string
8688 /**
8789 * Perform the request to Twitter API
8890 * @param array<string, mixed> $postData
89- * @return mixed
90- * @throws \GuzzleHttp\Exception\ GuzzleException|\RuntimeException|\JsonException
91+ * @return \stdClass|null
92+ * @throws GuzzleException|\RuntimeException|\JsonException
9193 */
92- public function performRequest (array $ postData = [], $ withHeaders = false )
94+ public function performRequest (array $ postData = [], bool $ withHeaders = false ): ? \ stdClass
9395 {
9496 try {
9597 $ headers = [
@@ -128,9 +130,10 @@ public function performRequest(array $postData = [], $withHeaders = false)
128130 'json ' => count ($ postData ) ? $ postData : null ,
129131 ]);
130132
133+ /** @var \stdClass|null $body */
131134 $ body = json_decode ($ response ->getBody ()->getContents (), false , 512 , JSON_THROW_ON_ERROR );
132135
133- if ($ withHeaders ) {
136+ if ($ withHeaders && $ body ) {
134137 $ body ->headers = $ response ->getHeaders ();
135138 }
136139
@@ -148,12 +151,11 @@ public function performRequest(array $postData = [], $withHeaders = false)
148151 return $ body ;
149152
150153 } catch (ServerException $ e ) {
151- /** @var \stdClass $payload */
152- $ payload = json_decode (str_replace ("\n" , "" , $ e ->getResponse ()->getBody ()->getContents ()), false , 512 ,
153- JSON_THROW_ON_ERROR );
154- throw new \RuntimeException ($ payload ->detail , $ payload ->status );
155- } catch (\GuzzleHttp \Exception \RequestException $ e ) {
156- throw new \RuntimeException ($ e ->getResponse ()->getBody ()->getContents (), $ e ->getCode ());
154+ /** @var \stdClass|null $payload */
155+ $ payload = json_decode ($ e ->getResponse ()->getBody ()->getContents (), false , 512 , JSON_THROW_ON_ERROR );
156+ throw new \RuntimeException ($ payload ->detail ?? $ e ->getMessage (), $ payload ->status ?? $ e ->getCode ());
157+ } catch (RequestException $ e ) {
158+ throw new \RuntimeException ($ e ->getMessage (), $ e ->getCode ());
157159 }
158160 }
159161
@@ -213,7 +215,7 @@ private function parseSettings(array $settings): void
213215 $ this ->bearer_token = $ settings ['bearer_token ' ];
214216 $ this ->access_token = $ settings ['access_token ' ];
215217 $ this ->access_token_secret = $ settings ['access_token_secret ' ];
216- $ this ->free_mode = $ settings ['free_mode ' ] ?? false ;
218+ $ this ->free_mode = ( bool ) ( $ settings ['free_mode ' ] ?? false ) ;
217219 $ this ->api_base_uri = $ settings ['api_base_uri ' ] ?? self ::API_BASE_URI ;
218220 }
219221
0 commit comments