Skip to content

Commit

Permalink
Switch to using options argument
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham committed Feb 11, 2024
1 parent b56c62a commit f27db86
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 60 deletions.
7 changes: 3 additions & 4 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,14 @@ class Request
protected $parameters;
protected $httpMethod;
protected $httpUrl;
protected $json;
public static $version = '1.0';

/**
* Constructor
*
* @param string $httpMethod
* @param string $httpUrl
* @param array|null $parameters
* @param ?array $parameters
*/
public function __construct(
string $httpMethod,
Expand Down Expand Up @@ -55,7 +54,7 @@ public static function fromConsumerAndToken(
string $httpMethod,
string $httpUrl,
array $parameters = [],
$json = false,
array $options = ['jsonPayload' => false],
) {
$defaults = [
'oauth_version' => Request::$version,
Expand All @@ -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']) {
$parameters = $defaults;
} else {
$parameters = array_merge($defaults, $parameters);
Expand Down
106 changes: 53 additions & 53 deletions src/TwitterOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -242,20 +242,20 @@ 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 (is_null($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);
}

/**
Expand All @@ -276,20 +276,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 (is_null($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);
}

/**
Expand Down Expand Up @@ -330,7 +330,7 @@ public function mediaStatus(string $media_id)
'command' => 'STATUS',
'media_id' => $media_id,
],
false,
['jsonPayload' => false],
);
}

Expand Down Expand Up @@ -358,7 +358,7 @@ private function uploadMediaNotChunked(string $path, array $parameters)
self::UPLOAD_HOST,
$path,
$parameters,
false,
['jsonPayload' => false],
);
}

Expand All @@ -378,7 +378,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"');
Expand All @@ -399,7 +399,7 @@ private function uploadMediaChunked(string $path, array $parameters)
fread($media, $this->chunkSize),
),
],
false,
['jsonPayload' => false],
);
}
fclose($media);
Expand All @@ -412,7 +412,7 @@ private function uploadMediaChunked(string $path, array $parameters)
'command' => 'FINALIZE',
'media_id' => $init->media_id_string,
],
false,
['jsonPayload' => false],
);
return $finalize;
}
Expand Down Expand Up @@ -492,8 +492,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
*/
Expand All @@ -502,19 +502,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,
);
}

Expand Down Expand Up @@ -546,20 +546,20 @@ 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
*/
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++;
Expand All @@ -586,8 +586,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
Expand All @@ -596,15 +596,15 @@ private function oAuthRequest(
string $url,
string $method,
array $parameters,
bool $json = false,
array $options = [],
) {
$request = Request::fromConsumerAndToken(
$this->consumer,
$this->token,
$method,
$url,
$parameters,
$json,
$options,
);
if (array_key_exists('oauth_callback', $parameters)) {
// Twitter doesn't like oauth_callback as a parameter.
Expand All @@ -630,7 +630,7 @@ private function oAuthRequest(
$method,
$authorization,
$parameters,
$json,
$options,
);
}

Expand Down Expand Up @@ -677,7 +677,7 @@ private function curlOptions(): array
* @param string $method
* @param string $authorization
* @param array $postfields
* @param bool $json
* @param array $options
*
* @return string
* @throws TwitterOAuthException
Expand All @@ -687,11 +687,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:',
Expand All @@ -701,22 +701,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;
}
Expand All @@ -725,11 +725,11 @@ 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.
Expand Down Expand Up @@ -803,25 +803,25 @@ 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;
}
}
2 changes: 1 addition & 1 deletion tests/TwitterOAuthDirectMessagesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testPostDirectMessagesEventsNew()
$result = $this->twitter->post(
'direct_messages/events/new',
$data,
true,
['jsonPayload' => true],
);
$this->assertEquals(200, $this->twitter->getLastHttpCode());
return $result;
Expand Down
4 changes: 2 additions & 2 deletions tests/TwitterOAuthMediaTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public function testPostStatusesUpdateWithMediaChunked()
$result = $this->twitter->upload(
'media/upload',
['media' => $file_path, 'media_type' => 'video/mp4'],
true,
['jsonPayload' => true],
);
$this->assertEquals(201, $this->twitter->getLastHttpCode());
$this->assertObjectHasAttribute('media_id_string', $result);
Expand Down Expand Up @@ -103,7 +103,7 @@ public function testPostStatusesUpdateWithMediaChunkedException()
$result = $this->twitter->upload(
'media/upload',
['media' => $file_path, 'media_type' => 'video/mp4'],
true,
['jsonPayload' => true],
);
}
}

0 comments on commit f27db86

Please sign in to comment.