Skip to content

Commit c69c919

Browse files
committed
templates
1 parent 8b066c0 commit c69c919

File tree

4 files changed

+49
-3
lines changed

4 files changed

+49
-3
lines changed

api/calls-api.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,8 @@ export const CallsApiAxiosParamCreator = function (configuration?: Configuration
6868

6969
// authentication OAuth2 required
7070
// oauth required
71-
const setOAuth = await setOAuthToObject(localVarHeaderParameter, "OAuth2", [], configuration);
71+
await setOAuthToObject(localVarHeaderParameter, "OAuth2", [], configuration)
72+
7273

7374

7475
localVarHeaderParameter['Content-Type'] = 'application/json';

configuration.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
* https://openapi-generator.tech
1111
* Do not edit the class manually.
1212
*/
13+
1314
import globalAxios from 'axios';
1415

1516
interface AWSv4Configuration {

custom_templates/common.mustache

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ export const setOAuthToObject = async function (object: any, name: string, scope
5454
const localVarAccessTokenValue = typeof configuration.accessToken === 'function'
5555
? await configuration.accessToken(name, scopes)
5656
: await configuration.accessToken;
57-
object["Authorization"] = "Bearer " + localVarAccessTokenValue;
57+
if (localVarAccessTokenValue) {
58+
object["Authorization"] = "Bearer " + localVarAccessTokenValue;
59+
}
5860
}
5961
}
6062

custom_templates/configuration.mustache

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
/* tslint:disable */
22
{{>licenseInfo}}
33

4+
import globalAxios from 'axios';
5+
46
interface AWSv4Configuration {
57
options?: {
68
region?: string
@@ -17,6 +19,8 @@ export interface ConfigurationParameters {
1719
apiKey?: string | Promise<string> | ((name: string) => string) | ((name: string) => Promise<string>);
1820
username?: string;
1921
password?: string;
22+
clientId?: string;
23+
clientSecret?: string;
2024
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
2125
awsv4?: AWSv4Configuration;
2226
basePath?: string;
@@ -39,12 +43,28 @@ export class Configuration {
3943
* parameter for basic security
4044
*/
4145
password?: string;
46+
/**
47+
* parameter for client ID
48+
*/
49+
clientId?: string;
50+
/**
51+
* parameter for client secret
52+
*/
53+
clientSecret?: string;
4254
/**
4355
* parameter for oauth2 security
4456
* @param name security name
4557
* @param scopes oauth2 scope
4658
*/
4759
accessToken?: string | Promise<string> | ((name?: string, scopes?: string[]) => string) | ((name?: string, scopes?: string[]) => Promise<string>);
60+
/**
61+
* temporary access token storage
62+
*/
63+
tempAccessToken?: string;
64+
/**
65+
* when the temporary access token expires
66+
*/
67+
tempAccessTokenExpiration?: number;
4868
/**
4969
* parameter for aws4 signature security
5070
* @param {Object} AWS4Signature - AWS4 Signature security
@@ -81,7 +101,29 @@ export class Configuration {
81101
this.apiKey = param.apiKey;
82102
this.username = param.username;
83103
this.password = param.password;
84-
this.accessToken = param.accessToken;
104+
this.clientId = param.clientId;
105+
this.clientSecret = param.clientSecret;
106+
this.tempAccessToken;
107+
this.tempAccessTokenExpiration;
108+
this.accessToken = param.accessToken ?? (async (name, scopes) => {
109+
const now = Math.floor(Date.now() / 1000);
110+
if (this.tempAccessToken && (!this.tempAccessTokenExpiration || this.tempAccessTokenExpiration > now + 60)) {
111+
return this.tempAccessToken;
112+
} else if (this.clientId && this.clientSecret) {
113+
const tokenRequestArgs = {
114+
method: 'POST',
115+
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
116+
auth: { username: this.clientId || '', password: this.clientSecret || '' },
117+
data: 'grant_type=client_credentials',
118+
url: 'https://api.bandwidth.com/api/v1/oauth2/token'
119+
};
120+
const response = await globalAxios.request(tokenRequestArgs);
121+
this.tempAccessToken = response.data.access_token;
122+
this.tempAccessTokenExpiration = now + response.data.expires_in;
123+
return this.tempAccessToken!;
124+
}
125+
return undefined;
126+
});
85127
this.awsv4 = param.awsv4;
86128
this.basePath = param.basePath;
87129
this.serverIndex = param.serverIndex;

0 commit comments

Comments
 (0)