Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: unable to switch API version on service #355 #356

Merged
merged 1 commit into from
Apr 27, 2021

Conversation

reliq
Copy link
Collaborator

@reliq reliq commented Apr 27, 2021

This exposes forApiV1() and forApiV2 methods which were not previously available on Twitter service.

Fixes: #355

@codecov
Copy link

codecov bot commented Apr 27, 2021

Codecov Report

Merging #356 (79ea57a) into main (a24e24d) will decrease coverage by 0.21%.
The diff coverage is 47.82%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main     #356      +/-   ##
============================================
- Coverage     34.70%   34.48%   -0.22%     
- Complexity      359      361       +2     
============================================
  Files            31       32       +1     
  Lines           804      812       +8     
============================================
+ Hits            279      280       +1     
- Misses          525      532       +7     
Impacted Files Coverage Δ Complexity Δ
src/ApiV1/Service/Twitter.php 10.00% <0.00%> (+1.66%) 8.00 <1.00> (-1.00) ⬆️
src/Service/Accessor.php 57.14% <50.00%> (-42.86%) 3.00 <2.00> (-1.00)
src/Concern/HotSwapper.php 52.94% <52.94%> (ø) 4.00 <4.00> (?)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update a24e24d...79ea57a. Read the comment docs.

@reliq reliq changed the title fix: unable to switch API version on singleton #355 fix: unable to switch API version on service #355 Apr 27, 2021
@reliq reliq merged commit fabbddf into main Apr 27, 2021
@reliq reliq deleted the fix-unable-to-switch-api-version branch April 27, 2021 18:54
@Cosnavel
Copy link

Great work on this 🚀

@Cosnavel
Copy link

Hey @reliq, sorry to bother you again. The PR does not fix the issue entirely.

$twitterV1 =  Twitter::forApiV1()->usingCredentials($token, $tokenSecret, env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'));
$twitterV2 =   Twitter::forApiV2()->usingCredentials($token, $tokenSecret, env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'));

dump($twitterV1);
dd($twitterV2);

I have set the TWITTER_API_VERSION to 1.1 in the .env.

Response:

Atymic\Twitter\ApiV1\Service\Twitter {#1430 ▼
  #config: Atymic\Twitter\Configuration {#1548 ▼
    #apiUrl: "api.twitter.com"
    #uploadUrl: "upload.twitter.com"
    #apiVersion: "1.1"
    #consumerKey: "*"
    #consumerSecret: "*"
    #accessToken: "*"
    #accessTokenSecret: "*"
    #debugMode: true
    #userAgent: "atymic/twitter v3.x-dev php v8.0.0"
    -authenticateUrl: "https://api.twitter.com/oauth/authenticate"
    -accessTokenUrl: "https://api.twitter.com/oauth/access_token"
    -requestTokenUrl: "https://api.twitter.com/oauth/request_token"
  }
  #querier: Atymic\Twitter\Service\Querier {#1550 ▶}
  #debug: true
}
Atymic\Twitter\ApiV1\Service\Twitter {#1432 ▼
  #config: Atymic\Twitter\Configuration {#1590 ▼
    #apiUrl: "api.twitter.com"
    #uploadUrl: "upload.twitter.com"
    #apiVersion: "2"
    #consumerKey: "*"
    #consumerSecret: "*"
    #accessToken: "*"
    #accessTokenSecret: "*"
    #debugMode: true
    #userAgent: "atymic/twitter v3.x-dev php v8.0.0"
    -authenticateUrl: "https://api.twitter.com/oauth/authenticate"
    -accessTokenUrl: "https://api.twitter.com/oauth/access_token"
    -requestTokenUrl: "https://api.twitter.com/oauth/request_token"
  }
  #querier: Atymic\Twitter\Service\Querier {#1592 ▶}
  #debug: true
}

Both Instances are Atymic\Twitter\ApiV1\Service\Twitter. The V2 Version should be a V2 Instance. Because of that I cannot access the Methods for the V2:

$twitterV1 =  $this->getTwitterInstanceV1($token, $tokenSecret, true);
$twitterV2 =   $this->getTwitterInstanceV2($token, $tokenSecret, true);

$twitterV2->searchRecent('to:' . 'cosnavel', []);

Response

Error
Call to undefined method Atymic\Twitter\ApiV1\Service\Twitter::searchRecent()

This has the same behavior vice versa.

Now with the API Version 2 set in the .env

$twitterV1 =  Twitter::forApiV1()->usingCredentials($token, $tokenSecret, env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'));
$twitterV2 =   Twitter::forApiV2()->usingCredentials($token, $tokenSecret, env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'));

dump($twitterV1);
dd($twitterV2);

Response:

Atymic\Twitter\Service\Accessor {#1430 ▼
  -querier: Atymic\Twitter\Service\Querier {#1550 ▼
    -config: Atymic\Twitter\Configuration {#1548 ▼
      #apiUrl: "api.twitter.com"
      #uploadUrl: "upload.twitter.com"
      #apiVersion: "1.1"
      #consumerKey: "*"
      #consumerSecret: "*"
      #accessToken: "*"
      #accessTokenSecret: "*"
      #debugMode: true
      #userAgent: "atymic/twitter v3.x-dev php v8.0.0"
      -authenticateUrl: "https://api.twitter.com/oauth/authenticate"
      -accessTokenUrl: "https://api.twitter.com/oauth/access_token"
      -requestTokenUrl: "https://api.twitter.com/oauth/request_token"
    }
    -clientFactory: Atymic\Twitter\Http\Factory\ClientCreator {#1443 ▶}
    -syncClient: Atymic\Twitter\Http\Client\SyncClient {#1552 ▶}
    -asyncClient: Atymic\Twitter\Http\Client\AsyncClient {#1549 ▶}
    -logger: null
  }
}
Atymic\Twitter\Service\Accessor {#1432 ▼
  -querier: Atymic\Twitter\Service\Querier {#1592 ▼
    -config: Atymic\Twitter\Configuration {#1590 ▼
      #apiUrl: "api.twitter.com"
      #uploadUrl: "upload.twitter.com"
      #apiVersion: "2"
      #consumerKey: "*"
      #consumerSecret: "*"
      #accessToken: "*"
      #accessTokenSecret: "*"
      #debugMode: true
      #userAgent: "atymic/twitter v3.x-dev php v8.0.0"
      -authenticateUrl: "https://api.twitter.com/oauth/authenticate"
      -accessTokenUrl: "https://api.twitter.com/oauth/access_token"
      -requestTokenUrl: "https://api.twitter.com/oauth/request_token"
    }
    -clientFactory: Atymic\Twitter\Http\Factory\ClientCreator {#1443 ▶}
    -syncClient: Atymic\Twitter\Http\Client\SyncClient {#1594 ▶}
    -asyncClient: Atymic\Twitter\Http\Client\AsyncClient {#1591 ▶}
    -logger: null
  }
}

Example - I tried to get User Information.

$twitterV1 =  $this->getTwitterInstanceV1($token, $tokenSecret, true);
$twitterV2 =   $this->getTwitterInstanceV2($token, $tokenSecret, true);

$this->twitterV1->getUsers(['user_id' => '1234']);

Exception:

Too few arguments to function Atymic\Twitter\Service\Accessor::getUsers(), 1 passed in exactly 2 expected

In fact, the getUsers of V2 gets called.

@reliq reliq restored the fix-unable-to-switch-api-version branch April 27, 2021 20:32
@reliq reliq deleted the fix-unable-to-switch-api-version branch April 27, 2021 20:38
@reliq
Copy link
Collaborator Author

reliq commented Apr 27, 2021

Good catch @Cosnavel! I totally forgot about this fact. I have made the fix here #357

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't switch instance from v1 to v2
2 participants