profile
viewpoint
If you are wondering where the data of this site comes from, please visit https://api.github.com/users/pajoda/events. GitMemory does not store any data, but only uses NGINX to cache data for a period of time. The idea behind GitMemory is simply to give users a better reading experience.

nschoellhorn/framework 0

A lightweight PHP framework without tight coupling but strong typing.

issue openedatymic/twitter

Access response headers to get x-rate-limit header values

I was using the 2.x version of this library and I have a situation where I am making lots of requests to getUserTimeline() and I want to know when I am close to hitting the rate limit so I can sleep and wait for the rate limit to reset. I want to sleep before I hit the rate limit so that I can leave some api calls available for other services.

In the 2.x version I was able to do something like this:

foreach ($screenNames as $screenName) {
    $timeline = $twitter->getUserTimeline(['screen_name' => $screenName]);
    
    // do something with the timeline
    
    // check if close to rate limit
    $headers = $twitter->response['headers'];
    $rateLimitRemaining = Arr::get($headers, 'x-rate-limit-remaining', 0);
    if ($rateLimitRemaining < 50) {
        $timestamp = Arr::get($headers, 'x-rate-limit-reset', 0);
        $secondsUntilReset = max($timestamp - time(), 0);
        sleep($secondsUntilReset);
    }
}

This works because I am able to access the previous response and headers.

I am aware of getAppRateLimit() but it seems wasteful to make an extra API call given that each response includes the x-rate-limit-* headers. I could catch the RateLimitedException but at that point it is too late since I want to sleep before I hit the rate limit.

Would you be open to storing the last response like it did in the 2.x version? If so which object would be the best place to store it? I wonder if this could cause a race condition, if you make a request and then before you can access the previous response another request is made so you are not getting the response that you intended.

Alternatively, after each request it may be possible to cache the rate limit values per resource in a format similar to the response from getAppRateLimit()

[
    "/statuses/user_timeline" => [
           "limit" => 900,
           "remaining" => 899,
           "reset" => 1621035809,
     ]
]

then you could get the rate limit values without having to make a separate API call using getAppRateLimit()

If that seems like overkill then I may resort to calling getAppRateLimit() after every X requests to see if I am close to hitting the rate limit.

created time in 6 hours

created tagatymic/twitter

tag3.1.8

Twitter API for Laravel 5.5+, 6.x, 7.x & 8.x

created time in 17 days

delete branch atymic/twitter

delete branch : fix-unable-to-switch-api-version-2

delete time in 17 days

push eventatymic/twitter

Reliq

commit sha 738a2b58775b9f59765c332fa20b9f40ad530167

fix: hotswapping methods forApiV1() and forApiV2() (#357)

view details

push time in 17 days

PR merged atymic/twitter

fix: hotswapping methods forApiV1() and forApiV2()

Follow-up to finally fix #355

+101 -13

1 comment

4 changed files

reliq

pr closed time in 17 days

issue closedatymic/twitter

Can't switch instance from v1 to v2

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

I tried to get version 2 instance from version 1, but it's returning error.

closed time in 17 days

adiv-phpian

pull request commentatymic/twitter

fix: unable to switch API version on service #355

Good catch @Cosnavel! I totally forgot about this fact. I have made the fix here https://github.com/atymic/twitter/pull/357

reliq

comment created time in 17 days

pull request commentatymic/twitter

fix: hotswapping methods forApiV1() and forApiV2()

Codecov Report

Merging #357 (5b0aacc) into main (1544d8b) will increase coverage by 2.52%. The diff coverage is 100.00%.

Impacted file tree graph

@@             Coverage Diff              @@
##               main     #357      +/-   ##
============================================
+ Coverage     34.39%   36.91%   +2.52%     
  Complexity      361      361              
============================================
  Files            32       32              
  Lines           814      818       +4     
============================================
+ Hits            280      302      +22     
+ Misses          534      516      -18     
Impacted Files Coverage Δ Complexity Δ
src/Concern/HotSwapper.php 100.00% <100.00%> (+52.63%) 4.00 <3.00> (ø)
src/ApiV1/Service/Twitter.php 50.00% <0.00%> (+40.00%) 8.00% <0.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 1544d8b...5b0aacc. Read the comment docs.

reliq

comment created time in 17 days

PR opened atymic/twitter

fix: hotswapping methods forApiV1() and forApiV2()

Follow-up to finally fix #355

+101 -13

0 comment

4 changed files

pr created time in 17 days

delete branch atymic/twitter

delete branch : fix-unable-to-switch-api-version

delete time in 17 days

create barnchatymic/twitter

branch : fix-unable-to-switch-api-version-2

created branch time in 17 days

create barnchatymic/twitter

branch : fix-unable-to-switch-api-version

created branch time in 17 days

IssuesEvent

pull request commentatymic/twitter

fix: unable to switch API version on service #355

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

comment created time in 17 days

issue closedatymic/twitter

Can't switch instance from v1 to v2

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

I tried to get version 2 instance from version 1, but it's returning error.

closed time in 17 days

adiv-phpian

issue commentatymic/twitter

Can't switch instance from v1 to v2

No problem @Cosnavel. You are welcome.


Fixed as per documentation with release 3.1.7

adiv-phpian

comment created time in 17 days

issue commentatymic/twitter

Can't switch instance from v1 to v2

I should have looked at your implementation of the HotSwapper. Thank you for your time. Great work. I appreciate it 👍🏼

adiv-phpian

comment created time in 17 days

created tagatymic/twitter

tag3.1.7

Twitter API for Laravel 5.5+, 6.x, 7.x & 8.x

created time in 17 days

issue commentatymic/twitter

Can't switch instance from v1 to v2

@Cosnavel this happens because the facade provides a singleton. I have updated these functions to behave as the documentation suggests in commit https://github.com/atymic/twitter/commit/1544d8b58d6f6f7e093e4d55a1d5e4a7d03c8f51

adiv-phpian

comment created time in 17 days

push eventatymic/twitter

reliq

commit sha 1544d8b58d6f6f7e093e4d55a1d5e4a7d03c8f51

fix: return separate instance from forApiv1() and forApiV2() methods

view details

push time in 17 days

issue commentatymic/twitter

Can't switch instance from v1 to v2

Before you have fixed the Method issue I have tried to fix this by updating the version directly in the config and then tried to get an instance of the other API version. This works neither well.


config(['twitter.api_version' => '1.1']);
$twitterV1 =  Twitter::usingCredentials($token, $tokenSecret, env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'));
config(['twitter.api_version' => '2']);
$twitterV2 =  Twitter::usingCredentials($token, $tokenSecret, env('TWITTER_CONSUMER_KEY'), env('TWITTER_CONSUMER_SECRET'));

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

Response:

Atymic\Twitter\ApiV1\Service\Twitter {#1450 ▼
  #config: Atymic\Twitter\Configuration {#1536 ▼
    #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 {#1538 ▶}
  #debug: true
}
Atymic\Twitter\ApiV1\Service\Twitter {#1450 ▼
  #config: Atymic\Twitter\Configuration {#1536 ▼
    #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 {#1538 ▶}
  #debug: true
}


As far as I have gotten into this, the Configuration does not change. Probably this is a separate issue.

adiv-phpian

comment created time in 17 days

issue commentatymic/twitter

Can't switch instance from v1 to v2

The error with the undefined method is gone. But you can't instantiate the two clients. Look at this snippet:

 $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 {#1450 ▼ -querier: Atymic\Twitter\Service\Querier {#1537 ▼ -config: Atymic\Twitter\Configuration {#1536 ▼ #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 {#1432 ▶} -syncClient: Atymic\Twitter\Http\Client\SyncClient {#1576 ▶} -asyncClient: Atymic\Twitter\Http\Client\AsyncClient {#1538 ▶} -logger: null } } Atymic\Twitter\Service\Accessor {#1450 ▼ -querier: Atymic\Twitter\Service\Querier {#1537 ▼ -config: Atymic\Twitter\Configuration {#1536 ▼ #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 {#1432 ▶} -syncClient: Atymic\Twitter\Http\Client\SyncClient {#1576 ▶} -asyncClient: Atymic\Twitter\Http\Client\AsyncClient {#1538 ▶} -logger: null }

adiv-phpian

comment created time in 17 days

IssuesEvent

issue commentatymic/twitter

Can't switch instance from v1 to v2

Hi @Cosnavel. Please tell me what is happening for you

adiv-phpian

comment created time in 17 days

issue commentatymic/twitter

Can't switch instance from v1 to v2

@reliq I don't think this is solved.

adiv-phpian

comment created time in 17 days

issue commentatymic/twitter

Can't switch instance from v1 to v2

Thanks for reporting this @adiv-phpian. This issue should be resolved.

adiv-phpian

comment created time in 17 days

pull request commentatymic/twitter

fix: unable to switch API version on service #355

Great work on this 🚀

reliq

comment created time in 17 days

created tagatymic/twitter

tag3.1.6

Twitter API for Laravel 5.5+, 6.x, 7.x & 8.x

created time in 17 days

delete branch atymic/twitter

delete branch : fix-unable-to-switch-api-version

delete time in 17 days

push eventatymic/twitter

Reliq

commit sha fabbddf42d97591f39a95707437204d2b0b31e18

fix: unable to switch API version on service (#356)

view details

push time in 17 days