44
55use GuzzleHttp \Client ;
66use GuzzleHttp \Exception \GuzzleException ;
7- use GuzzleHttp \Psr7 \Response ;
87use InvalidArgumentException ;
98use JsonException ;
109
@@ -568,23 +567,25 @@ private function _request(
568567 throw new InvalidRequestException ($ e ->getMessage (), $ e ->getCode (), $ e );
569568 }
570569
570+ $ contents = $ response ->getBody ()->getContents ();
571+
571572 // Validate if the response was successful
572573 if ($ response ->getStatusCode () !== 200 ) {
573- throw new InvalidRequestException ($ response -> getBody ()-> getContents () ,
574+ throw new InvalidRequestException ($ contents ,
574575 $ response ->getStatusCode ());
575576 }
576577
577578 // Sometimes the response was unsuccessful, but the status code was 200
578579 if ($ format === self ::FORMAT_JSON ) {
579- $ valid = $ this ->_isValidResponse ($ response );
580+ $ valid = $ this ->_isValidResponse ($ contents );
580581
581582 if ($ valid !== true ) {
582583 throw new InvalidResponseException ($ valid .' ( ' .$ this ->_parseUrl ($ method , $ params )
583584 .') ' , 403 );
584585 }
585586 }
586587
587- return $ this ->_parseResponse ($ response , $ format );
588+ return $ this ->_parseResponse ($ contents , $ format );
588589 }
589590
590591 /**
@@ -654,20 +655,18 @@ private function _parseUrl(string $method, array $params = []): string
654655 /**
655656 * Check if the request was successful.
656657 *
657- * @param mixed $response
658+ * @param string $contents
658659 *
659660 * @return bool|string
660661 * @throws JsonException
661662 */
662- private function _isValidResponse (Response $ response ): bool |string
663+ private function _isValidResponse (string $ contents ): bool |string
663664 {
664- $ body = $ response ->getBody ()->getContents ();
665-
666- if ($ body === '' ) {
665+ if ($ contents === '' ) {
667666 return 'Empty response! ' ;
668667 }
669668
670- $ result = json_decode ($ body , true , 512 , JSON_THROW_ON_ERROR );
669+ $ result = json_decode ($ contents , true , 512 , JSON_THROW_ON_ERROR );
671670
672671 if (isset ($ result ['result ' ]) && (strtolower ($ result ['result ' ]) === 'error ' )) {
673672 return $ result ['message ' ];
@@ -679,21 +678,21 @@ private function _isValidResponse(Response $response): bool|string
679678 /**
680679 * Parse request result
681680 *
682- * @param Response $response
681+ * @param string $contents
683682 * @param string|null $overrideFormat Override the default format
684683 *
685684 * @return mixed Either the parsed response body object (parsed from json) or the raw response object.
686685 * @throws JsonException
687686 */
688- private function _parseResponse (Response $ response , string $ overrideFormat = null ): mixed
687+ private function _parseResponse (string $ contents , string $ overrideFormat = null ): mixed
689688 {
690689 $ format = $ overrideFormat ?? $ this ->_format ;
691690
692691 return match ($ format ) {
693- self ::FORMAT_JSON => json_decode ($ response -> getBody ()-> getContents () ,
692+ self ::FORMAT_JSON => json_decode ($ contents ,
694693 $ this ->_isJsonDecodeAssoc , 512 ,
695694 JSON_THROW_ON_ERROR ),
696- default => $ response ,
695+ default => $ contents ,
697696 };
698697 }
699698
@@ -2677,7 +2676,7 @@ public function getVisitsUntilConversion(
26772676 * value without '#'
26782677 * @param array $optional
26792678 *
2680- * @return bool|object
2679+ * @return bool|string
26812680 * @@throws InvalidRequestException|JsonException|InvalidResponseException
26822681 */
26832682 public function getImageGraph (
0 commit comments