Skip to content

Commit 7a3ba43

Browse files
authored
Create a common package (#680)
* Move common files This creates a new package containing classes shared between v1 and v2. * Fix tests * Fix unstable * Fix operation servers * Fix examples * Update docs * Rebase * Rebase * Rebase
1 parent 86a547a commit 7a3ba43

File tree

1,424 files changed

+2930
-3509
lines changed

Some content is hidden

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

1,424 files changed

+2930
-3509
lines changed

.generator/src/generator/cli.py

Lines changed: 54 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -11,23 +11,20 @@
1111

1212

1313
@click.command()
14-
@click.option(
15-
"-i",
16-
"--input",
14+
@click.argument(
15+
"specs",
16+
nargs=-1,
1717
type=click.Path(exists=True, file_okay=True, dir_okay=False, path_type=pathlib.Path),
1818
)
1919
@click.option(
2020
"-o",
2121
"--output",
2222
type=click.Path(path_type=pathlib.Path),
2323
)
24-
def cli(input, output):
24+
def cli(specs, output):
2525
"""
2626
Generate a Typescript code snippet from OpenAPI specification.
2727
"""
28-
spec = openapi.load(input)
29-
30-
version = input.parent.name
3128

3229
env = Environment(loader=FileSystemLoader(str(pathlib.Path(__file__).parent / "templates")))
3330

@@ -51,8 +48,6 @@ def cli(input, output):
5148
env.globals["package_name"] = package_name
5249
env.globals["npm_name"] = npm_name
5350
env.globals["enumerate"] = enumerate
54-
env.globals["version"] = version
55-
env.globals["openapi"] = spec
5651
env.globals["get_name"] = openapi.get_name
5752
env.globals["type_to_typescript"] = openapi.type_to_typescript
5853
env.globals["get_type_for_attribute"] = openapi.get_type_for_attribute
@@ -69,49 +64,65 @@ def cli(input, output):
6964

7065
api_j2 = env.get_template("api/api.j2")
7166
model_j2 = env.get_template("model/model.j2")
72-
configuration_j2 = env.get_template("configuration.j2")
73-
servers_j2 = env.get_template("servers.j2")
7467

7568
extra_files = {
76-
"util.ts": env.get_template("util.j2"),
77-
"apis/baseapi.ts": env.get_template("api/baseapi.j2"),
78-
"apis/exception.ts": env.get_template("api/exception.j2"),
79-
"auth/auth.ts": env.get_template("auth/auth.j2"),
8069
"models/ObjectSerializer.ts": env.get_template("model/ObjectSerializer.j2"),
81-
"http/http.ts": env.get_template("http/http.j2"),
82-
"http/isomorphic-fetch.ts": env.get_template("http/isomorphic-fetch.j2"),
8370
"index.ts": env.get_template("index.j2"),
8471
}
8572

86-
apis = openapi.apis(spec)
87-
models = openapi.models(spec)
73+
all_specs = {}
74+
all_apis = {}
75+
for spec_path in specs:
76+
spec = openapi.load(spec_path)
8877

89-
package_path = output / f"{package_name}-{version}"
78+
version = spec_path.parent.name
9079

91-
for name, model in models.items():
92-
filename = name + ".ts"
93-
model_path = package_path / "models" / filename
94-
model_path.parent.mkdir(parents=True, exist_ok=True)
95-
with model_path.open("w+") as fp:
96-
fp.write(model_j2.render(name=name, model=model))
80+
env.globals["version"] = version
81+
env.globals["openapi"] = spec
9782

98-
for name, operations in apis.items():
99-
filename = name.replace(" ", "") + "Api.ts"
100-
api_path = package_path / "apis" / filename
101-
api_path.parent.mkdir(parents=True, exist_ok=True)
102-
with api_path.open("w+") as fp:
103-
fp.write(api_j2.render(name=name, operations=operations, models=models))
83+
all_specs[version] = spec
10484

105-
for name, template in extra_files.items():
106-
filename = package_path / name
107-
filename.parent.mkdir(parents=True, exist_ok=True)
108-
with filename.open("w+") as fp:
109-
fp.write(template.render(apis=apis, models=models))
85+
apis = openapi.apis(spec)
86+
models = openapi.models(spec)
87+
88+
all_apis[version] = apis
89+
90+
package_path = output / f"{package_name}-{version}"
11091

111-
configuration_path = package_path / "configuration.ts"
112-
with configuration_path.open("w+") as fp:
113-
fp.write(configuration_j2.render())
92+
for name, model in models.items():
93+
filename = name + ".ts"
94+
model_path = package_path / "models" / filename
95+
model_path.parent.mkdir(parents=True, exist_ok=True)
96+
with model_path.open("w+") as fp:
97+
fp.write(model_j2.render(name=name, model=model))
11498

115-
servers_path = package_path / "servers.ts"
116-
with servers_path.open("w+") as fp:
117-
fp.write(servers_j2.render(apis=apis, operations=operations))
99+
for name, operations in apis.items():
100+
filename = name.replace(" ", "") + "Api.ts"
101+
api_path = package_path / "apis" / filename
102+
api_path.parent.mkdir(parents=True, exist_ok=True)
103+
with api_path.open("w+") as fp:
104+
fp.write(api_j2.render(name=name, operations=operations, models=models))
105+
106+
for name, template in extra_files.items():
107+
filename = package_path / name
108+
filename.parent.mkdir(parents=True, exist_ok=True)
109+
with filename.open("w+") as fp:
110+
fp.write(template.render(apis=apis, models=models))
111+
112+
common_files = {
113+
"util.ts": env.get_template("util.j2"),
114+
"baseapi.ts": env.get_template("api/baseapi.j2"),
115+
"exception.ts": env.get_template("api/exception.j2"),
116+
"auth.ts": env.get_template("auth/auth.j2"),
117+
"http/http.ts": env.get_template("http/http.j2"),
118+
"http/isomorphic-fetch.ts": env.get_template("http/isomorphic-fetch.j2"),
119+
"servers.ts": env.get_template("servers.j2"),
120+
"configuration.ts": env.get_template("configuration.j2"),
121+
"index.ts": env.get_template("common_index.j2"),
122+
}
123+
124+
for name, template in common_files.items():
125+
filename = output / "datadog-api-client-common" / name
126+
filename.parent.mkdir(parents=True, exist_ok=True)
127+
with filename.open("w+") as fp:
128+
fp.write(template.render(apis=all_apis))

.generator/src/generator/formatter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ def get_response_type(schema, version):
426426

427427
response_schema = list(schema["content"].values())[0]["schema"]
428428
if response_schema.get("format") == "binary":
429-
return f"{version}.HttpFile"
429+
return "client.HttpFile"
430430

431431
if response_schema.get("type") == "array":
432432
nested_schema = response_schema.get("items")

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

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1-
import { BaseAPIRequestFactory, RequiredError } from "./baseapi";
2-
import { Configuration, getServer, applySecurityAuthentication} from "../configuration";
1+
import { BaseAPIRequestFactory, RequiredError } from "../../datadog-api-client-common/baseapi";
2+
import { Configuration, getServer, applySecurityAuthentication} from "../../datadog-api-client-common/configuration";
33
import {
44
RequestContext,
55
HttpMethod,
66
ResponseContext,
77
HttpFile
8-
} from "../http/http";
8+
} from "../../datadog-api-client-common/http/http";
99

1010
import FormData from "form-data";
1111

1212
import { logger } from "../../../logger";
1313
import { ObjectSerializer } from "../models/ObjectSerializer";
14-
import { ApiException } from "./exception";
15-
import { isCodeInRange } from "../util";
14+
import { ApiException } from "../../datadog-api-client-common/exception";
15+
import { isCodeInRange } from "../../datadog-api-client-common/util";
1616

1717
{% for model in get_api_models(operations)|sort %}
1818
import { {{ model }} } from "../models/{{ model }}";
@@ -38,7 +38,7 @@ export class {{ className }}RequestFactory extends BaseAPIRequestFactory {
3838
{%- if "x-unstable" in operation %}
3939

4040
logger.warn("Using unstable operation '{{ operationId }}'");
41-
if (!_config.unstableOperations['{{ operationId }}']) {
41+
if (!_config.unstableOperations['{{ version }}.{{ operationId }}']) {
4242
throw new Error("Unstable operation '{{ operationId }}' is disabled");
4343
}
4444
{%- endif %}
@@ -58,7 +58,7 @@ export class {{ className }}RequestFactory extends BaseAPIRequestFactory {
5858
{%- endfor %};
5959

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

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Configuration } from "../configuration";
1+
import { Configuration } from "./configuration";
22

33
/**
44
*

.generator/src/generator/templates/auth/auth.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { RequestContext } from "../http/http";
1+
import { RequestContext } from "./http/http";
22

33
/**
44
* Interface authentication schemes.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export * from "./http/http";
2+
export * from "./http/isomorphic-fetch";
3+
export * from "./auth";
4+
export { createConfiguration } from "./configuration"
5+
export { setServerVariables } from "./configuration"
6+
export { Configuration } from "./configuration"
7+
export * from "./exception";
8+
export * from "./servers";
9+

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

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { HttpLibrary, HttpConfiguration, RequestContext } from "./http/http";
22
import { IsomorphicFetchHttpLibrary as DefaultHttpLibrary } from "./http/isomorphic-fetch";
33
import { BaseServerConfiguration, server1, servers, operationServers } from "./servers";
4-
import { configureAuthMethods, AuthMethods, AuthMethodsConfiguration } from "./auth/auth";
4+
import { configureAuthMethods, AuthMethods, AuthMethodsConfiguration } from "./auth";
55

66
export interface Configuration {
77
readonly baseServer?: BaseServerConfiguration;
@@ -86,13 +86,15 @@ export function createConfiguration(conf: ConfigurationParameters = {}): Configu
8686
serverIndex: conf.serverIndex || 0,
8787
operationServerIndices: conf.operationServerIndices || {},
8888
unstableOperations: {
89-
{%- for path in openapi.paths.values() %}
90-
{%- for operation in path.values()|sort(attribute="operationId") %}
89+
{%- for version, api in apis.items() %}
90+
{%- for operations in api.values() %}
91+
{%- for _, _, operation in operations|sort(attribute="2.operationId") %}
9192
{%- if "x-unstable" in operation %}
92-
"{{ operation.operationId|untitle_case }}": false,
93+
"{{ version }}.{{ operation.operationId|untitle_case }}": false,
9394
{%- endif %}
9495
{%- endfor %}
9596
{%- endfor %}
97+
{%- endfor %}
9698
},
9799
httpApi: conf.httpApi || new DefaultHttpLibrary(),
98100
authMethods: configureAuthMethods(authMethods),

.generator/src/generator/templates/example.j2

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@
33
*/
44

55
import * as fs from "fs";
6-
import { {{ version }} } from "@datadog/datadog-api-client";
6+
import { client, {{ version }} } from "@datadog/datadog-api-client";
77

8-
const configuration = {{ version }}.createConfiguration();
8+
const configuration = client.createConfiguration();
99
{%- for operation in context._enable_operations %}
1010
configuration.unstableOperations["{{ operation|untitle_case }}"] = true;
1111
{%- endfor %}

.generator/src/generator/templates/index.j2

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,3 @@
1-
export * from "./http/http";
2-
export * from "./http/isomorphic-fetch";
3-
export * from "./auth/auth";
4-
export { createConfiguration } from "./configuration"
5-
export { setServerVariables } from "./configuration"
6-
7-
export { Configuration } from "./configuration"
8-
export * from "./apis/exception";
9-
export * from "./servers";
10-
111
{%- for api, operations in apis.items()|sort(attribute="0", case_sensitive=True) %}
122
{%- set classname = api.replace(" ", "") + "Api" %}
133

.generator/src/generator/templates/model/ObjectSerializer.j2

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import { {{ name }} } from "./{{ name }}";
44
{%- endif %}
55
{%- endfor %}
6-
import { UnparsedObject } from "../util";
6+
import { UnparsedObject } from "../../datadog-api-client-common/util";
77
import { logger } from "../../../logger";
88

99
const primitives = [

0 commit comments

Comments
 (0)