@@ -37,11 +37,11 @@ def __init__(self, api_name: str, api_version: str):
37
37
api_name: The name of the Google API (e.g., "calendar")
38
38
api_version: The version of the API (e.g., "v3")
39
39
"""
40
- self .api_name = api_name
41
- self .api_version = api_version
42
- self .google_api_resource = None
43
- self .google_api_spec = None
44
- self .openapi_spec = {
40
+ self ._api_name = api_name
41
+ self ._api_version = api_version
42
+ self ._google_api_resource = None
43
+ self ._google_api_spec = None
44
+ self ._openapi_spec = {
45
45
"openapi" : "3.0.0" ,
46
46
"info" : {},
47
47
"servers" : [],
@@ -53,18 +53,20 @@ def fetch_google_api_spec(self) -> None:
53
53
"""Fetches the Google API specification using discovery service."""
54
54
try :
55
55
logger .info (
56
- "Fetching Google API spec for %s %s" , self .api_name , self .api_version
56
+ "Fetching Google API spec for %s %s" ,
57
+ self ._api_name ,
58
+ self ._api_version ,
57
59
)
58
60
# Build a resource object for the specified API
59
- self .google_api_resource = build (self .api_name , self .api_version )
61
+ self ._google_api_resource = build (self ._api_name , self ._api_version )
60
62
61
63
# Access the underlying API discovery document
62
- self .google_api_spec = self .google_api_resource ._rootDesc
64
+ self ._google_api_spec = self ._google_api_resource ._rootDesc
63
65
64
- if not self .google_api_spec :
66
+ if not self ._google_api_spec :
65
67
raise ValueError ("Failed to retrieve API specification" )
66
68
67
- logger .info ("Successfully fetched %s API specification" , self .api_name )
69
+ logger .info ("Successfully fetched %s API specification" , self ._api_name )
68
70
except HttpError as e :
69
71
logger .error ("HTTP Error: %s" , e )
70
72
raise
@@ -78,7 +80,7 @@ def convert(self) -> Dict[str, Any]:
78
80
Returns:
79
81
Dict containing the converted OpenAPI v3 specification
80
82
"""
81
- if not self .google_api_spec :
83
+ if not self ._google_api_spec :
82
84
self .fetch_google_api_spec ()
83
85
84
86
# Convert basic API information
@@ -94,49 +96,49 @@ def convert(self) -> Dict[str, Any]:
94
96
self ._convert_schemas ()
95
97
96
98
# Convert endpoints/paths
97
- self ._convert_resources (self .google_api_spec .get ("resources" , {}))
99
+ self ._convert_resources (self ._google_api_spec .get ("resources" , {}))
98
100
99
101
# Convert top-level methods, if any
100
- self ._convert_methods (self .google_api_spec .get ("methods" , {}), "/" )
102
+ self ._convert_methods (self ._google_api_spec .get ("methods" , {}), "/" )
101
103
102
- return self .openapi_spec
104
+ return self ._openapi_spec
103
105
104
106
def _convert_info (self ) -> None :
105
107
"""Convert basic API information."""
106
- self .openapi_spec ["info" ] = {
107
- "title" : self .google_api_spec .get ("title" , f"{ self .api_name } API" ),
108
- "description" : self .google_api_spec .get ("description" , "" ),
109
- "version" : self .google_api_spec .get ("version" , self .api_version ),
108
+ self ._openapi_spec ["info" ] = {
109
+ "title" : self ._google_api_spec .get ("title" , f"{ self ._api_name } API" ),
110
+ "description" : self ._google_api_spec .get ("description" , "" ),
111
+ "version" : self ._google_api_spec .get ("version" , self ._api_version ),
110
112
"contact" : {},
111
- "termsOfService" : self .google_api_spec .get ("documentationLink" , "" ),
113
+ "termsOfService" : self ._google_api_spec .get ("documentationLink" , "" ),
112
114
}
113
115
114
116
# Add documentation links if available
115
- docs_link = self .google_api_spec .get ("documentationLink" )
117
+ docs_link = self ._google_api_spec .get ("documentationLink" )
116
118
if docs_link :
117
- self .openapi_spec ["externalDocs" ] = {
119
+ self ._openapi_spec ["externalDocs" ] = {
118
120
"description" : "API Documentation" ,
119
121
"url" : docs_link ,
120
122
}
121
123
122
124
def _convert_servers (self ) -> None :
123
125
"""Convert server information."""
124
- base_url = self .google_api_spec .get (
126
+ base_url = self ._google_api_spec .get (
125
127
"rootUrl" , ""
126
- ) + self .google_api_spec .get ("servicePath" , "" )
128
+ ) + self ._google_api_spec .get ("servicePath" , "" )
127
129
128
130
# Remove trailing slash if present
129
131
if base_url .endswith ("/" ):
130
132
base_url = base_url [:- 1 ]
131
133
132
- self .openapi_spec ["servers" ] = [{
134
+ self ._openapi_spec ["servers" ] = [{
133
135
"url" : base_url ,
134
- "description" : f"{ self .api_name } { self .api_version } API" ,
136
+ "description" : f"{ self ._api_name } { self ._api_version } API" ,
135
137
}]
136
138
137
139
def _convert_security_schemes (self ) -> None :
138
140
"""Convert authentication and authorization schemes."""
139
- auth = self .google_api_spec .get ("auth" , {})
141
+ auth = self ._google_api_spec .get ("auth" , {})
140
142
oauth2 = auth .get ("oauth2" , {})
141
143
142
144
if oauth2 :
@@ -147,7 +149,7 @@ def _convert_security_schemes(self) -> None:
147
149
for scope , scope_info in scopes .items ():
148
150
formatted_scopes [scope ] = scope_info .get ("description" , "" )
149
151
150
- self .openapi_spec ["components" ]["securitySchemes" ]["oauth2" ] = {
152
+ self ._openapi_spec ["components" ]["securitySchemes" ]["oauth2" ] = {
151
153
"type" : "oauth2" ,
152
154
"description" : "OAuth 2.0 authentication" ,
153
155
"flows" : {
@@ -162,26 +164,28 @@ def _convert_security_schemes(self) -> None:
162
164
}
163
165
164
166
# Add API key authentication (most Google APIs support this)
165
- self .openapi_spec ["components" ]["securitySchemes" ]["apiKey" ] = {
167
+ self ._openapi_spec ["components" ]["securitySchemes" ]["apiKey" ] = {
166
168
"type" : "apiKey" ,
167
169
"in" : "query" ,
168
170
"name" : "key" ,
169
171
"description" : "API key for accessing this API" ,
170
172
}
171
173
172
174
# Create global security requirement
173
- self .openapi_spec ["security" ] = [
175
+ self ._openapi_spec ["security" ] = [
174
176
{"oauth2" : list (formatted_scopes .keys ())} if oauth2 else {},
175
177
{"apiKey" : []},
176
178
]
177
179
178
180
def _convert_schemas (self ) -> None :
179
181
"""Convert schema definitions (models)."""
180
- schemas = self .google_api_spec .get ("schemas" , {})
182
+ schemas = self ._google_api_spec .get ("schemas" , {})
181
183
182
184
for schema_name , schema_def in schemas .items ():
183
185
converted_schema = self ._convert_schema_object (schema_def )
184
- self .openapi_spec ["components" ]["schemas" ][schema_name ] = converted_schema
186
+ self ._openapi_spec ["components" ]["schemas" ][
187
+ schema_name
188
+ ] = converted_schema
185
189
186
190
def _convert_schema_object (
187
191
self , schema_def : Dict [str , Any ]
@@ -314,11 +318,11 @@ def _convert_methods(
314
318
path_params = self ._extract_path_parameters (rest_path )
315
319
316
320
# Create path entry if it doesn't exist
317
- if rest_path not in self .openapi_spec ["paths" ]:
318
- self .openapi_spec ["paths" ][rest_path ] = {}
321
+ if rest_path not in self ._openapi_spec ["paths" ]:
322
+ self ._openapi_spec ["paths" ][rest_path ] = {}
319
323
320
324
# Add the operation for this method
321
- self .openapi_spec ["paths" ][rest_path ][http_method ] = (
325
+ self ._openapi_spec ["paths" ][rest_path ][http_method ] = (
322
326
self ._convert_operation (method_data , path_params )
323
327
)
324
328
@@ -472,7 +476,7 @@ def save_openapi_spec(self, output_path: str) -> None:
472
476
output_path: Path where the OpenAPI spec should be saved
473
477
"""
474
478
with open (output_path , "w" , encoding = "utf-8" ) as f :
475
- json .dump (self .openapi_spec , f , indent = 2 )
10000
479
+ json .dump (self ._openapi_spec , f , indent = 2 )
476
480
logger .info ("OpenAPI specification saved to %s" , output_path )
477
481
478
482
0 commit comments