http-content-parser 0.0.24__tar.gz → 0.0.25__tar.gz

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (18) hide show
  1. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/PKG-INFO +1 -1
  2. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/pyproject.toml +1 -1
  3. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/src/http_content_parser/param_util.py +53 -27
  4. http_content_parser-0.0.25/tests/test_param_util.py +130 -0
  5. http_content_parser-0.0.24/tests/test_param_util.py +0 -34
  6. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/.gitignore +0 -0
  7. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/LICENSE +0 -0
  8. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/README.md +0 -0
  9. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/requirements.txt +0 -0
  10. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/src/http_content_parser/__init__.py +0 -0
  11. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/src/http_content_parser/api_parser.py +0 -0
  12. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/src/http_content_parser/curl_parser.py +0 -0
  13. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/src/http_content_parser/openapi_parser.py +0 -0
  14. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/src/http_content_parser/postman_parser.py +0 -0
  15. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/src/http_content_parser/req_data.py +0 -0
  16. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/src/http_content_parser/swagger2_parser.py +0 -0
  17. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/tests/test_api_parser.py +0 -0
  18. {http_content_parser-0.0.24 → http_content_parser-0.0.25}/tests/test_postman.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: http_content_parser
3
- Version: 0.0.24
3
+ Version: 0.0.25
4
4
  Summary: parse http's payload and response
5
5
  Author-email: leo <suleiabc@gmail.com>
6
6
  License-File: LICENSE
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "http_content_parser"
7
- version = "0.0.24"
7
+ version = "0.0.25"
8
8
  authors = [{ name = "leo", email = "suleiabc@gmail.com" }]
9
9
  description = "parse http's payload and response"
10
10
  readme = "README.md"
@@ -50,20 +50,29 @@ class ParamUtil(object):
50
50
  # empty list -> record type
51
51
  self._param_list.append(prefix_str + f"['{k}']{middle_char}{new_v}")
52
52
  else:
53
- # record this key as list type, then iterate items
54
- self._param_list.append(prefix_str + f"['{k}']{middle_char}{new_v}")
55
- i = 0
56
- for item in v:
57
- if isinstance(item, dict):
58
- temp_prefix_str = prefix_str + f"['{k}'][{i}]"
59
- # record the indexed item as dict type in recursion
60
- self.split_dict_params(
61
- item, temp_prefix_str, middle_char, nontype
62
- )
63
- i += 1
64
- else:
65
- # primitive list element -> record list type only
66
- break
53
+ # If list contains a single primitive element, record that element directly
54
+ if len(v) == 1 and not isinstance(v[0], dict):
55
+ elem_v = self.adjust_type(v[0], nontype=nontype)
56
+ self._param_list.append(
57
+ prefix_str + f"['{k}']{middle_char}{elem_v}"
58
+ )
59
+ else:
60
+ # record this key as list type, then iterate items
61
+ self._param_list.append(
62
+ prefix_str + f"['{k}']{middle_char}{new_v}"
63
+ )
64
+ i = 0
65
+ for item in v:
66
+ if isinstance(item, dict):
67
+ temp_prefix_str = prefix_str + f"['{k}'][{i}]"
68
+ # record the indexed item as dict type in recursion
69
+ self.split_dict_params(
70
+ item, temp_prefix_str, middle_char, nontype
71
+ )
72
+ i += 1
73
+ else:
74
+ # primitive list element -> nothing further
75
+ break
67
76
 
68
77
  else:
69
78
  self._param_list.append(prefix_str + f"['{k}']{middle_char}{new_v}")
@@ -83,8 +92,15 @@ class ParamUtil(object):
83
92
 
84
93
  # nontype用来控制value值是否输出原值还是参数类型
85
94
  def adjust_type(self, value, nontype=False):
86
- if isinstance(value, (bool, int, dict, list)):
95
+ if isinstance(value, bool) or isinstance(value, int):
87
96
  return value
97
+ if isinstance(value, dict):
98
+ return "{}"
99
+ if isinstance(value, list):
100
+ if len(value) == 1 and not isinstance(value[0], dict):
101
+ return value
102
+ else:
103
+ return "[]"
88
104
  else:
89
105
  if nontype and len(value) > 10:
90
106
  return "'string'"
@@ -111,18 +127,25 @@ class SwaggerParam(object):
111
127
  if not v:
112
128
  self._param_list.append({prefix_str + f"['{k}']": new_v})
113
129
  else:
114
- # record this key as list type
115
- self._param_list.append({prefix_str + f"['{k}']": new_v})
116
- i = 0
117
- for item in v:
118
- if isinstance(item, dict):
119
- temp_prefix_str = prefix_str + f"['{k}'][{i}]"
120
- self.split_params_for_dict_type(
121
- item, temp_prefix_str, nontype
122
- )
123
- i += 1
130
+ # If list contains a single primitive element, return original key/value at top-level
131
+ if len(v) == 1 and not isinstance(v[0], dict):
132
+ if prefix_str == "":
133
+ self._param_list.append({k: v})
124
134
  else:
125
- break
135
+ self._param_list.append({prefix_str + f"['{k}']": repr(v)})
136
+ else:
137
+ # record this key as list type
138
+ self._param_list.append({prefix_str + f"['{k}']": new_v})
139
+ i = 0
140
+ for item in v:
141
+ if isinstance(item, dict):
142
+ temp_prefix_str = prefix_str + f"['{k}'][{i}]"
143
+ self.split_params_for_dict_type(
144
+ item, temp_prefix_str, nontype
145
+ )
146
+ i += 1
147
+ else:
148
+ break
126
149
 
127
150
  else:
128
151
  self._param_list.append({prefix_str + f"['{k}']": new_v})
@@ -147,7 +170,10 @@ class SwaggerParam(object):
147
170
  if isinstance(value, dict):
148
171
  return "{}"
149
172
  if isinstance(value, list):
150
- return "[]"
173
+ if len(value) == 1 and not isinstance(value[0], dict):
174
+ return value
175
+ else:
176
+ return "[]"
151
177
  else:
152
178
  if nontype and len(value) > 10:
153
179
  return "'string'"
@@ -0,0 +1,130 @@
1
+ import json
2
+ from http_content_parser.param_util import ParamUtil
3
+
4
+
5
+ def test_param_util():
6
+ s = {
7
+ "variable": [
8
+ {"key": "host", "value": "api.example.com"},
9
+ {"name": "token", "value": "abc123"},
10
+ ],
11
+ "item": [
12
+ {
13
+ "name": "Folder",
14
+ "item": [
15
+ {
16
+ "name": "GetUser",
17
+ "request": {
18
+ "method": "GET",
19
+ "header": [
20
+ {"key": "Authorization", "value": "Bearer {{token}}"}
21
+ ],
22
+ "url": {
23
+ "raw": "https://{{host}}/v1/users?id={{uid}}",
24
+ "path": ["v1", "users"],
25
+ "query": [{"key": "id", "value": "{{uid}}"}],
26
+ },
27
+ },
28
+ "variable": [{"key": "uid", "value": "42"}],
29
+ }
30
+ ],
31
+ }
32
+ ],
33
+ }
34
+ result = ParamUtil.split_swagger_param_and_type(s, nontype=False)
35
+ expected_result = [
36
+ {"['variable']": "[]"},
37
+ {"['variable'][0]['key']": "'host'"},
38
+ {"['variable'][0]['value']": "'api.example.com'"},
39
+ {"['variable'][1]['name']": "'token'"},
40
+ {"['variable'][1]['value']": "'abc123'"},
41
+ {"['item']": "[]"},
42
+ {"['item'][0]['name']": "'Folder'"},
43
+ {"['item'][0]['item']": "[]"},
44
+ {"['item'][0]['item'][0]['name']": "'GetUser'"},
45
+ {"['item'][0]['item'][0]['request']": "{}"},
46
+ {"['item'][0]['item'][0]['request']['method']": "'GET'"},
47
+ {"['item'][0]['item'][0]['request']['header']": "[]"},
48
+ {"['item'][0]['item'][0]['request']['header'][0]['key']": "'Authorization'"},
49
+ {
50
+ "['item'][0]['item'][0]['request']['header'][0]['value']": "'Bearer {{token}}'"
51
+ },
52
+ {"['item'][0]['item'][0]['request']['url']": "{}"},
53
+ {
54
+ "['item'][0]['item'][0]['request']['url']['raw']": "'https://{{host}}/v1/users?id={{uid}}'"
55
+ },
56
+ {"['item'][0]['item'][0]['request']['url']['path']": "[]"},
57
+ {"['item'][0]['item'][0]['request']['url']['query']": "[]"},
58
+ {"['item'][0]['item'][0]['request']['url']['query'][0]['key']": "'id'"},
59
+ {"['item'][0]['item'][0]['request']['url']['query'][0]['value']": "'{{uid}}'"},
60
+ {"['item'][0]['item'][0]['variable']": "[]"},
61
+ {"['item'][0]['item'][0]['variable'][0]['key']": "'uid'"},
62
+ {"['item'][0]['item'][0]['variable'][0]['value']": "'42'"},
63
+ ]
64
+ assert result == expected_result
65
+
66
+
67
+ def test_param_util2():
68
+ s = {
69
+ "vmid": ["3690976212158729"],
70
+ "type": ["1"],
71
+ "pn": ["1"],
72
+ "ps": ["24"],
73
+ "playform": ["web"],
74
+ "follow_status": ["0"],
75
+ "web_location": ["333.1387"],
76
+ "w_rid": ["a64a78654a1948dcc86f483f3d86aaef"],
77
+ "wts": ["1765612099"],
78
+ }
79
+ result = ParamUtil.split_swagger_param_and_type(s, nontype=False)
80
+ expected_result = [
81
+ {"vmid": ["3690976212158729"]},
82
+ {"type": ["1"]},
83
+ {"pn": ["1"]},
84
+ {"ps": ["24"]},
85
+ {"playform": ["web"]},
86
+ {"follow_status": ["0"]},
87
+ {"web_location": ["333.1387"]},
88
+ {"w_rid": ["a64a78654a1948dcc86f483f3d86aaef"]},
89
+ {"wts": ["1765612099"]},
90
+ ]
91
+ assert result == expected_result
92
+
93
+
94
+ def test_param_util3():
95
+ s = {
96
+ "vmid": "3690976212158729",
97
+ "type": "1",
98
+ "pn": "1",
99
+ "ps": ["24"],
100
+ "playform": ["web"],
101
+ "follow_status": "0",
102
+ "web_location": "333.1387",
103
+ "w_rid": "a64a78654a1948dcc86f483f3d86aaef",
104
+ "wts": ["1765612099"],
105
+ }
106
+ result = ParamUtil.split_swagger_param_and_type(s, nontype=False)
107
+ expected_result = [
108
+ {"['vmid']": "'3690976212158729'"},
109
+ {"['type']": "'1'"},
110
+ {"['pn']": "'1'"},
111
+ {"ps": ["24"]},
112
+ {"playform": ["web"]},
113
+ {"['follow_status']": "'0'"},
114
+ {"['web_location']": "'333.1387'"},
115
+ {"['w_rid']": "'a64a78654a1948dcc86f483f3d86aaef'"},
116
+ {"wts": ["1765612099"]},
117
+ ]
118
+ assert result == expected_result
119
+
120
+
121
+ def test_param_util4():
122
+ s = {"vmid": "3690976212158729", "type": {}, "name": []}
123
+ result = ParamUtil.split_swagger_param_and_type(s, nontype=False)
124
+ print(result)
125
+ expected_result = [
126
+ {"['vmid']": "'3690976212158729'"},
127
+ {"['type']": "{}"},
128
+ {"['name']": "[]"},
129
+ ]
130
+ assert result == expected_result
@@ -1,34 +0,0 @@
1
- from http_content_parser.param_util import ParamUtil
2
-
3
-
4
- def test_param_util():
5
- s = {
6
- "variable": [
7
- {"key": "host", "value": "api.example.com"},
8
- {"name": "token", "value": "abc123"},
9
- ],
10
- "item": [
11
- {
12
- "name": "Folder",
13
- "item": [
14
- {
15
- "name": "GetUser",
16
- "request": {
17
- "method": "GET",
18
- "header": [
19
- {"key": "Authorization", "value": "Bearer {{token}}"}
20
- ],
21
- "url": {
22
- "raw": "https://{{host}}/v1/users?id={{uid}}",
23
- "path": ["v1", "users"],
24
- "query": [{"key": "id", "value": "{{uid}}"}],
25
- },
26
- },
27
- "variable": [{"key": "uid", "value": "42"}],
28
- }
29
- ],
30
- }
31
- ],
32
- }
33
- result = ParamUtil.split_swagger_param_and_type(s, nontype=True)
34
- print(result)