6
6
7
7
result = {}
8
8
result_ext = {}
9
- proto = {}
9
+ proto = {
10
+ "get" : {},
11
+ "post" : {},
12
+ "put" : {},
13
+ "delete" : {},
14
+ "patch" : {}
15
+ }
16
+
17
+ def strip_var (original_pattern , original_path ):
18
+ newpattern = re .sub (r'{[^/=]+?}' , r'*' , original_pattern )
19
+ newpattern = re .sub (r'{.+?=(.+)}' , r'\1' , newpattern )
20
+ offset = newpattern .find ('/' , newpattern .find ('/v' ) + 1 )
21
+ version = newpattern [1 :offset ]
22
+ newpattern = "*" + newpattern [offset :]
23
+ newpath = original_path .replace (version , "{" + "_version}" )
24
+
25
+ return newpattern , newpath
26
+
27
+ def dig_nested (obj , path ):
28
+ if 'nested' in obj :
29
+ for k , v in obj ['nested' ].items ():
30
+ dig_nested (v , path + k + "/" )
31
+ if 'methods' in obj :
32
+ for k , v in obj ['methods' ].items ():
33
+ if 'options' in v :
34
+ if "(google.api.http).get" in v ['options' ]:
35
+ newpattern , newpath = strip_var (v ['options' ]["(google.api.http).get" ], path + k )
36
+ proto ['get' ][newpattern ] = newpath
37
+ if "(google.api.http).post" in v ['options' ]:
38
+ newpattern , newpath = strip_var (v ['options' ]["(google.api.http).post" ], path + k )
39
+ proto ['post' ][newpattern ] = newpath
40
+ if "(google.api.http).put" in v ['options' ]:
41
+ newpattern , newpath = strip_var (v ['options' ]["(google.api.http).put" ], path + k )
42
+ proto ['put' ][newpattern ] = newpath
43
+ if "(google.api.http).delete" in v ['options' ]:
44
+ newpattern , newpath = strip_var (v ['options' ]["(google.api.http).delete" ], path + k )
45
+ proto ['delete' ][newpattern ] = newpath
46
+ if "(google.api.http).patch" in v ['options' ]:
47
+ newpattern , newpath = strip_var (v ['options' ]["(google.api.http).patch" ], path + k )
48
+ proto ['patch' ][newpattern ] = newpath
10
49
11
50
def resources_recurse (resources , resource_type_path ):
12
51
for resource_type in resources .keys ():
13
- print ("-- " + resource_type )
52
+ # print("-- " + resource_type)
14
53
if 'methods' in resources [resource_type ]:
15
54
for method_name in resources [resource_type ]['methods' ].keys ():
16
55
method_id = resources [resource_type ]['methods' ][method_name ]['id' ]
@@ -19,14 +58,26 @@ def resources_recurse(resources, resource_type_path):
19
58
if flatpath .startswith (apidetail ['version' ] + "/" ):
20
59
flatpath = "{" + "_version}" + flatpath [len (apidetail ['version' ]):]
21
60
61
+ normalized_flatpath = re .sub (r'{(.+?)}' , r'*' , flatpath )
62
+ api_path = None
63
+ if normalized_flatpath in proto [resources [resource_type ]['methods' ][method_name ]['httpMethod' ].lower ()]:
64
+ api_path = proto [resources [resource_type ]['methods' ][method_name ]['httpMethod' ].lower ()][normalized_flatpath ]
65
+ del proto [resources [resource_type ]['methods' ][method_name ]['httpMethod' ].lower ()][normalized_flatpath ]
66
+ elif method_id .startswith ("compute." ): # missing leading */
67
+ normalized_flatpath = "*/" + normalized_flatpath
68
+ if normalized_flatpath in proto [resources [resource_type ]['methods' ][method_name ]['httpMethod' ].lower ()]:
69
+ api_path = proto [resources [resource_type ]['methods' ][method_name ]['httpMethod' ].lower ()][normalized_flatpath ]
70
+ del proto [resources [resource_type ]['methods' ][method_name ]['httpMethod' ].lower ()][normalized_flatpath ]
71
+
22
72
result [api ['name' ]]['methods' ][method_id ] = {
23
73
'description' : resources [resource_type ]['methods' ][method_name ]['description' ] if 'description' in resources [resource_type ]['methods' ][method_name ] else '' ,
24
74
'httpMethod' : resources [resource_type ]['methods' ][method_name ]['httpMethod' ],
25
75
'method' : method_name ,
26
76
'resourceType' : resource_type ,
27
77
'resourceTypePath' : resource_type_path ,
28
78
'flatPath' : flatpath ,
29
- 'versions' : []
79
+ 'versions' : [],
80
+ 'apiPath' : api_path ,
30
81
}
31
82
result_ext [api ['name' ]]['methods' ][method_id ] = {
32
83
'description' : resources [resource_type ]['methods' ][method_name ]['description' ] if 'description' in resources [resource_type ]['methods' ][method_name ] else '' ,
@@ -36,6 +87,7 @@ def resources_recurse(resources, resource_type_path):
36
87
'resourceTypePath' : resource_type_path ,
37
88
'flatPath' : flatpath ,
38
89
'versions' : [],
90
+ 'apiPath' : api_path ,
39
91
'parameters' : resources [resource_type ]['methods' ][method_name ]['parameters' ] if 'parameters' in resources [resource_type ]['methods' ][method_name ] else {}
40
92
}
41
93
result [api ['name' ]]['methods' ][method_id ]['versions' ].append (apidetail ['version' ])
@@ -47,6 +99,13 @@ def resources_recurse(resources, resource_type_path):
47
99
with open ("gcp/google-api-go-client/api-list.json" , "r" ) as f :
48
100
apilist = json .loads (f .read ())['items' ]
49
101
102
+ for dirpath , dnames , fnames in os .walk ("gcp/google-cloud-node/" ):
103
+ for fn in fnames :
104
+ if fn == "protos.json" :
105
+ with open (os .path .join (dirpath , fn )) as f :
106
+ singleproto = json .loads (f .read ())
107
+ dig_nested (singleproto , '' )
108
+
50
109
for api in apilist :
51
110
if api ['name' ] not in result :
52
111
result [api ['name' ]] = {
@@ -70,13 +129,16 @@ def resources_recurse(resources, resource_type_path):
70
129
path_version = "v0.alpha"
71
130
if path_version == "beta" :
72
131
path_version = "v0.beta"
73
- with open ("gcp/google-api-go-client/{}/{}/{}-api.json" .format (api ['name' ], path_version , api ['name' ]), "r" ) as f :
74
- apidetail = json .loads (f .read ())
75
- print (api ['name' ])
76
- print ("- " + api ['version' ])
77
- resources_recurse ({
78
- "" : apidetail
79
- }, "" )
132
+ try :
133
+ with open ("gcp/google-api-go-client/{}/{}/{}-api.json" .format (api ['name' ], path_version , api ['name' ]), "r" ) as f :
134
+ apidetail = json .loads (f .read ())
135
+ #print(api['name'])
136
+ #print("- " + api['version'])
137
+ resources_recurse ({
138
+ "" : apidetail
139
+ }, "" )
140
+ except Exception as e :
141
+ print (e )
80
142
81
143
with open ("gcp/methods.json" , "w" ) as f :
82
144
f .write (json .dumps (result , indent = 2 , sort_keys = True ))
0 commit comments