atymic/twitter

Can't switch instance from v1 to v2

adiv-phpian opened this issue · 8 comments

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.

reliq commented

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

@reliq I don't think this is solved.

reliq commented

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

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
  }

Both of them are API Version 2.

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.

reliq commented

@Cosnavel this happens because the facade provides a singleton. I have updated these functions to behave as the documentation suggests in commit 1544d8b

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

reliq commented

No problem @Cosnavel. You are welcome.


Fixed as per documentation with release 3.1.7