|
77 | 77 | import javax.net.ssl.TrustManagerFactory; |
78 | 78 | import javax.net.ssl.X509TrustManager; |
79 | 79 |
|
| 80 | +import org.joda.time.DateTime; |
| 81 | + |
80 | 82 | import okio.BufferedSink; |
81 | 83 | import okio.Okio; |
82 | 84 |
|
@@ -178,14 +180,95 @@ public ApiClient() { |
178 | 180 | authentications = Collections.unmodifiableMap(authentications); |
179 | 181 | } |
180 | 182 |
|
181 | | - /** |
182 | | - * Get base path |
183 | | - * |
184 | | - * @return Baes path |
185 | | - */ |
186 | | - public String getBasePath() { |
187 | | - return basePath; |
188 | | - } |
| 183 | + private String appSid; |
| 184 | + private String appKey; |
| 185 | + private String appVersion; |
| 186 | + private String appGrantType; |
| 187 | + private DateTime getAccessTokenTime; |
| 188 | + |
| 189 | + /** |
| 190 | + * Get access token |
| 191 | + * |
| 192 | + * @param grantType |
| 193 | + * @param clientId |
| 194 | + * @param clientSecret |
| 195 | + * @param version |
| 196 | + * @return |
| 197 | + * @throws ApiException |
| 198 | + */ |
| 199 | + public String getAccessToken(String grantType, String clientId, |
| 200 | + String clientSecret, String version) throws ApiException { |
| 201 | + Object localVarPostBody = null; |
| 202 | + |
| 203 | + // create path and map variables] |
| 204 | + appGrantType = grantType; |
| 205 | + appSid = clientId; |
| 206 | + appKey = clientSecret; |
| 207 | + appVersion = version; |
| 208 | + |
| 209 | + String localVarPath = "/connect/token"; |
| 210 | + if (version == "v1.1") { |
| 211 | + localVarPath = "/oauth2/token"; |
| 212 | + } |
| 213 | + List<Pair> localVarQueryParams = new ArrayList<Pair>(); |
| 214 | + |
| 215 | + Map<String, String> localVarHeaderParams = new HashMap<String, String>(); |
| 216 | + |
| 217 | + Map<String, Object> localVarFormParams = new HashMap<String, Object>(); |
| 218 | + if (grantType != null) |
| 219 | + localVarFormParams.put("grant_type", grantType); |
| 220 | + if (clientId != null) |
| 221 | + localVarFormParams.put("client_id", clientId); |
| 222 | + if (clientSecret != null) |
| 223 | + localVarFormParams.put("client_secret", clientSecret); |
| 224 | + |
| 225 | + final String[] localVarAccepts = { "application/json" }; |
| 226 | + final String localVarAccept = selectHeaderAccept(localVarAccepts); |
| 227 | + if (localVarAccept != null) |
| 228 | + localVarHeaderParams.put("Accept", localVarAccept); |
| 229 | + |
| 230 | + final String[] localVarContentTypes = { "application/x-www-form-urlencoded" }; |
| 231 | + final String localVarContentType = selectHeaderContentType(localVarContentTypes); |
| 232 | + localVarHeaderParams.put("Content-Type", localVarContentType); |
| 233 | + |
| 234 | + String[] localVarAuthNames = new String[] {}; |
| 235 | + |
| 236 | + Request request = buildRequest(localVarPath, "POST", localVarQueryParams, localVarPostBody, |
| 237 | + localVarHeaderParams, localVarFormParams, localVarAuthNames, null); |
| 238 | + |
| 239 | + com.squareup.okhttp.Call call = httpClient.newCall(request); |
| 240 | + |
| 241 | + Type localVarReturnType = new TypeToken<AccessTokenResponse>() { |
| 242 | + }.getType(); |
| 243 | + ApiResponse<AccessTokenResponse> resp = execute(call, |
| 244 | + localVarReturnType); |
| 245 | + String accessToken = resp.getData().getAccessToken(); |
| 246 | + getAccessTokenTime = DateTime.now(); |
| 247 | + return accessToken; |
| 248 | + } |
| 249 | + |
| 250 | + public void checkAccessToken() throws ApiException { |
| 251 | + if (getAccessTokenTime == null) { |
| 252 | + return; |
| 253 | + } |
| 254 | + if (DateTime.now().compareTo(getAccessTokenTime.plusSeconds(86300))>0 ) { |
| 255 | + setBasePath("https://api.aspose.cloud"); |
| 256 | + String accessToken = getAccessToken(appGrantType, appSid, appKey, |
| 257 | + appVersion); |
| 258 | + getAccessTokenTime = DateTime.now(); |
| 259 | + setBasePath("https://api.aspose.cloud/" + appVersion); |
| 260 | + addDefaultHeader("Authorization", "Bearer " + accessToken); |
| 261 | + } |
| 262 | + } |
| 263 | + |
| 264 | + /** |
| 265 | + * Get base path |
| 266 | + * |
| 267 | + * @return Baes path |
| 268 | + */ |
| 269 | + public String getBasePath() { |
| 270 | + return basePath; |
| 271 | + } |
189 | 272 |
|
190 | 273 | /** |
191 | 274 | * Set base path |
@@ -457,56 +540,7 @@ public Map<String, Authentication> getAuthentications() { |
457 | 540 | public Authentication getAuthentication(String authName) { |
458 | 541 | return authentications.get(authName); |
459 | 542 | } |
460 | | - /** |
461 | | - * Get access token |
462 | | - * @param grantType |
463 | | - * @param clientId |
464 | | - * @param clientSecret |
465 | | - * @param version |
466 | | - * @return |
467 | | - * @throws ApiException |
468 | | - */ |
469 | | - public String getAccessToken(String grantType, String clientId, String clientSecret,String version) throws ApiException { |
470 | | - Object localVarPostBody = null; |
471 | | - |
472 | | - // create path and map variables |
473 | 543 |
|
474 | | - String localVarPath = "/connect/token"; |
475 | | - if( version == "v1.1") |
476 | | - { |
477 | | - localVarPath ="/oauth2/token"; |
478 | | - } |
479 | | - List<Pair> localVarQueryParams = new ArrayList<Pair>(); |
480 | | - |
481 | | - Map<String, String> localVarHeaderParams = new HashMap<String, String>(); |
482 | | - |
483 | | - Map<String, Object> localVarFormParams = new HashMap<String, Object>(); |
484 | | - if (grantType != null) |
485 | | - localVarFormParams.put("grant_type", grantType); |
486 | | - if (clientId != null) |
487 | | - localVarFormParams.put("client_id", clientId); |
488 | | - if (clientSecret != null) |
489 | | - localVarFormParams.put("client_secret", clientSecret); |
490 | | - |
491 | | - final String[] localVarAccepts = { |
492 | | - "application/json" |
493 | | - }; |
494 | | - final String localVarAccept = selectHeaderAccept(localVarAccepts); |
495 | | - if (localVarAccept != null) localVarHeaderParams.put("Accept", localVarAccept); |
496 | | - |
497 | | - final String[] localVarContentTypes = { |
498 | | - "application/x-www-form-urlencoded" |
499 | | - }; |
500 | | - final String localVarContentType = selectHeaderContentType(localVarContentTypes); |
501 | | - localVarHeaderParams.put("Content-Type", localVarContentType); |
502 | | - |
503 | | - String[] localVarAuthNames = new String[] { }; |
504 | | - |
505 | | - com.squareup.okhttp.Call call= buildCall(localVarPath, "POST", localVarQueryParams, localVarPostBody, localVarHeaderParams, localVarFormParams, localVarAuthNames, null); |
506 | | - Type localVarReturnType = new TypeToken<AccessTokenResponse>(){}.getType(); |
507 | | - ApiResponse<AccessTokenResponse> resp = execute(call, localVarReturnType); |
508 | | - return resp.getData().getAccessToken(); |
509 | | - } |
510 | 544 | /** |
511 | 545 | * Helper method to set username for the first HTTP basic authentication. |
512 | 546 | * |
|
0 commit comments