api-foundry 0.8.12__tar.gz → 0.8.13__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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.5
2
2
  Name: api-foundry
3
- Version: 0.8.12
3
+ Version: 0.8.13
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,5 +1,5 @@
1
1
  # __init__.py
2
- __version__ = "0.8.12"
2
+ __version__ = "0.8.13"
3
3
 
4
4
  from api_foundry.iac.pulumi.api_foundry import APIFoundry
5
5
  from cloud_foundry import logger
@@ -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,60 @@ 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``,
154
+ as cloud_foundry prefixes it) written with "~1" for "/", as the stage's
155
+ method settings name it.
156
+ """
157
+ limits = {"*/*": _throttle(spec.get("x-af-throttle", DEFAULT_THROTTLE), "spec")}
158
+ for path, item in (spec.get("paths") or {}).items():
159
+ for method in _METHODS:
160
+ operation = item.get(method) if isinstance(item, dict) else None
161
+ if not isinstance(operation, dict) or "x-af-throttle" not in operation:
162
+ continue
163
+ resource = (
164
+ f"{path_prefix.rstrip('/')}/{path.lstrip('/')}" if path_prefix else path
165
+ )
166
+ limits[f"{resource.replace('/', '~1')}/{method.upper()}"] = _throttle(
167
+ operation["x-af-throttle"], f"{method.upper()} {path}"
168
+ )
169
+ return limits
170
+
171
+
116
172
  class APIFoundry(ComponentResource):
117
173
  api_spec_editor: APISpecEditor
118
174
 
@@ -273,6 +329,26 @@ class APIFoundry(ComponentResource):
273
329
 
274
330
  self.domain = self.rest_api.domain
275
331
 
332
+ # Throttling, one method settings resource per limit. Each updates
333
+ # the same stage, and concurrent stage updates conflict, so each
334
+ # waits for the one before.
335
+ previous = None
336
+ for method_path, limit in throttle_limits(api_spec_dict, path_prefix).items():
337
+ slug = re.sub(r"[^A-Za-z0-9]+", "-", method_path).strip("-") or "stage"
338
+ previous = aws.apigateway.MethodSettings(
339
+ f"{name}-limit-{slug}",
340
+ rest_api=self.rest_api.rest_api_id,
341
+ stage_name=self.rest_api.stage_name,
342
+ method_path=method_path,
343
+ settings=aws.apigateway.MethodSettingsSettingsArgs(
344
+ throttling_rate_limit=limit["rate"],
345
+ throttling_burst_limit=limit["burst"],
346
+ ),
347
+ opts=pulumi.ResourceOptions(
348
+ parent=self, depends_on=[previous] if previous else None
349
+ ),
350
+ )
351
+
276
352
  self.register_outputs({f"{name}_domain": self.domain})
277
353
 
278
354
  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 prop.get("type") != "array" or "$ref" in items or items.get("type") in (
162
- "object",
163
- "array",
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__(self, api_name: str, schema_object: Dict[str, Any]):
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