Skip to content

Commit 5d9a5aa

Browse files
authored
Use configuration method for server variables (#1284)
To be able to use serveral clients against different site, have configuration-local server instances so that the global defined ones aren't modified. Closes #1281
1 parent 0980089 commit 5d9a5aa

File tree

76 files changed

+1695
-2069
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+1695
-2069
lines changed

.generator/src/generator/templates/api/api.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { BaseAPIRequestFactory, RequiredError } from "../../datadog-api-client-common/baseapi";
2-
import { Configuration, getServer, applySecurityAuthentication} from "../../datadog-api-client-common/configuration";
2+
import { Configuration, applySecurityAuthentication} from "../../datadog-api-client-common/configuration";
33
import {
44
RequestContext,
55
HttpMethod,
@@ -57,7 +57,7 @@ export class {{ className }}RequestFactory extends BaseAPIRequestFactory {
5757
{%- endfor %};
5858

5959
// Make Request Context
60-
const requestContext = getServer(_config, '{{ version }}.{{ className }}.{{ operationId }}').makeRequestContext(localVarPath, HttpMethod.{{ method.upper() }});
60+
const requestContext = _config.getServer('{{ version }}.{{ className }}.{{ operationId }}').makeRequestContext(localVarPath, HttpMethod.{{ method.upper() }});
6161
{%- if operation|accept_headers|length == 0 %}
6262
requestContext.setHeaderParam("Accept", "application/json");
6363
{%- else %}

.generator/src/generator/templates/configuration.j2

Lines changed: 77 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,78 @@ import { HttpLibrary, HttpConfiguration, RequestContext, ZstdCompressorCallback
22
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "./http/isomorphic-fetch";
33
import { BaseServerConfiguration, server1, servers, operationServers } from "./servers";
44
import { configureAuthMethods, AuthMethods, AuthMethodsConfiguration } from "./auth";
5+
import { logger } from "../../logger";
56

6-
export interface Configuration {
7+
export class Configuration {
78
readonly baseServer?: BaseServerConfiguration;
89
readonly serverIndex: number;
9-
readonly operationServerIndices: { [ name: string ]: number };
10+
readonly operationServerIndices: { [name: string]: number };
1011
readonly httpApi: HttpLibrary;
1112
readonly authMethods: AuthMethods;
1213
readonly httpConfig: HttpConfiguration;
1314
readonly debug: boolean | undefined;
1415
unstableOperations: { [name: string]: boolean };
15-
}
16+
servers: BaseServerConfiguration[];
17+
operationServers: { [endpoint: string]: BaseServerConfiguration[] };
18+
19+
public constructor(
20+
baseServer: BaseServerConfiguration | undefined,
21+
serverIndex: number,
22+
operationServerIndices: { [name: string]: number },
23+
httpApi: HttpLibrary,
24+
authMethods: AuthMethods,
25+
httpConfig: HttpConfiguration,
26+
debug: boolean | undefined,
27+
unstableOperations: { [name: string]: boolean },
28+
) {
29+
this.baseServer = baseServer;
30+
this.serverIndex = serverIndex;
31+
this.operationServerIndices = operationServerIndices;
32+
this.httpApi = httpApi;
33+
this.authMethods = authMethods;
34+
this.httpConfig = httpConfig;
35+
this.debug = debug;
36+
this.unstableOperations = unstableOperations;
37+
this.servers = [];
38+
for (const server of servers) {
39+
this.servers.push(server.clone());
40+
}
41+
this.operationServers = {};
42+
for (const endpoint in operationServers) {
43+
this.operationServers[endpoint] = [];
44+
for (const server of operationServers[endpoint]) {
45+
this.operationServers[endpoint].push(server.clone());
46+
}
47+
}
48+
}
1649

50+
setServerVariables(serverVariables: { [key: string]: string }): void {
51+
if (this.baseServer !== undefined) {
52+
this.baseServer.setVariables(serverVariables);
53+
return;
54+
}
55+
56+
const index = this.serverIndex;
57+
this.servers[index].setVariables(serverVariables);
58+
59+
for (const op in this.operationServers) {
60+
this.operationServers[op][0].setVariables(serverVariables);
61+
}
62+
}
63+
64+
getServer(endpoint: string): BaseServerConfiguration {
65+
if (this.baseServer !== undefined) {
66+
return this.baseServer;
67+
}
68+
const index =
69+
endpoint in this.operationServerIndices
70+
? this.operationServerIndices[endpoint]
71+
: this.serverIndex;
72+
return endpoint in operationServers
73+
? this.operationServers[endpoint][index]
74+
: this.servers[index];
75+
}
76+
}
1777

1878
/**
1979
* Interface with which a configuration object can be configured.
@@ -85,11 +145,15 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
85145
{%- endif %}
86146
{%- endfor %}
87147

88-
const configuration: Configuration = {
89-
baseServer: conf.baseServer,
90-
serverIndex: conf.serverIndex || 0,
91-
operationServerIndices: conf.operationServerIndices || {},
92-
unstableOperations: {
148+
const configuration = new Configuration(
149+
conf.baseServer,
150+
conf.serverIndex || 0,
151+
conf.operationServerIndices || {},
152+
conf.httpApi || new DefaultHttpLibrary(),
153+
configureAuthMethods(authMethods),
154+
conf.httpConfig || {},
155+
conf.debug,
156+
{
93157
{%- for version, api in apis.items() %}
94158
{%- for operations in api.values() %}
95159
{%- for _, _, operation in operations|sort(attribute="2.operationId") %}
@@ -100,22 +164,15 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
100164
{%- endfor %}
101165
{%- endfor %}
102166
},
103-
httpApi: conf.httpApi || new DefaultHttpLibrary(),
104-
authMethods: configureAuthMethods(authMethods),
105-
httpConfig: conf.httpConfig || {},
106-
debug: conf.debug
107-
};
167+
);
108168
configuration.httpApi.zstdCompressorCallback = conf.zstdCompressorCallback
109169
configuration.httpApi.debug = configuration.debug;
110170
return configuration;
111171
}
112172

113173
export function getServer(conf: Configuration, endpoint: string): BaseServerConfiguration {
114-
if (conf.baseServer !== undefined) {
115-
return conf.baseServer;
116-
}
117-
const index = (endpoint in conf.operationServerIndices) ? conf.operationServerIndices[endpoint] : conf.serverIndex;
118-
return (endpoint in operationServers) ? operationServers[endpoint][index] : servers[index];
174+
logger.warn("getServer is deprecated, please use Configuration.getServer instead.");
175+
return conf.getServer(endpoint);
119176
}
120177

121178
/**
@@ -124,17 +181,8 @@ export function getServer(conf: Configuration, endpoint: string): BaseServerConf
124181
* @param serverVariables key/value object representing the server variables (site, name, protocol, ...)
125182
*/
126183
export function setServerVariables(conf: Configuration, serverVariables: { [key: string]: string }): void {
127-
if (conf.baseServer !== undefined) {
128-
conf.baseServer.setVariables(serverVariables);
129-
return ;
130-
}
131-
132-
const index = conf.serverIndex;
133-
servers[index].setVariables(serverVariables);
134-
135-
for (const op in operationServers) {
136-
operationServers[op][0].setVariables(serverVariables);
137-
}
184+
logger.warn("setServerVariables is deprecated, please use Configuration.setServerVariables instead.");
185+
return conf.setServerVariables(serverVariables);
138186
}
139187

140188
/**

.generator/src/generator/templates/servers.j2

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ export class BaseServerConfiguration {
2121
return this.variableConfiguration
2222
}
2323

24+
public clone(): BaseServerConfiguration {
25+
return new BaseServerConfiguration(this.url, {...this.variableConfiguration});
26+
}
27+
2428
private getUrl() {
2529
let replacedUrl = this.url;
2630
for (const key in this.variableConfiguration) {

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ import { client } from '@datadog/datadog-api-client';
7474

7575
const configuration = client.createConfiguration();
7676

77-
client.setServerVariables(configuration, {
77+
configuration.setServerVariables({
7878
site: "datadoghq.eu"
7979
});
8080
```

packages/datadog-api-client-common/configuration.ts

Lines changed: 81 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,9 @@ import {
1616
AuthMethods,
1717
AuthMethodsConfiguration,
1818
} from "./auth";
19+
import { logger } from "../../logger";
1920

20-
export interface Configuration {
21+
export class Configuration {
2122
readonly baseServer?: BaseServerConfiguration;
2223
readonly serverIndex: number;
2324
readonly operationServerIndices: { [name: string]: number };
@@ -26,6 +27,66 @@ export interface Configuration {
2627
readonly httpConfig: HttpConfiguration;
2728
readonly debug: boolean | undefined;
2829
unstableOperations: { [name: string]: boolean };
30+
servers: BaseServerConfiguration[];
31+
operationServers: { [endpoint: string]: BaseServerConfiguration[] };
32+
33+
public constructor(
34+
baseServer: BaseServerConfiguration | undefined,
35+
serverIndex: number,
36+
operationServerIndices: { [name: string]: number },
37+
httpApi: HttpLibrary,
38+
authMethods: AuthMethods,
39+
httpConfig: HttpConfiguration,
40+
debug: boolean | undefined,
41+
unstableOperations: { [name: string]: boolean }
42+
) {
43+
this.baseServer = baseServer;
44+
this.serverIndex = serverIndex;
45+
this.operationServerIndices = operationServerIndices;
46+
this.httpApi = httpApi;
47+
this.authMethods = authMethods;
48+
this.httpConfig = httpConfig;
49+
this.debug = debug;
50+
this.unstableOperations = unstableOperations;
51+
this.servers = [];
52+
for (const server of servers) {
53+
this.servers.push(server.clone());
54+
}
55+
this.operationServers = {};
56+
for (const endpoint in operationServers) {
57+
this.operationServers[endpoint] = [];
58+
for (const server of operationServers[endpoint]) {
59+
this.operationServers[endpoint].push(server.clone());
60+
}
61+
}
62+
}
63+
64+
setServerVariables(serverVariables: { [key: string]: string }): void {
65+
if (this.baseServer !== undefined) {
66+
this.baseServer.setVariables(serverVariables);
67+
return;
68+
}
69+
70+
const index = this.serverIndex;
71+
this.servers[index].setVariables(serverVariables);
72+
73+
for (const op in this.operationServers) {
74+
this.operationServers[op][0].setVariables(serverVariables);
75+
}
76+
}
77+
78+
getServer(endpoint: string): BaseServerConfiguration {
79+
if (this.baseServer !== undefined) {
80+
return this.baseServer;
81+
}
82+
const index =
83+
endpoint in this.operationServerIndices
84+
? this.operationServerIndices[endpoint]
85+
: this.serverIndex;
86+
return endpoint in operationServers
87+
? this.operationServers[endpoint][index]
88+
: this.servers[index];
89+
}
2990
}
3091

3192
/**
@@ -109,11 +170,15 @@ export function createConfiguration(
109170
authMethods["appKeyAuth"] = process.env.DD_APP_KEY;
110171
}
111172

112-
const configuration: Configuration = {
113-
baseServer: conf.baseServer,
114-
serverIndex: conf.serverIndex || 0,
115-
operationServerIndices: conf.operationServerIndices || {},
116-
unstableOperations: {
173+
const configuration = new Configuration(
174+
conf.baseServer,
175+
conf.serverIndex || 0,
176+
conf.operationServerIndices || {},
177+
conf.httpApi || new DefaultHttpLibrary(),
178+
configureAuthMethods(authMethods),
179+
conf.httpConfig || {},
180+
conf.debug,
181+
{
117182
"v2.createCIAppPipelineEvent": false,
118183
"v2.cancelDowntime": false,
119184
"v2.createDowntime": false,
@@ -156,12 +221,8 @@ export function createConfiguration(
156221
"v2.getIncidentTeam": false,
157222
"v2.listIncidentTeams": false,
158223
"v2.updateIncidentTeam": false,
159-
},
160-
httpApi: conf.httpApi || new DefaultHttpLibrary(),
161-
authMethods: configureAuthMethods(authMethods),
162-
httpConfig: conf.httpConfig || {},
163-
debug: conf.debug,
164-
};
224+
}
225+
);
165226
configuration.httpApi.zstdCompressorCallback = conf.zstdCompressorCallback;
166227
configuration.httpApi.debug = configuration.debug;
167228
return configuration;
@@ -171,16 +232,10 @@ export function getServer(
171232
conf: Configuration,
172233
endpoint: string
173234
): BaseServerConfiguration {
174-
if (conf.baseServer !== undefined) {
175-
return conf.baseServer;
176-
}
177-
const index =
178-
endpoint in conf.operationServerIndices
179-
? conf.operationServerIndices[endpoint]
180-
: conf.serverIndex;
181-
return endpoint in operationServers
182-
? operationServers[endpoint][index]
183-
: servers[index];
235+
logger.warn(
236+
"getServer is deprecated, please use Configuration.getServer instead."
237+
);
238+
return conf.getServer(endpoint);
184239
}
185240

186241
/**
@@ -192,17 +247,10 @@ export function setServerVariables(
192247
conf: Configuration,
193248
serverVariables: { [key: string]: string }
194249
): void {
195-
if (conf.baseServer !== undefined) {
196-
conf.baseServer.setVariables(serverVariables);
197-
return;
198-
}
199-
200-
const index = conf.serverIndex;
201-
servers[index].setVariables(serverVariables);
202-
203-
for (const op in operationServers) {
204-
operationServers[op][0].setVariables(serverVariables);
205-
}
250+
logger.warn(
251+
"setServerVariables is deprecated, please use Configuration.setServerVariables instead."
252+
);
253+
return conf.setServerVariables(serverVariables);
206254
}
207255

208256
/**

packages/datadog-api-client-common/servers.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ export class BaseServerConfiguration {
2424
return this.variableConfiguration;
2525
}
2626

27+
public clone(): BaseServerConfiguration {
28+
return new BaseServerConfiguration(this.url, {
29+
...this.variableConfiguration,
30+
});
31+
}
32+
2733
private getUrl() {
2834
let replacedUrl = this.url;
2935
for (const key in this.variableConfiguration) {

0 commit comments

Comments
 (0)