-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerateApi.py
116 lines (83 loc) · 2.79 KB
/
generateApi.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
from __future__ import print_function, unicode_literals
import argparse
import json
import os
import subprocess
import sys
import tempfile
import textwrap
class _FancyFormatter(argparse.ArgumentDefaultsHelpFormatter,
argparse.RawDescriptionHelpFormatter):
pass
_v_format = {
"name": '',
"path": '',
"type": '',
"param": ''
}
def main(sysargs=sys.argv[:]):
parser = argparse.ArgumentParser(
description='Generate code!',
formatter_class=_FancyFormatter)
parser.add_argument(
'-i', '--in-json',
type=argparse.FileType('r'),
default=sys.stdin,
help='Input JSON file which defines each type to be generated'
)
parser.epilog = __doc__
args = parser.parse_args(sysargs[1:])
_generate_code(args.in_json)
return 0
def _generate_code(input_json):
global output_go
data = json.load(input_json)
for key, values in data.items():
if key != "AccessApi":
output_go = open(key + '.go', mode='w', buffering=-1)
tmpfile = tempfile.NamedTemporaryFile(suffix='.go', delete=False)
_write_files(tmpfile, values)
tmpfile.close()
new_content = subprocess.check_output(
['goimports', tmpfile.name]
).decode('utf-8')
output_go.write(new_content)
output_go.flush()
os.remove(tmpfile.name)
def _write_files(outfile, data):
_fwrite(outfile, """\
// this documentation
package go_nifi_api
import "./types"
// WARNING: This file is generated!
""")
for i in data['methods']:
_format().setdefault("name", '{}{}'.format(_get_method(i["method"]), i["path"].replace('/', '')))
_format().setdefault("path", i["path"].lower())
if "body" in i["response"][0]:
_format().setdefault("type", i["response"][0]["body"])
if "path" in i["request"][0]:
param = "{}, ".format(i["request"][0]["param"])
_fwrite(outfile, """\
// {name} this godoc
func (a *app) {name}({param}token, method string) *types.{type} {{
variables := types.{type}{{}}
url := fmt.Sprintf("%s/%s/%s", a.host, "{path}", "")
bytes, _ := a.Do(url, token, method, nil)
_ = json.Unmarshal(bytes, &variables)
return &variables
}}\n\n
""".format(**_format()))
def _fwrite(outfile, content):
print(textwrap.dedent(content), end='', file=outfile)
def _format():
return _v_format
def _get_method(type):
return {
'POST': 'create',
'GET ': 'get',
'DELETE': 'delete',
'PUT': 'put',
}[type]
if __name__ == '__main__':
sys.exit(main())