|
18 | 18 | import sys |
19 | 19 | from collections import OrderedDict |
20 | 20 |
|
| 21 | +import urllib3 |
| 22 | +from constants import KUBERNETES_BRANCH, SPEC_VERSION |
| 23 | + |
21 | 24 | # these four constants are shown as part of this example in []: |
22 | 25 | # "[watch]Pod[List]" is the deprecated version of "[list]Pod?[watch]=True" |
23 | 26 | WATCH_OP_PREFIX = "watch" |
24 | 27 | WATCH_OP_SUFFIX = "List" |
25 | 28 | LIST_OP_PREFIX = "list" |
26 | 29 | WATCH_QUERY_PARAM_NAME = "watch" |
27 | 30 |
|
| 31 | +SPEC_URL = 'https://raw.githubusercontent.com/kubernetes/kubernetes/' \ |
| 32 | + '%s/api/openapi-spec/swagger.json' % KUBERNETES_BRANCH |
| 33 | + |
| 34 | +OUTPUT_PATH = os.path.join(os.path.dirname(__file__), 'swagger.json') |
28 | 35 |
|
29 | 36 | _ops = ['get', 'put', 'post', 'delete', 'options', 'head', 'patch'] |
30 | 37 |
|
@@ -94,37 +101,38 @@ def strip_tags_from_operation_id(operation, _): |
94 | 101 | operation['operationId'] = operation_id |
95 | 102 |
|
96 | 103 |
|
97 | | -def process_swagger(infile, outfile): |
98 | | - with open(infile, 'r') as f: |
99 | | - spec = json.load(f, object_pairs_hook=OrderedDict) |
| 104 | +def process_swagger(spec): |
| 105 | + apply_func_to_spec_operations(spec, strip_tags_from_operation_id) |
| 106 | + |
| 107 | + operation_ids = {} |
| 108 | + apply_func_to_spec_operations(spec, lambda op, _: operator.setitem( |
| 109 | + operation_ids, op['operationId'], op)) |
100 | 110 |
|
101 | | - apply_func_to_spec_operations(spec, strip_tags_from_operation_id) |
| 111 | + try: |
| 112 | + apply_func_to_spec_operations( |
| 113 | + spec, remove_watch_operations, operation_ids) |
| 114 | + except PreprocessingException as e: |
| 115 | + print(e.message) |
102 | 116 |
|
103 | | - operation_ids = {} |
104 | | - apply_func_to_spec_operations(spec, lambda op, _: operator.setitem( |
105 | | - operation_ids, op['operationId'], op)) |
| 117 | + # TODO: Kubernetes does not set a version for OpenAPI spec yet, |
| 118 | + # remove this when that is fixed. |
| 119 | + spec['info']['version'] = SPEC_VERSION |
106 | 120 |
|
107 | | - try: |
108 | | - apply_func_to_spec_operations( |
109 | | - spec, remove_watch_operations, operation_ids) |
110 | | - except PreprocessingException as e: |
111 | | - print(e.message) |
| 121 | + return spec |
112 | 122 |
|
113 | | - # TODO: Kubernetes does not set a version for OpenAPI spec yet, |
114 | | - # remove this when that is fixed. |
115 | | - spec['info']['version'] = "v1.5.0-beta.1" |
116 | 123 |
|
117 | | - with open(outfile, 'w') as out: |
118 | | - json.dump(spec, out, sort_keys=False, indent=2, |
| 124 | +def main(): |
| 125 | + pool = urllib3.PoolManager() |
| 126 | + with pool.request('GET', SPEC_URL, preload_content=False) as response: |
| 127 | + if response.status != 200: |
| 128 | + print "Error downloading spec file. Reason: %s" % response.reason |
| 129 | + return 1 |
| 130 | + in_spec = json.load(response, object_pairs_hook=OrderedDict) |
| 131 | + out_spec = process_swagger(in_spec) |
| 132 | + with open(OUTPUT_PATH, 'w') as out: |
| 133 | + json.dump(out_spec, out, sort_keys=False, indent=2, |
119 | 134 | separators=(',', ': '), ensure_ascii=True) |
| 135 | + return 0 |
120 | 136 |
|
121 | 137 |
|
122 | | -def main(): |
123 | | - if len(sys.argv) < 3: |
124 | | - print "Usage:\n\tpython %s infile outfile.\n" % sys.argv[0] |
125 | | - sys.exit(0) |
126 | | - if not os.path.isfile(sys.argv[1]): |
127 | | - print "Input file %s does not exist." % sys.argv[1] |
128 | | - process_swagger(sys.argv[1], sys.argv[2]) |
129 | | - |
130 | | -main() |
| 138 | +sys.exit(main()) |
0 commit comments