costvine-api-utils 0.1.77__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.
@@ -0,0 +1,12 @@
1
+ Metadata-Version: 2.4
2
+ Name: costvine-api-utils
3
+ Version: 0.1.77
4
+ Summary: Some simple utilities for handling inbound API calls in Python cloud functions
5
+ Author: John D. Underhill
6
+ Author-email: junderhill@qisproject.com
7
+ Requires-Python: >=3.12,<4.0
8
+ Classifier: Programming Language :: Python :: 3
9
+ Classifier: Programming Language :: Python :: 3.12
10
+ Classifier: Programming Language :: Python :: 3.13
11
+ Classifier: Programming Language :: Python :: 3.14
12
+ Requires-Dist: firebase-functions
@@ -0,0 +1,3 @@
1
+ """Simple API utilities."""
2
+
3
+ from .main import *
@@ -0,0 +1,61 @@
1
+ import os
2
+ import copy
3
+ from collections.abc import Iterable
4
+ from flask import Request
5
+
6
+
7
+ def is_health_check(req: Request) -> bool:
8
+ """Returns True if the specified path is a health check request."""
9
+
10
+ return "__/health" in req.path
11
+
12
+
13
+ def patch_request(original_request: Request, ssl_domains: Iterable[str]) -> Request:
14
+ """
15
+ This function patches the request object so we can use it with the OpenAPI validator: (1) If the name of the called cloud
16
+ function is not the first component of the path, patch it in; (2) When deployed, SSL terminates at the load balancer, and
17
+ the scheme (protocol) is always reported as HTTP, even though it was originally HTTPS, so we fix that as well. The
18
+ `ssl_domains` parameter specifies which host domains should have their scheme patched to HTTPS.
19
+ """
20
+
21
+ req = copy.copy(original_request)
22
+ print("Original request path:", req.path)
23
+
24
+ if not req.path.startswith("/"):
25
+ print("Warning: Expected the request path to start with a slash. Fixing that.")
26
+ req.path = "/" + req.path
27
+
28
+ service_name = os.environ.get("K_SERVICE", "")
29
+ print("Service name:", service_name)
30
+ if not service_name:
31
+ print(
32
+ "Warning: Expected the K_SERVICE environment variable to be set. Patching in the service name (if required) will not take place."
33
+ )
34
+ elif not req.path.startswith("/" + service_name):
35
+ req.path = "/" + service_name + req.path
36
+
37
+ if req.path.endswith("/"):
38
+ print("Warning: Expected the request path NOT to end with a slash. Fixing that.")
39
+ req.path = req.path[:-1]
40
+
41
+ print("Patched request path:", req.path)
42
+
43
+ url = req.host_url.lower()
44
+ if any(domain in url for domain in ssl_domains):
45
+ # `host_url` is a `cached_property`; write through `__dict__` to update the
46
+ # cached value without tripping type checkers that disallow assignment.
47
+ req.__dict__["host_url"] = req.host_url.replace("http://", "https://", 1)
48
+ req.scheme = "https"
49
+ print("Patched request host_url:", req.host_url)
50
+
51
+ return req
52
+
53
+
54
+ def get_api_function_name(req: Request) -> str:
55
+ """Return the API function name or an empty string if none was provided."""
56
+
57
+ path_components = patch_request(req, ()).path.split("/")
58
+
59
+ if len(path_components) < 3:
60
+ return ""
61
+ return path_components[2]
@@ -0,0 +1,28 @@
1
+ [tool.poetry]
2
+ name = "costvine-api-utils"
3
+ version = "0.1.77"
4
+ description = "Some simple utilities for handling inbound API calls in Python cloud functions"
5
+ authors = ["John D. Underhill <junderhill@qisproject.com>"]
6
+
7
+ [tool.costvine.scripts]
8
+ pretty = "pretty"
9
+ format = "format"
10
+ lint = "lint"
11
+ types = "types"
12
+ tests = "tests"
13
+ check = "check"
14
+ "distribute" = "distribute-pypi"
15
+
16
+ [tool.poetry.dependencies]
17
+ python = "^3.12"
18
+ firebase-functions = "*"
19
+
20
+ [tool.poetry.group.dev.dependencies]
21
+ pip = "*"
22
+ mypy = "*"
23
+ pylint = "*"
24
+ black = "*"
25
+
26
+ [build-system]
27
+ requires = ["poetry-core"]
28
+ build-backend = "poetry.core.masonry.api"