1
- import pathlib
2
-
3
1
import click
4
- from jinja2 import Environment , FileSystemLoader
5
-
6
- from . import openapi
7
- from . import formatter
8
-
9
- package_name = "datadog-api-client"
10
- npm_name = "@datadog/datadog-api-client"
11
2
12
3
13
4
@click .command ()
14
- @click .argument (
15
- "specs" ,
16
- nargs = - 1 ,
17
- type = click .Path (exists = True , file_okay = True , dir_okay = False , path_type = pathlib .Path ),
18
- )
19
- @click .option (
20
- "-o" ,
21
- "--output" ,
22
- type = click .Path (path_type = pathlib .Path ),
23
- )
24
- def cli (specs , output ):
5
+ def cli ():
25
6
"""
26
7
Generate a Typescript code snippet from OpenAPI specification.
27
8
"""
28
-
29
- env = Environment (loader = FileSystemLoader (str (pathlib .Path (__file__ ).parent / "templates" )))
30
-
31
- env .filters ["accept_headers" ] = openapi .accept_headers
32
- env .filters ["attribute_name" ] = formatter .attribute_name
33
- env .filters ["block_comment" ] = formatter .block_comment
34
- env .filters ["camel_case" ] = formatter .camel_case
35
- env .filters ["collection_format" ] = openapi .collection_format
36
- env .filters ["format_server" ] = openapi .format_server
37
- env .filters ["format_value" ] = formatter .format_value
38
- env .filters ["parameter_schema" ] = openapi .parameter_schema
39
- env .filters ["parameters" ] = openapi .parameters
40
- env .filters ["return_type" ] = openapi .return_type
41
- env .filters ["snake_case" ] = formatter .snake_case
42
- env .filters ["form_parameter" ] = openapi .form_parameter
43
- env .filters ["untitle_case" ] = formatter .untitle_case
44
- env .filters ["response_type" ] = openapi .get_type_for_response
45
- env .filters ["responses_by_types" ] = openapi .responses_by_types
46
- env .filters ["docstring" ] = formatter .docstring
47
-
48
- env .globals ["get_references_for_model" ] = openapi .get_references_for_model
49
- env .globals ["package_name" ] = package_name
50
- env .globals ["npm_name" ] = npm_name
51
- env .globals ["enumerate" ] = enumerate
52
- env .globals ["get_name" ] = openapi .get_name
53
- env .globals ["type_to_typescript" ] = openapi .type_to_typescript
54
- env .globals ["get_type_for_attribute" ] = openapi .get_type_for_attribute
55
- env .globals ["get_type_for_parameter" ] = openapi .get_type_for_parameter
56
- env .globals ["get_format_for_schema" ] = openapi .get_format_for_schema
57
- env .globals ["get_api_models" ] = openapi .get_api_models
58
- env .globals ["response" ] = openapi .response
59
- env .globals ["get_enums_list" ] = openapi .get_enums_list
60
- env .globals ["get_format" ] = openapi .get_format
61
- env .globals ["get_default" ] = openapi .get_default
62
- env .globals ["get_container" ] = openapi .get_container
63
- env .globals ["get_container_type" ] = openapi .get_container_type
64
- env .globals ["get_type_at_path" ] = openapi .get_type_at_path
65
- env .globals ["get_security_names" ] = openapi .get_security_names
66
-
67
- api_j2 = env .get_template ("api/api.j2" )
68
- model_j2 = env .get_template ("model/model.j2" )
69
-
70
- extra_files = {
71
- "models/ObjectSerializer.ts" : env .get_template ("model/ObjectSerializer.j2" ),
72
- "index.ts" : env .get_template ("index.j2" ),
73
- }
74
-
75
- test_scenarios_files = {
76
- "scenarios_model_mapping.ts" : env .get_template ("scenarios_model_mapping.j2" )
77
- }
78
-
79
- all_specs = {}
80
- all_apis = {}
81
- for spec_path in specs :
82
- spec = openapi .load (spec_path )
83
-
84
- version = spec_path .parent .name
85
-
86
- env .globals ["version" ] = version
87
- env .globals ["openapi" ] = spec
88
-
89
- all_specs [version ] = spec
90
-
91
- apis = openapi .apis (spec )
92
- models = openapi .models (spec )
93
-
94
- all_apis [version ] = apis
95
-
96
- package_path = output / f"{ package_name } -{ version } "
97
-
98
- for name , model in models .items ():
99
- filename = name + ".ts"
100
- model_path = package_path / "models" / filename
101
- model_path .parent .mkdir (parents = True , exist_ok = True )
102
- with model_path .open ("w+" ) as fp :
103
- fp .write (model_j2 .render (name = name , model = model ))
104
-
105
- for name , operations in apis .items ():
106
- filename = name .replace (" " , "" ).replace ("-" , "" ) + "Api.ts"
107
- api_path = package_path / "apis" / filename
108
- api_path .parent .mkdir (parents = True , exist_ok = True )
109
- with api_path .open ("w+" ) as fp :
110
- fp .write (api_j2 .render (name = name , operations = operations , models = models ))
111
-
112
- for name , template in extra_files .items ():
113
- filename = package_path / name
114
- filename .parent .mkdir (parents = True , exist_ok = True )
115
- with filename .open ("w+" ) as fp :
116
- fp .write (template .render (apis = apis , models = models ))
117
-
118
- common_files = {
119
- "util.ts" : env .get_template ("util.j2" ),
120
- "baseapi.ts" : env .get_template ("api/baseapi.j2" ),
121
- "exception.ts" : env .get_template ("api/exception.j2" ),
122
- "auth.ts" : env .get_template ("auth/auth.j2" ),
123
- "http/http.ts" : env .get_template ("http/http.j2" ),
124
- "http/isomorphic-fetch.ts" : env .get_template ("http/isomorphic-fetch.j2" ),
125
- "servers.ts" : env .get_template ("servers.j2" ),
126
- "configuration.ts" : env .get_template ("configuration.j2" ),
127
- "index.ts" : env .get_template ("common_index.j2" ),
128
- }
129
-
130
- for name , template in common_files .items ():
131
- filename = output / "datadog-api-client-common" / name
132
- filename .parent .mkdir (parents = True , exist_ok = True )
133
- with filename .open ("w+" ) as fp :
134
- fp .write (template .render (apis = all_apis ))
135
-
136
- # Parameter mappings for bdd tests
137
- scenarios_test_output = pathlib .Path ("../features/support/" )
138
- for name , template in test_scenarios_files .items ():
139
- filename = scenarios_test_output / name
140
- with filename .open ("w" ) as fp :
141
- fp .write (template .render (all_apis = all_apis ))
9
+ print ("This generator is deprecated and will be fully removed in the future." )
10
+ exit (1 )
0 commit comments