lapis-api 0.2.1__tar.gz → 0.2.1.post1__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 (33) hide show
  1. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/PKG-INFO +2 -1
  2. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/pyproject.toml +2 -1
  3. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/src/lapis/lapis.py +58 -34
  4. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/.github/CONTRIBUTIONS.md +0 -0
  5. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/.github/workflows/pylint.yml +0 -0
  6. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/.github/workflows/python-publish.yml +0 -0
  7. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/.gitignore +0 -0
  8. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/.pylintrc +0 -0
  9. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/LICENSE +0 -0
  10. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/README.md +0 -0
  11. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/src/Lapis.egg-info/PKG-INFO +0 -0
  12. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/src/Lapis.egg-info/SOURCES.txt +0 -0
  13. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/src/Lapis.egg-info/dependency_links.txt +0 -0
  14. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/src/Lapis.egg-info/top_level.txt +0 -0
  15. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/src/lapis/__init__.py +0 -0
  16. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/src/lapis/protocols/http1.py +0 -0
  17. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/src/lapis/protocols/websocket.py +0 -0
  18. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/src/lapis/server_types.py +0 -0
  19. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/basic_load_test/api/other/path.py +0 -0
  20. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/basic_load_test/api/path.py +0 -0
  21. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/basic_load_test/test.py +0 -0
  22. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/server_config_test/config.json +0 -0
  23. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/server_config_test/other/api/path.py +0 -0
  24. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/server_config_test/test.py +0 -0
  25. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/slugs_test/api/[slug1]/other/[slug2]/path.py +0 -0
  26. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/slugs_test/api/[slug1]/path.py +0 -0
  27. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/slugs_test/api/[slug2]/maybe/[slug3]/path.py +0 -0
  28. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/slugs_test/api/path.py +0 -0
  29. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/slugs_test/test.py +0 -0
  30. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/streamed_response_test/api/path.py +0 -0
  31. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/streamed_response_test/test.py +0 -0
  32. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/websocket_test/api/path.py +0 -0
  33. {lapis_api-0.2.1 → lapis_api-0.2.1.post1}/tests/websocket_test/test.py +0 -0
@@ -1,8 +1,9 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: lapis-api
3
- Version: 0.2.1
3
+ Version: 0.2.1.post1
4
4
  Summary: An organized way to create REST APIs
5
5
  Project-URL: Homepage, https://github.com/CQVan/Lapis
6
+ Project-URL: Wiki, https://github.com/CQVan/Lapis/wiki
6
7
  Project-URL: Issues, https://github.com/CQVan/Lapis/issues
7
8
  Author: Chandler Van
8
9
  License: MIT
@@ -7,7 +7,7 @@ packages = ["src/lapis"]
7
7
 
8
8
  [project]
9
9
  name = "lapis-api"
10
- version = "0.2.1"
10
+ version = "0.2.1.post1"
11
11
  description = "An organized way to create REST APIs"
12
12
  readme = "README.md"
13
13
  requires-python = ">=3.9"
@@ -27,4 +27,5 @@ classifiers = [
27
27
 
28
28
  [project.urls]
29
29
  Homepage = "https://github.com/CQVan/Lapis"
30
+ Wiki = "https://github.com/CQVan/Lapis/wiki"
30
31
  Issues = "https://github.com/CQVan/Lapis/issues"
@@ -37,6 +37,9 @@ class Lapis:
37
37
  __taken_endpoints: list[str] = []
38
38
  __protocols: list[type[Protocol]] = []
39
39
 
40
+ __slug_pattern = re.compile(r"\[[^\]]+\]")
41
+ __path_pattern = re.compile(r"^\/([a-zA-Z0-9._-]+)(\/[a-zA-Z0-9._-]+)*$")
42
+
40
43
  __running: bool = False
41
44
 
42
45
  def __init__(self, config: ServerConfig | None = None):
@@ -110,6 +113,19 @@ class Lapis:
110
113
  if p.is_dir() and p.name.startswith("[") and p.name.endswith("]")
111
114
  ]
112
115
 
116
+ def __validate_path(self, relative_path: pathlib.Path):
117
+ posix_rel = relative_path.as_posix()
118
+
119
+ slugs = self.__slug_pattern.findall(posix_rel)
120
+ if len(slugs) != len(set(slugs)):
121
+ raise BadAPIDirectory(f"Endpoint contains duplicate slugs: {posix_rel}")
122
+
123
+ clean_path_str = relative_path.with_suffix("").as_posix()
124
+ deslugged_path = "/" + self.__slug_pattern.sub("s", clean_path_str)
125
+
126
+ if not self.__path_pattern.match(deslugged_path):
127
+ raise BadAPIDirectory(f"Invalid characters or format in path: {posix_rel}")
128
+
113
129
  def _bake_paths(self):
114
130
  server_path = pathlib.Path(sys.argv[0]).resolve()
115
131
  # Simplified path joining
@@ -125,9 +141,6 @@ class Lapis:
125
141
  if not root.exists():
126
142
  raise BadAPIDirectory(f'api directory "{root}" does not exist')
127
143
 
128
- slug_pattern = re.compile(r"\[[^\]]+\]")
129
- path_pattern = re.compile(r"^\/([a-zA-Z0-9._-]+)(\/[a-zA-Z0-9._-]+)*$")
130
-
131
144
  self.__paths = {}
132
145
  endpoint_paths: dict[str, pathlib.Path] = {}
133
146
 
@@ -137,8 +150,12 @@ class Lapis:
137
150
 
138
151
  relative_path = path.relative_to(root)
139
152
 
140
- posix_rel = relative_path.as_posix()
141
- normalized_path = slug_pattern.sub("[slug]", posix_rel)
153
+ # Will raise exception if invalid path
154
+ self.__validate_path(relative_path)
155
+
156
+ normalized_path = self.__slug_pattern.sub(
157
+ "[slug]", relative_path.as_posix()
158
+ )
142
159
 
143
160
  if normalized_path in endpoint_paths:
144
161
  raise BadAPIDirectory(
@@ -147,14 +164,6 @@ class Lapis:
147
164
  f' - "{endpoint_paths[normalized_path]}"'
148
165
  )
149
166
 
150
- clean_path_str = relative_path.with_suffix("").as_posix()
151
- deslugged_path = "/" + slug_pattern.sub("s", clean_path_str)
152
-
153
- if not path_pattern.match(deslugged_path):
154
- raise BadAPIDirectory(
155
- f"Invalid characters or format in path: {posix_rel}"
156
- )
157
-
158
167
  endpoint_paths[normalized_path] = relative_path
159
168
 
160
169
  current_level = self.__paths
@@ -175,32 +184,47 @@ class Lapis:
175
184
  def __has_endpoint_path(
176
185
  self, base_url: str
177
186
  ) -> tuple[dict[str, any] | None, dict[str, str]]:
178
- # Digs through api cache map to find the correct endpoint directory
179
- slugs = {}
180
- path = pathlib.Path(f"{self.cfg.api_directory}{base_url}")
181
- parts: list[str] = path.relative_to(self.cfg.api_directory).parts
182
-
183
- leaf: dict = self.__paths
184
- for part in parts:
185
- if part in leaf:
186
- leaf = leaf[part]
187
- continue
187
+ # Convert the URL into parts, ignoring the leading slash
188
+ path_parts = pathlib.Path(base_url.lstrip("/")).parts
189
+
190
+ # Start the recursive search
191
+ return self._search_tree(self.__paths, path_parts, {})
192
+
193
+ def _search_tree(
194
+ self, current_level: dict, remaining_parts: tuple[str, ...], slugs: dict
195
+ ) -> tuple[dict | None, dict]:
188
196
 
189
- # checks if there are dynamic routes available
190
- dynamic_routes: list[str] = list(
191
- {key for key in leaf if key.startswith("[") and key.endswith("]")}
197
+ # Base case
198
+ if not remaining_parts:
199
+ return (current_level, slugs) if current_level else (None, {})
200
+
201
+ part = remaining_parts[0]
202
+ next_parts = remaining_parts[1:]
203
+
204
+ # Check for if no slugs
205
+ if part in current_level:
206
+ result, captured_slugs = self._search_tree(
207
+ current_level[part], next_parts, slugs
192
208
  )
209
+ if result is not None:
210
+ return result, captured_slugs
193
211
 
194
- if len(dynamic_routes) == 1:
195
- slugs[dynamic_routes[0].strip("[]")] = part
196
- leaf = leaf[dynamic_routes[0]]
197
- else:
198
- return (None, {})
212
+ # There are slugs
213
+ dynamic_keys = [
214
+ k for k in current_level if k.startswith("[") and k.endswith("]")
215
+ ]
199
216
 
200
- if len(leaf) == 0:
201
- return (None, {})
217
+ for key in dynamic_keys:
218
+ slug_name = key.strip("[]")
219
+ new_slugs = {**slugs, slug_name: part}
220
+
221
+ result, captured_slugs = self._search_tree(
222
+ current_level[key], next_parts, new_slugs
223
+ )
224
+ if result is not None:
225
+ return result, captured_slugs
202
226
 
203
- return (leaf, slugs)
227
+ return None, {}
204
228
 
205
229
  def _handle_request(self, client: socket.socket):
206
230
  data = client.recv(self.cfg.max_request_size)
File without changes
File without changes
File without changes
File without changes