Skip to content

Commit

Permalink
Add support for API v2
Browse files Browse the repository at this point in the history
  • Loading branch information
abraham committed Jul 18, 2021
1 parent aff45e3 commit d6b3b50
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 2 deletions.
19 changes: 19 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
*/
class Config
{
// Update extension function when updating this list.
private const SUPPORTED_VERSIONS = ['1.1', '2'];

/** @var int How long to wait for a response from the API */
protected $timeout = 5;
/** @var int how long to wait while connecting to the API */
Expand All @@ -19,6 +22,8 @@ class Config
protected $maxRetries = 0;
/** @var int Delay in seconds before we retry the request */
protected $retriesDelay = 1;
/** @var string Version of the Twitter API requests should target */
protected $apiVersion = '1.1';

/**
* Decode JSON Response as associative Array
Expand All @@ -39,6 +44,20 @@ class Config
/** @var integer Size for Chunked Uploads */
protected $chunkSize = 250000; // 0.25 MegaByte

/**
* Set the the Twitter API version.
*
* @param string $apiVersion
*/
public function setApiVersion(string $apiVersion): void
{
if (in_array($apiVersion, self::SUPPORTED_VERSIONS, true)) {
$this->apiVersion = $apiVersion;
} else {
throw new TwitterOAuthException('Unsupported API version');
}
}

/**
* Set the connection and response timeouts.
*
Expand Down
22 changes: 20 additions & 2 deletions src/TwitterOAuth.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
*/
class TwitterOAuth extends Config
{
private const API_VERSION = '1.1';
private const API_HOST = 'https://api.twitter.com';
private const UPLOAD_HOST = 'https://upload.twitter.com';

Expand Down Expand Up @@ -441,6 +440,19 @@ private function cleanUpParameters(array $parameters)
return $parameters;
}

/**
* Get URL extension for current API Version.
*
* @return string
*/
private function extension()
{
return [
'1.1' => '.json',
'2' => '',
][$this->apiVersion];
}

/**
* @param string $method
* @param string $host
Expand All @@ -459,7 +471,13 @@ private function http(
) {
$this->resetLastResponse();
$this->resetAttemptsNumber();
$url = sprintf('%s/%s/%s.json', $host, self::API_VERSION, $path);
$url = sprintf(
'%s/%s/%s%s',
$host,
$this->apiVersion,
$path,
$this->extension(),
);
$this->response->setApiPath($path);
if (!$json) {
$parameters = $this->cleanUpParameters($parameters);
Expand Down
40 changes: 40 additions & 0 deletions tests/TwitterOAuthV2Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

/**
* WARNING: Running tests will post and delete through the actual Twitter account when updating or saving VCR cassettes.
*/

declare(strict_types=1);

namespace Abraham\TwitterOAuth\Test;

use PHPUnit\Framework\TestCase;
use Abraham\TwitterOAuth\TwitterOAuth;

class TwitterOAuthV2Test extends TestCase
{
/** @var TwitterOAuth */
protected $twitter;

protected function setUp(): void
{
$this->markTestSkipped('Fixtures need to be updated');
$this->twitter = new TwitterOAuth(
CONSUMER_KEY,
CONSUMER_SECRET,
ACCESS_TOKEN,
ACCESS_TOKEN_SECRET,
);
$this->userId = explode('-', ACCESS_TOKEN)[0];
$this->twitter->setApiVersion('2');
}

/**
* @vcr testV2GetUsers.json
*/
public function testV2GetUsers()
{
$this->twitter->get('users', ['ids' => 12]);
$this->assertEquals(200, $this->twitter->getLastHttpCode());
}
}
1 change: 1 addition & 0 deletions tests/fixtures/testV2GetUsers.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[]

0 comments on commit d6b3b50

Please sign in to comment.