-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathupdate_snippets_with_responses.py
More file actions
182 lines (135 loc) · 4.72 KB
/
update_snippets_with_responses.py
File metadata and controls
182 lines (135 loc) · 4.72 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import os
import importlib
import logging
from http import HTTPStatus
import requests
import json
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("prefix", help="Snippets prefix to process. Like 'minimal_api', 'relationship_', etc")
parser.add_argument("-v", "--verbose", help="set logging level to DEBUG", action="store_true")
log = logging.getLogger(__name__)
SNIPPETS_DIR = "snippets"
SORT_KEYS_ON_DUMP = True
SNIPPET_RESULT_POSTFIX = "_result"
REMOVE_PYTHON_SNIPPET = True
SORTING_ORDER = [
"create",
"get",
"patch",
"update", # like patch
"delete",
]
ORDER_POS = {i: v for i, v in enumerate(SORTING_ORDER)}
class StrOrderCRUD:
def __init__(self, inner):
self.inner = inner
def __lt__(self, other):
index_1 = -1
index_2 = -1
for index, name in ORDER_POS.items():
substring = f"__{name}_"
if substring in self.inner:
index_1 = index
if substring in other.inner:
index_2 = index
if index_1 != index_2:
return index_1 < index_2
return self.inner < other.inner
def run_request_for_module(module_name: str):
log.info("Start processing %r", module_name)
module_full_name = ".".join((SNIPPETS_DIR, module_name))
log.debug("import module %s", module_full_name)
module = importlib.import_module(module_full_name)
log.info("Process module %s", module)
response: requests.Response = module.response
log.info("Response %s", response)
http_response_text = []
response_reason = response.reason or ""
if response.status_code != HTTPStatus.OK:
response_reason = response_reason.title()
http_response_text.append(
# "HTTP/1.1 201 Created"
"{} {} {}".format(
"HTTP/1.1",
response.status_code,
response_reason,
)
)
if ct := response.headers.get("content-type"):
http_response_text.append("{}: {}".format("Content-Type", ct))
http_response_text.append("")
if response.content:
# TODO: handle non-json response?
http_response_text.append(
json.dumps(
response.json(),
sort_keys=SORT_KEYS_ON_DUMP,
indent=2,
),
)
http_response_text.append("")
result_text = "\n".join(http_response_text)
log.debug("Result text:\n%s", result_text)
result_file_name = "/".join((SNIPPETS_DIR, module_name + SNIPPET_RESULT_POSTFIX))
with open(result_file_name, "w") as f:
res = f.write(result_text)
log.info("Wrote text (%s) to %r", res, result_file_name)
log.info("Processed %r", module_name)
def add_help_lines(lines: list, module_name: str) -> None:
"""
Append help lines to create smth like this:
'''
Request:
.. literalinclude:: ./http_snippets/snippets/minimal_api__create_user
:language: HTTP
Response:
.. literalinclude:: ./http_snippets/snippets/minimal_api__create_user_result
:language: HTTP
'''
"""
literalinclude_file = ".. literalinclude:: ./http_snippets/snippets/" + module_name
rst_language_http = " :language: HTTP"
lines.append("")
lines.append("Request:")
lines.append("")
lines.append(literalinclude_file)
lines.append(rst_language_http)
lines.append("")
lines.append("Response:")
lines.append("")
lines.append(literalinclude_file + SNIPPET_RESULT_POSTFIX)
lines.append(rst_language_http)
lines.append("")
def main():
args = parser.parse_args()
if args.verbose:
logging.basicConfig(level=logging.DEBUG)
log.warning("Starting")
available_modules = os.listdir(SNIPPETS_DIR)
log.debug("all available snippets: %s", available_modules)
modules_to_process = list(
# exclude unknown
filter(lambda name: name.startswith(args.prefix), available_modules)
)
modules_to_process.sort(key=StrOrderCRUD)
log.warning("modules to process (with order): %s", modules_to_process)
result_help_text = []
result_help_text.append("=" * 30)
for module_file in modules_to_process:
if module_file.endswith(".py"):
module_name = module_file[:-3]
try:
run_request_for_module(module_name)
except Exception:
log.exception("Could not process module %r, skipping", module_file)
else:
if REMOVE_PYTHON_SNIPPET:
os.unlink("/".join((SNIPPETS_DIR, module_file)))
add_help_lines(result_help_text, module_name)
result_help_text.append("=" * 30)
result_help_text.append("")
print("\n".join(result_help_text))
log.warning("Done")
if __name__ == "__main__":
main()