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 31b32a1
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 75 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 = [],
) {
$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'] ?? false) {
$parameters = $defaults;
} else {
$parameters = array_merge($defaults, $parameters);
Expand Down
138 changes: 74 additions & 64 deletions src/TwitterOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -234,28 +234,36 @@ 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,
]);
}

/**
* Make POST requests to the API.
*
* @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,
);
}

/**
Expand All @@ -268,45 +276,47 @@ 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,
]);
}

/**
* Make PUT requests to the API.
*
* @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);
}

/**
* Upload media to upload.twitter.com.
*
* @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);
Expand All @@ -330,7 +340,7 @@ public function mediaStatus(string $media_id)
'command' => 'STATUS',
'media_id' => $media_id,
],
false,
['jsonPayload' => false],
);
}

Expand All @@ -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,
]);
}

/**
Expand All @@ -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"');
Expand All @@ -399,7 +405,7 @@ private function uploadMediaChunked(string $path, array $parameters)
fread($media, $this->chunkSize),
),
],
false,
['jsonPayload' => false],
);
}
fclose($media);
Expand All @@ -412,7 +418,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 +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
*/
Expand All @@ -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,
);
}

Expand Down Expand Up @@ -546,20 +552,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 +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
Expand All @@ -596,15 +602,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 +636,7 @@ private function oAuthRequest(
$method,
$authorization,
$parameters,
$json,
$options,
);
}

Expand Down Expand Up @@ -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
Expand All @@ -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:',
Expand All @@ -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;
}
Expand All @@ -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.
Expand Down Expand Up @@ -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;
}
}
Loading

0 comments on commit 31b32a1

Please sign in to comment.