api-foundry 0.8.12__tar.gz → 0.8.14__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.
- {api_foundry-0.8.12 → api_foundry-0.8.14}/PKG-INFO +1 -1
- {api_foundry-0.8.12 → api_foundry-0.8.14}/api_foundry/__init__.py +1 -1
- {api_foundry-0.8.12 → api_foundry-0.8.14}/api_foundry/iac/pulumi/api_foundry.py +78 -0
- {api_foundry-0.8.12 → api_foundry-0.8.14}/api_foundry/utils/model_factory.py +37 -6
- {api_foundry-0.8.12 → api_foundry-0.8.14}/.gitignore +0 -0
- {api_foundry-0.8.12 → api_foundry-0.8.14}/LICENSE +0 -0
- {api_foundry-0.8.12 → api_foundry-0.8.14}/api_foundry/iac/gateway_spec.py +0 -0
- {api_foundry-0.8.12 → api_foundry-0.8.14}/api_foundry/scripts/install_secret.py +0 -0
- {api_foundry-0.8.12 → api_foundry-0.8.14}/api_foundry/scripts/postgres_to_openapi.py +0 -0
- {api_foundry-0.8.12 → api_foundry-0.8.14}/api_foundry/utils/app_exception.py +0 -0
- {api_foundry-0.8.12 → api_foundry-0.8.14}/api_foundry/utils/logger.py +0 -0
- {api_foundry-0.8.12 → api_foundry-0.8.14}/api_foundry/utils/schema_validator.py +0 -0
- {api_foundry-0.8.12 → api_foundry-0.8.14}/pyproject.toml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: api-foundry
|
|
3
|
-
Version: 0.8.
|
|
3
|
+
Version: 0.8.14
|
|
4
4
|
Summary: The `api_foundry` project is a powerful tool designed to automate the deployment of REST APIs on AWS using Lambda services to access and interact with relational databases (RDBMS). This project leverages the OpenAPI specification to define and manage the APIs
|
|
5
5
|
Project-URL: Documentation, https://github.com/DanRepik/api-foundry
|
|
6
6
|
Project-URL: Source, https://github.com/DanRepik/api-foundry
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import os
|
|
2
|
+
import re
|
|
2
3
|
import json
|
|
3
4
|
import yaml
|
|
4
5
|
import boto3
|
|
@@ -6,6 +7,7 @@ from typing import Optional, Union
|
|
|
6
7
|
from pulumi import ComponentResource
|
|
7
8
|
|
|
8
9
|
import pulumi
|
|
10
|
+
import pulumi_aws as aws
|
|
9
11
|
import cloud_foundry
|
|
10
12
|
|
|
11
13
|
from api_foundry.iac.gateway_spec import APISpecEditor
|
|
@@ -113,6 +115,62 @@ def load_api_spec(api_spec: Union[str, list[str]]) -> dict:
|
|
|
113
115
|
return merged
|
|
114
116
|
|
|
115
117
|
|
|
118
|
+
# Requests per second (steady rate) and burst API Gateway lets through, across
|
|
119
|
+
# all callers, for every route of the stage unless the spec's x-af-throttle says
|
|
120
|
+
# otherwise. Far below the account default (10,000/5,000), so an open route
|
|
121
|
+
# can't run up the database bill.
|
|
122
|
+
DEFAULT_THROTTLE = {"rate": 100, "burst": 200}
|
|
123
|
+
_METHODS = ("get", "put", "post", "delete", "patch")
|
|
124
|
+
|
|
125
|
+
|
|
126
|
+
def _throttle(value, where: str) -> dict:
|
|
127
|
+
"""An x-af-throttle checked: {rate: positive number, burst: positive integer}."""
|
|
128
|
+
if not isinstance(value, dict) or set(value) != {"rate", "burst"}:
|
|
129
|
+
raise ValueError(
|
|
130
|
+
f"{where}: x-af-throttle needs rate and burst, e.g. {{rate: 10, burst: 20}}"
|
|
131
|
+
)
|
|
132
|
+
rate, burst = value["rate"], value["burst"]
|
|
133
|
+
if (
|
|
134
|
+
isinstance(rate, bool)
|
|
135
|
+
or not isinstance(rate, (int, float))
|
|
136
|
+
or rate <= 0
|
|
137
|
+
or isinstance(burst, bool)
|
|
138
|
+
or not isinstance(burst, int)
|
|
139
|
+
or burst <= 0
|
|
140
|
+
):
|
|
141
|
+
raise ValueError(
|
|
142
|
+
f"{where}: x-af-throttle rate must be a positive number and burst a "
|
|
143
|
+
f"positive integer, not {value!r}"
|
|
144
|
+
)
|
|
145
|
+
return {"rate": float(rate), "burst": burst}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def throttle_limits(spec: dict, path_prefix: Optional[str] = None) -> dict[str, dict]:
|
|
149
|
+
"""API Gateway method settings paths and their limits.
|
|
150
|
+
|
|
151
|
+
``*/*`` is the whole stage: the spec's top-level ``x-af-throttle``, else
|
|
152
|
+
DEFAULT_THROTTLE. Each path operation with its own ``x-af-throttle`` gets
|
|
153
|
+
``{resource path}/{METHOD}``: the resource path with ``path_prefix`` (as
|
|
154
|
+
cloud_foundry prefixes it) and without its leading "/", the form the AWS
|
|
155
|
+
provider's ``method_path`` takes. The provider stores and looks up the
|
|
156
|
+
setting under exactly this key, so a "~1"-encoded path deploys without
|
|
157
|
+
error and matches no resource.
|
|
158
|
+
"""
|
|
159
|
+
limits = {"*/*": _throttle(spec.get("x-af-throttle", DEFAULT_THROTTLE), "spec")}
|
|
160
|
+
for path, item in (spec.get("paths") or {}).items():
|
|
161
|
+
for method in _METHODS:
|
|
162
|
+
operation = item.get(method) if isinstance(item, dict) else None
|
|
163
|
+
if not isinstance(operation, dict) or "x-af-throttle" not in operation:
|
|
164
|
+
continue
|
|
165
|
+
resource = (
|
|
166
|
+
f"{path_prefix.rstrip('/')}/{path.lstrip('/')}" if path_prefix else path
|
|
167
|
+
)
|
|
168
|
+
limits[f"{resource.lstrip('/')}/{method.upper()}"] = _throttle(
|
|
169
|
+
operation["x-af-throttle"], f"{method.upper()} {path}"
|
|
170
|
+
)
|
|
171
|
+
return limits
|
|
172
|
+
|
|
173
|
+
|
|
116
174
|
class APIFoundry(ComponentResource):
|
|
117
175
|
api_spec_editor: APISpecEditor
|
|
118
176
|
|
|
@@ -273,6 +331,26 @@ class APIFoundry(ComponentResource):
|
|
|
273
331
|
|
|
274
332
|
self.domain = self.rest_api.domain
|
|
275
333
|
|
|
334
|
+
# Throttling, one method settings resource per limit. Each updates
|
|
335
|
+
# the same stage, and concurrent stage updates conflict, so each
|
|
336
|
+
# waits for the one before.
|
|
337
|
+
previous = None
|
|
338
|
+
for method_path, limit in throttle_limits(api_spec_dict, path_prefix).items():
|
|
339
|
+
slug = re.sub(r"[^A-Za-z0-9]+", "-", method_path).strip("-") or "stage"
|
|
340
|
+
previous = aws.apigateway.MethodSettings(
|
|
341
|
+
f"{name}-limit-{slug}",
|
|
342
|
+
rest_api=self.rest_api.rest_api_id,
|
|
343
|
+
stage_name=self.rest_api.stage_name,
|
|
344
|
+
method_path=method_path,
|
|
345
|
+
settings=aws.apigateway.MethodSettingsSettingsArgs(
|
|
346
|
+
throttling_rate_limit=limit["rate"],
|
|
347
|
+
throttling_burst_limit=limit["burst"],
|
|
348
|
+
),
|
|
349
|
+
opts=pulumi.ResourceOptions(
|
|
350
|
+
parent=self, depends_on=[previous] if previous else None
|
|
351
|
+
),
|
|
352
|
+
)
|
|
353
|
+
|
|
276
354
|
self.register_outputs({f"{name}_domain": self.domain})
|
|
277
355
|
|
|
278
356
|
def integrations(self) -> list[dict]:
|
|
@@ -36,12 +36,26 @@ SUPPORTED_TYPES = {
|
|
|
36
36
|
}
|
|
37
37
|
|
|
38
38
|
|
|
39
|
-
|
|
40
39
|
# A Postgres array column type: a type name followed by "[]" ("text[]",
|
|
41
40
|
# "integer[]", "character varying[]"). The query engine interpolates the
|
|
42
41
|
# element type into SQL casts, so nothing else is accepted.
|
|
43
42
|
ARRAY_COLUMN_TYPE = re.compile(r"^\s*[A-Za-z][A-Za-z0-9_ ]*?\s*\[\]\s*$")
|
|
44
43
|
|
|
44
|
+
# Most rows one read of a schema object returns, unless the spec's (or the
|
|
45
|
+
# schema object's) x-af-limit says otherwise. The query engine caps __limit
|
|
46
|
+
# at it and applies it when __limit is left out.
|
|
47
|
+
DEFAULT_ROW_LIMIT = 1000
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
def row_limit(value: Any, where: str) -> int:
|
|
51
|
+
"""An x-af-limit checked: a positive integer."""
|
|
52
|
+
if isinstance(value, bool) or not isinstance(value, int) or value <= 0:
|
|
53
|
+
raise ApplicationException(
|
|
54
|
+
500, f"{where}: x-af-limit must be a positive integer, not {value!r}"
|
|
55
|
+
)
|
|
56
|
+
return value
|
|
57
|
+
|
|
58
|
+
|
|
45
59
|
class OpenAPIElement:
|
|
46
60
|
"""
|
|
47
61
|
Base class for OpenAPI elements like schema properties and
|
|
@@ -158,9 +172,14 @@ class SchemaObjectProperty(OpenAPIElement):
|
|
|
158
172
|
"type is a type name followed by [], such as text[]",
|
|
159
173
|
)
|
|
160
174
|
items = prop.get("items") or {}
|
|
161
|
-
if
|
|
162
|
-
"
|
|
163
|
-
"
|
|
175
|
+
if (
|
|
176
|
+
prop.get("type") != "array"
|
|
177
|
+
or "$ref" in items
|
|
178
|
+
or items.get("type")
|
|
179
|
+
in (
|
|
180
|
+
"object",
|
|
181
|
+
"array",
|
|
182
|
+
)
|
|
164
183
|
):
|
|
165
184
|
raise ApplicationException(
|
|
166
185
|
500,
|
|
@@ -391,9 +410,18 @@ class SchemaObjectAssociation(OpenAPIElement):
|
|
|
391
410
|
class SchemaObject(OpenAPIElement):
|
|
392
411
|
"""Represents a schema object in the OpenAPI specification."""
|
|
393
412
|
|
|
394
|
-
def __init__(
|
|
413
|
+
def __init__(
|
|
414
|
+
self,
|
|
415
|
+
api_name: str,
|
|
416
|
+
schema_object: Dict[str, Any],
|
|
417
|
+
default_limit: int = DEFAULT_ROW_LIMIT,
|
|
418
|
+
):
|
|
395
419
|
super().__init__()
|
|
396
420
|
self.api_name = api_name
|
|
421
|
+
self.limit = row_limit(
|
|
422
|
+
schema_object.get("x-af-limit", default_limit),
|
|
423
|
+
f"Schema object '{api_name}'",
|
|
424
|
+
)
|
|
397
425
|
|
|
398
426
|
# Get database name (should always exist as caller filters)
|
|
399
427
|
self.database = schema_object.get("x-af-database", "").lower()
|
|
@@ -827,10 +855,13 @@ class ModelFactory:
|
|
|
827
855
|
(like DTOs, enums, or reference-only types) are silently skipped.
|
|
828
856
|
"""
|
|
829
857
|
schema_objects = {}
|
|
858
|
+
default_limit = row_limit(
|
|
859
|
+
self.spec.get("x-af-limit", DEFAULT_ROW_LIMIT), "spec"
|
|
860
|
+
)
|
|
830
861
|
schemas = self.spec.get("components", {}).get("schemas", {})
|
|
831
862
|
for name, schema in schemas.items():
|
|
832
863
|
if "x-af-database" in schema:
|
|
833
|
-
schema_objects[name] = SchemaObject(name, schema)
|
|
864
|
+
schema_objects[name] = SchemaObject(name, schema, default_limit)
|
|
834
865
|
else:
|
|
835
866
|
log.debug(f"Skipping schema '{name}' - no x-af-database attribute")
|
|
836
867
|
return schema_objects
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|