From 31b32a1b9fa838d7ff08202ccfde7a8f052f84a0 Mon Sep 17 00:00:00 2001 From: Abraham Williams <4braham@gmail.com> Date: Sun, 11 Feb 2024 18:27:38 +0000 Subject: [PATCH] Switch to using options argument --- src/Request.php | 7 +- src/TwitterOAuth.php | 138 ++++++++++++----------- tests/TwitterOAuthDirectMessagesTest.php | 8 +- tests/TwitterOAuthMediaTest.php | 4 +- 4 files changed, 82 insertions(+), 75 deletions(-) diff --git a/src/Request.php b/src/Request.php index dc502d11..8d2f982b 100644 --- a/src/Request.php +++ b/src/Request.php @@ -14,7 +14,6 @@ class Request protected $parameters; protected $httpMethod; protected $httpUrl; - protected $json; public static $version = '1.0'; /** @@ -22,7 +21,7 @@ class Request * * @param string $httpMethod * @param string $httpUrl - * @param array|null $parameters + * @param ?array $parameters */ public function __construct( string $httpMethod, @@ -55,7 +54,7 @@ public static function fromConsumerAndToken( string $httpMethod, string $httpUrl, array $parameters = [], - $json = false, + array $options = [], ) { $defaults = [ 'oauth_version' => Request::$version, @@ -69,7 +68,7 @@ public static function fromConsumerAndToken( // The json payload is not included in the signature on json requests, // therefore it shouldn't be included in the parameters array. - if ($json) { + if ($options['jsonPayload'] ?? false) { $parameters = $defaults; } else { $parameters = array_merge($defaults, $parameters); diff --git a/src/TwitterOAuth.php b/src/TwitterOAuth.php index 6e3748f0..099a8286 100644 --- a/src/TwitterOAuth.php +++ b/src/TwitterOAuth.php @@ -234,7 +234,9 @@ public function oauth2(string $path, array $parameters = []) */ public function get(string $path, array $parameters = []) { - return $this->http('GET', self::API_HOST, $path, $parameters, false); + return $this->http('GET', self::API_HOST, $path, $parameters, [ + 'jsonPayload' => false, + ]); } /** @@ -242,20 +244,26 @@ public function get(string $path, array $parameters = []) * * @param string $path * @param array $parameters - * @param ?bool $json + * @param array $options * * @return array|object */ public function post( string $path, array $parameters = [], - ?bool $json = null, + array $options = [], ) { - if (is_null($json)) { - $json = $this->useJsonBody(); + if (!isset($options['jsonPayload'])) { + $options['jsonPayload'] = $this->useJsonBody(); } - return $this->http('POST', self::API_HOST, $path, $parameters, $json); + return $this->http( + 'POST', + self::API_HOST, + $path, + $parameters, + $options, + ); } /** @@ -268,7 +276,9 @@ public function post( */ public function delete(string $path, array $parameters = []) { - return $this->http('DELETE', self::API_HOST, $path, $parameters, false); + return $this->http('DELETE', self::API_HOST, $path, $parameters, [ + 'jsonPayload' => false, + ]); } /** @@ -276,20 +286,20 @@ public function delete(string $path, array $parameters = []) * * @param string $path * @param array $parameters - * @param ?bool $json + * @param array $options * * @return array|object */ public function put( string $path, array $parameters = [], - ?bool $json = null, + array $options = [], ) { - if (is_null($json)) { - $json = $this->useJsonBody(); + if (!isset($options['jsonPayload'])) { + $options['jsonPayload'] = $this->useJsonBody(); } - return $this->http('PUT', self::API_HOST, $path, $parameters, $json); + return $this->http('PUT', self::API_HOST, $path, $parameters, $options); } /** @@ -297,16 +307,16 @@ public function put( * * @param string $path * @param array $parameters - * @param boolean $chunked + * @param array $options * * @return array|object */ public function upload( string $path, array $parameters = [], - bool $chunked = false, + array $options = [], ) { - if ($chunked) { + if ($options['chunkedUpload'] ?? false) { return $this->uploadMediaChunked($path, $parameters); } else { return $this->uploadMediaNotChunked($path, $parameters); @@ -330,7 +340,7 @@ public function mediaStatus(string $media_id) 'command' => 'STATUS', 'media_id' => $media_id, ], - false, + ['jsonPayload' => false], ); } @@ -353,13 +363,9 @@ private function uploadMediaNotChunked(string $path, array $parameters) ); } $parameters['media'] = base64_encode($file); - return $this->http( - 'POST', - self::UPLOAD_HOST, - $path, - $parameters, - false, - ); + return $this->http('POST', self::UPLOAD_HOST, $path, $parameters, [ + 'jsonPayload' => false, + ]); } /** @@ -378,7 +384,7 @@ private function uploadMediaChunked(string $path, array $parameters) self::UPLOAD_HOST, $path, $this->mediaInitParameters($parameters), - false, + ['jsonPayload' => false], ); if (!property_exists($init, 'media_id_string')) { throw new TwitterOAuthException('Missing "media_id_string"'); @@ -399,7 +405,7 @@ private function uploadMediaChunked(string $path, array $parameters) fread($media, $this->chunkSize), ), ], - false, + ['jsonPayload' => false], ); } fclose($media); @@ -412,7 +418,7 @@ private function uploadMediaChunked(string $path, array $parameters) 'command' => 'FINALIZE', 'media_id' => $init->media_id_string, ], - false, + ['jsonPayload' => false], ); return $finalize; } @@ -492,8 +498,8 @@ private function useJsonBody() * @param string $method * @param string $host * @param string $path - * @param array $parameters - * @param bool $json + * @param array $parameters + * @param array $options * * @return array|object */ @@ -502,19 +508,19 @@ private function http( string $host, string $path, array $parameters, - bool $json, + array $options, ) { $this->resetLastResponse(); $this->resetAttemptsNumber(); $this->response->setApiPath($path); - if (!$json) { + if (!$options['jsonPayload']) { $parameters = $this->cleanUpParameters($parameters); } return $this->makeRequests( $this->apiUrl($host, $path), $method, $parameters, - $json, + $options, ); } @@ -546,8 +552,8 @@ protected function apiUrl(string $host, string $path) * @param string $method * @param string $url * @param string $method - * @param array $parameters - * @param bool $json + * @param array $parameters + * @param array $options * * @return array|object */ @@ -555,11 +561,11 @@ private function makeRequests( string $url, string $method, array $parameters, - bool $json, + array $options, ) { do { $this->sleepIfNeeded(); - $result = $this->oAuthRequest($url, $method, $parameters, $json); + $result = $this->oAuthRequest($url, $method, $parameters, $options); $response = JsonDecoder::decode($result, $this->decodeJsonAsArray); $this->response->setBody($response); $this->attempts++; @@ -586,8 +592,8 @@ private function requestsAvailable(): bool * * @param string $url * @param string $method - * @param array $parameters - * @param bool $json + * @param array $parameters + * @param array $options * * @return string * @throws TwitterOAuthException @@ -596,7 +602,7 @@ private function oAuthRequest( string $url, string $method, array $parameters, - bool $json = false, + array $options = [], ) { $request = Request::fromConsumerAndToken( $this->consumer, @@ -604,7 +610,7 @@ private function oAuthRequest( $method, $url, $parameters, - $json, + $options, ); if (array_key_exists('oauth_callback', $parameters)) { // Twitter doesn't like oauth_callback as a parameter. @@ -630,7 +636,7 @@ private function oAuthRequest( $method, $authorization, $parameters, - $json, + $options, ); } @@ -677,7 +683,7 @@ private function curlOptions(): array * @param string $method * @param string $authorization * @param array $postfields - * @param bool $json + * @param array $options * * @return string * @throws TwitterOAuthException @@ -687,11 +693,11 @@ private function request( string $method, string $authorization, array $postfields, - bool $json = false, + array $options, ): string { - $options = $this->curlOptions(); - $options[CURLOPT_URL] = $url; - $options[CURLOPT_HTTPHEADER] = [ + $curlOptions = $this->curlOptions(); + $curlOptions[CURLOPT_URL] = $url; + $curlOptions[CURLOPT_HTTPHEADER] = [ 'Accept: application/json', $authorization, 'Expect:', @@ -701,22 +707,22 @@ private function request( case 'GET': break; case 'POST': - $options[CURLOPT_POST] = true; - $options = $this->setPostfieldsOptions( - $options, + $curlOptions[CURLOPT_POST] = true; + $curlOptions = $this->setPostfieldsOptions( + $curlOptions, $postfields, - $json, + $options, ); break; case 'DELETE': - $options[CURLOPT_CUSTOMREQUEST] = 'DELETE'; + $curlOptions[CURLOPT_CUSTOMREQUEST] = 'DELETE'; break; case 'PUT': - $options[CURLOPT_CUSTOMREQUEST] = 'PUT'; - $options = $this->setPostfieldsOptions( - $options, + $curlOptions[CURLOPT_CUSTOMREQUEST] = 'PUT'; + $curlOptions = $this->setPostfieldsOptions( + $curlOptions, $postfields, - $json, + $options, ); break; } @@ -725,11 +731,12 @@ private function request( in_array($method, ['GET', 'PUT', 'DELETE']) && !empty($postfields) ) { - $options[CURLOPT_URL] .= '?' . Util::buildHttpQuery($postfields); + $curlOptions[CURLOPT_URL] .= + '?' . Util::buildHttpQuery($postfields); } $curlHandle = curl_init(); - curl_setopt_array($curlHandle, $options); + curl_setopt_array($curlHandle, $curlOptions); $response = curl_exec($curlHandle); // Throw exceptions on cURL errors. @@ -803,25 +810,28 @@ private function curlCaOpt(string $path): int * * @param array $options * @param array $postfields - * @param bool $json + * @param array $options * * @return array */ private function setPostfieldsOptions( - array $options, + array $curlOptions, array $postfields, - bool $json, + array $options, ): array { - if ($json) { - $options[CURLOPT_HTTPHEADER][] = 'Content-type: application/json'; - $options[CURLOPT_POSTFIELDS] = json_encode( + if ($options['jsonPayload']) { + $curlOptions[CURLOPT_HTTPHEADER][] = + 'Content-type: application/json'; + $curlOptions[CURLOPT_POSTFIELDS] = json_encode( $postfields, JSON_THROW_ON_ERROR, ); } else { - $options[CURLOPT_POSTFIELDS] = Util::buildHttpQuery($postfields); + $curlOptions[CURLOPT_POSTFIELDS] = Util::buildHttpQuery( + $postfields, + ); } - return $options; + return $curlOptions; } } diff --git a/tests/TwitterOAuthDirectMessagesTest.php b/tests/TwitterOAuthDirectMessagesTest.php index fab15a11..28901576 100644 --- a/tests/TwitterOAuthDirectMessagesTest.php +++ b/tests/TwitterOAuthDirectMessagesTest.php @@ -46,11 +46,9 @@ public function testPostDirectMessagesEventsNew() ], ], ]; - $result = $this->twitter->post( - 'direct_messages/events/new', - $data, - true, - ); + $result = $this->twitter->post('direct_messages/events/new', $data, [ + 'jsonPayload' => true, + ]); $this->assertEquals(200, $this->twitter->getLastHttpCode()); return $result; } diff --git a/tests/TwitterOAuthMediaTest.php b/tests/TwitterOAuthMediaTest.php index cf7e5684..30e6d5a7 100644 --- a/tests/TwitterOAuthMediaTest.php +++ b/tests/TwitterOAuthMediaTest.php @@ -75,7 +75,7 @@ public function testPostStatusesUpdateWithMediaChunked() $result = $this->twitter->upload( 'media/upload', ['media' => $file_path, 'media_type' => 'video/mp4'], - true, + ['chunkedUpload' => true], ); $this->assertEquals(201, $this->twitter->getLastHttpCode()); $this->assertObjectHasAttribute('media_id_string', $result); @@ -103,7 +103,7 @@ public function testPostStatusesUpdateWithMediaChunkedException() $result = $this->twitter->upload( 'media/upload', ['media' => $file_path, 'media_type' => 'video/mp4'], - true, + ['chunkedUpload' => true], ); } }