@@ -41,9 +41,9 @@ final class OAuth2ClientLib extends AbstractOAuth implements ApiClient
4141 */
4242 public function getInstance ()
4343 {
44- return (self ::$ _provider instanceof \League \OAuth2 \Client \Provider \GenericProvider)
45- ? self ::$ _provider
46- : $ this ->_getOAuthInstance ();
44+ return (self ::$ _provider instanceof \League \OAuth2 \Client \Provider \GenericProvider)
45+ ? self ::$ _provider
46+ : $ this ->_getOAuthInstance ();
4747 }
4848
4949 /**
@@ -59,46 +59,46 @@ public function getInstance()
5959 */
6060 public function request ($ type , $ url , $ params = array (), string $ tenantId = null )
6161 {
62- ApiDebug::p ('running request from ' . __CLASS__ );
63-
64- switch ($ type ) {
65- case \League \OAuth2 \Client \Provider \AbstractProvider::METHOD_POST :
66- if (self ::$ _epoint == UPWORK_GRAPHQL_EP_NAME ) {
67- $ options = array ('headers ' => array ('content-type ' => 'application/json ' ));
68- is_null ($ tenantId ) || $ options ['headers ' ]['X-Upwork-API-TenantId ' ] = $ tenantId ;
69- $ options ['body ' ] = $ params ;
70- } else {
71- $ options = array ('headers ' => array ('content-type ' => 'application/x-www-form-urlencoded ' ));
72- $ options ['body ' ] = http_build_query ($ params , null , '& ' , \PHP_QUERY_RFC3986 );
73- }
74-
75- $ url = ApiUtils::getFullUrl ($ url , self ::$ _epoint );
76- break ;
77- case \League \OAuth2 \Client \Provider \AbstractProvider::METHOD_GET :
78- $ options = array ();
79- $ url = ApiUtils::getFullUrl ($ url , self ::$ _epoint , (($ type == 'GET ' ? $ params : null )));
80- break ;
81- default :
82- throw new ApiException ('Unsupported HTTP method. ' );
83- }
84-
85- $ options ['headers ' ]['user-agent ' ] = ApiConfig::UPWORK_LIBRARY_USER_AGENT ;
86- $ request = $ this ->getInstance ()->getAuthenticatedRequest ($ type , $ url , self ::$ _accessToken , $ options );
87-
88- ApiDebug::p ('prepared request ' , $ request );
89-
90- try {
91- // do not use getParsedResponse, it returns an array
92- // but we need a raw json that will be decoded and returned as StdClass object
93- $ response = $ this ->getInstance ()->getResponse ($ request );
62+ ApiDebug::p ('running request from ' . __CLASS__ );
63+
64+ switch ($ type ) {
65+ case \League \OAuth2 \Client \Provider \AbstractProvider::METHOD_POST :
66+ if (self ::$ _epoint == UPWORK_GRAPHQL_EP_NAME ) {
67+ $ options = array ('headers ' => array ('content-type ' => 'application/json ' ));
68+ is_null ($ tenantId ) || $ options ['headers ' ]['X-Upwork-API-TenantId ' ] = $ tenantId ;
69+ $ options ['body ' ] = $ params ;
70+ } else {
71+ $ options = array ('headers ' => array ('content-type ' => 'application/x-www-form-urlencoded ' ));
72+ $ options ['body ' ] = http_build_query ($ params , null , '& ' , \PHP_QUERY_RFC3986 );
73+ }
74+
75+ $ url = ApiUtils::getFullUrl ($ url , self ::$ _epoint );
76+ break ;
77+ case \League \OAuth2 \Client \Provider \AbstractProvider::METHOD_GET :
78+ $ options = array ();
79+ $ url = ApiUtils::getFullUrl ($ url , self ::$ _epoint , (($ type == 'GET ' ? $ params : null )));
80+ break ;
81+ default :
82+ throw new ApiException ('Unsupported HTTP method. ' );
83+ }
84+
85+ $ options ['headers ' ]['user-agent ' ] = ApiConfig::UPWORK_LIBRARY_USER_AGENT ;
86+ $ request = $ this ->getInstance ()->getAuthenticatedRequest ($ type , $ url , self ::$ _accessToken , $ options );
87+
88+ ApiDebug::p ('prepared request ' , $ request );
89+
90+ try {
91+ // do not use getParsedResponse, it returns an array
92+ // but we need a raw json that will be decoded and returned as StdClass object
93+ $ response = $ this ->getInstance ()->getResponse ($ request );
9494 } catch (\GuzzleHttp \Exception \ClientException $ e ) {
9595 $ eResponse = $ e ->getResponse ();
9696 $ response = $ eResponse ->getBody ()->getContents ();
97- } catch (\Exception $ e ) {
98- $ response = $ e ->getResponseBody ();
99- }
100-
101- ApiDebug::p ('got response from server ' , $ response );
97+ } catch (\Exception $ e ) {
98+ $ response = $ e ->getResponseBody ();
99+ }
100+
101+ ApiDebug::p ('got response from server ' , $ response );
102102
103103 return (string ) $ response ->getBody ();
104104 }
@@ -107,14 +107,15 @@ public function request($type, $url, $params = array(), string $tenantId = null)
107107 * Get access token
108108 *
109109 * @param string $authzCode Authorization code (a received verifier)
110+ * @param string $grantType Grant Type
110111 * @access protected
111112 * @return array
112113 */
113- protected function _setupTokens ($ authzCode )
114+ protected function _setupTokens ($ authzCode, $ grantType )
114115 {
115116 ApiDebug::p ('requesting access token ' );
116117
117- return $ this ->_requestTokens (' authorization_code ' , array ('code ' => $ authzCode ));
118+ return $ this ->_requestTokens ($ grantType , array ('code ' => $ authzCode ));
118119 }
119120
120121 /**
@@ -126,9 +127,9 @@ protected function _setupTokens($authzCode)
126127 */
127128 protected function _refreshTokens ($ refreshToken )
128129 {
129- ApiDebug::p ('refreshing the existing access token ' );
130+ ApiDebug::p ('refreshing the existing access token ' );
130131
131- return $ this ->_requestTokens ('refresh_token ' , array ('refresh_token ' => $ refreshToken ));
132+ return $ this ->_requestTokens ('refresh_token ' , array ('refresh_token ' => $ refreshToken ));
132133 }
133134
134135 /**
@@ -147,11 +148,11 @@ protected function _getOAuthInstance($authType = null)
147148 'clientSecret ' => self ::$ _clientSecret ,
148149 'redirectUri ' => self ::$ _redirectUri ,
149150 'urlAuthorize ' => ApiUtils::getFullUrl (self ::URL_AUTH , '' ),
150- 'urlAccessToken ' => ApiUtils::getFullUrl (self ::URL_ATOKEN , 'api ' ),
151- 'urlResourceOwnerDetails ' => ''
151+ 'urlAccessToken ' => ApiUtils::getFullUrl (self ::URL_ATOKEN , 'api ' ),
152+ 'urlResourceOwnerDetails ' => ''
152153 );
153154
154- self ::$ _provider = new \League \OAuth2 \Client \Provider \GenericProvider ($ options );
155+ self ::$ _provider = new \League \OAuth2 \Client \Provider \GenericProvider ($ options );
155156
156157 return self ::$ _provider ;
157158 }
@@ -170,21 +171,24 @@ private function _requestTokens(string $type, array $options)
170171
171172 $ accessTokenInfo = array ();
172173
173- $ accessToken = $ this ->getInstance ()->getHttpClient ()->getConfig ()['handler ' ]->push (
174+ $ accessToken = $ this ->getInstance ()->getHttpClient ()->getConfig ()['handler ' ]->push (
174175 Middleware::mapRequest (function (RequestInterface $ request ) {
175176 return $ request ->withHeader ('User-Agent ' , ApiConfig::UPWORK_LIBRARY_USER_AGENT );
176177 }));
177178 $ accessToken = $ this ->getInstance ()->getAccessToken ($ type , $ options );
178179
179180 $ accessTokenInfo ['access_token ' ] = $ accessToken ->getToken ();
180- $ accessTokenInfo ['refresh_token ' ] = $ accessToken ->getRefreshToken ();
181- $ accessTokenInfo ['expires_in ' ] = $ accessToken ->getExpires ();
182-
183- ApiDebug::p ('got access token info ' , $ accessTokenInfo );
181+ $ accessTokenInfo ['expires_in ' ] = $ accessToken ->getExpires ();
184182
185183 self ::$ _accessToken = $ accessTokenInfo ['access_token ' ];
186- self ::$ _refreshToken = $ accessTokenInfo ['refresh_token ' ];
187- self ::$ _expiresIn = $ accessTokenInfo ['expires_in ' ];
184+ self ::$ _expiresIn = $ accessTokenInfo ['expires_in ' ];
185+
186+ if ($ type === 'authorization_code ' ) {
187+ $ accessTokenInfo ['refresh_token ' ] = $ accessToken ->getRefreshToken ();
188+ self ::$ _refreshToken = $ accessTokenInfo ['refresh_token ' ];
189+ }
190+
191+ ApiDebug::p ('got access token info ' , $ accessTokenInfo );
188192
189193 return $ accessTokenInfo ;
190194 }
0 commit comments