api-foundry 0.1.13__tar.gz → 0.1.15__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 (73) hide show
  1. {api_foundry-0.1.13 → api_foundry-0.1.15}/PKG-INFO +1 -1
  2. {api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging → api_foundry-0.1.15}/api_foundry/scripts/postgres_to_openapi.py +2 -3
  3. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/utils/model_factory.py +48 -14
  4. {api_foundry-0.1.13 → api_foundry-0.1.15}/pyproject.toml +1 -2
  5. api_foundry-0.1.13/api_foundry/scripts/postgres_to_openapi.py +0 -216
  6. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/__init__.py +0 -0
  7. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/adapters/adapter.py +0 -67
  8. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/adapters/case_change_adapter.py +0 -79
  9. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/adapters/gateway_adapter.py +0 -112
  10. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/cloudprints/archive_builder.py +0 -6
  11. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/cloudprints/hash_comparator.py +0 -90
  12. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/cloudprints/pulumi/lambda_.py +0 -106
  13. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/cloudprints/pulumi/log_group.pv +0 -18
  14. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/cloudprints/pulumi/rest_api.py +0 -16
  15. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/cloudprints/python_archive_builder.py +0 -207
  16. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/connectors/connection.py +0 -32
  17. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/connectors/connection_factory.py +0 -110
  18. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/connectors/oracle_connector.py +0 -29
  19. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/connectors/postgres_connection.py +0 -105
  20. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/dao/dao.py +0 -23
  21. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/dao/operation_dao.py +0 -141
  22. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/dao/sql_custom_query_handler.py +0 -66
  23. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/dao/sql_delete_query_handler.py +0 -36
  24. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/dao/sql_insert_query_handler.py +0 -102
  25. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/dao/sql_query_handler.py +0 -378
  26. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/dao/sql_select_query_handler.py +0 -303
  27. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/dao/sql_subselect_query_handler.py +0 -63
  28. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/dao/sql_update_query_handler.py +0 -59
  29. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/iac/gateway_spec.py +0 -632
  30. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/iac/handler.py +0 -42
  31. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/iac/pulumi/api_foundry.py +0 -98
  32. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/operation.py +0 -15
  33. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/services/service.py +0 -60
  34. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/services/transactional_service.py +0 -35
  35. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/utils/app_exception.py +0 -22
  36. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/utils/logger.py +0 -60
  37. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/utils/model_factory.py +0 -448
  38. api_foundry-0.1.13/examples/chinook-postgres-pulumi/temp/chinook_postgres-archive-builder-lambda/staging/api_foundry/utils/spec_handler.py +0 -74
  39. {api_foundry-0.1.13 → api_foundry-0.1.15}/.gitignore +0 -0
  40. {api_foundry-0.1.13 → api_foundry-0.1.15}/LICENSE +0 -0
  41. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/__init__.py +0 -0
  42. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/adapters/adapter.py +0 -0
  43. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/adapters/case_change_adapter.py +0 -0
  44. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/adapters/gateway_adapter.py +0 -0
  45. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/cloudprints/archive_builder.py +0 -0
  46. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/cloudprints/hash_comparator.py +0 -0
  47. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/cloudprints/pulumi/lambda_.py +0 -0
  48. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/cloudprints/pulumi/log_group.pv +0 -0
  49. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/cloudprints/pulumi/rest_api.py +0 -0
  50. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/cloudprints/python_archive_builder.py +0 -0
  51. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/connectors/connection.py +0 -0
  52. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/connectors/connection_factory.py +0 -0
  53. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/connectors/oracle_connector.py +0 -0
  54. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/connectors/postgres_connection.py +0 -0
  55. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/dao/dao.py +0 -0
  56. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/dao/operation_dao.py +0 -0
  57. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/dao/sql_custom_query_handler.py +0 -0
  58. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/dao/sql_delete_query_handler.py +0 -0
  59. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/dao/sql_insert_query_handler.py +0 -0
  60. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/dao/sql_query_handler.py +0 -0
  61. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/dao/sql_select_query_handler.py +0 -0
  62. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/dao/sql_subselect_query_handler.py +0 -0
  63. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/dao/sql_update_query_handler.py +0 -0
  64. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/iac/gateway_spec.py +0 -0
  65. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/iac/handler.py +0 -0
  66. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/iac/pulumi/api_foundry.py +0 -0
  67. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/operation.py +0 -0
  68. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/scripts/install_secret.py +0 -0
  69. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/services/service.py +0 -0
  70. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/services/transactional_service.py +0 -0
  71. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/utils/app_exception.py +0 -0
  72. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/utils/logger.py +0 -0
  73. {api_foundry-0.1.13 → api_foundry-0.1.15}/api_foundry/utils/spec_handler.py +0 -0
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.3
2
2
  Name: api-foundry
3
- Version: 0.1.13
3
+ Version: 0.1.15
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
@@ -69,7 +69,7 @@ class PostgresSchemaToOpenAPI:
69
69
  primary_keys = {}
70
70
  for pk in pks:
71
71
  primary_keys[pk["column_name"]] = (
72
- "auto" if pk["is_identity"] == "YES" else True
72
+ "auto" if pk["is_identity"] == "YES" else "manual"
73
73
  )
74
74
 
75
75
  return primary_keys
@@ -120,8 +120,7 @@ class PostgresSchemaToOpenAPI:
120
120
  if primary_keys[column_name] == "auto":
121
121
  column_info[
122
122
  "description"
123
- ] = f"Unique identifier for the {table}."
124
- column_info["example"] = 1
123
+ ] = f"Unique identifier for the {table}. Read only"
125
124
 
126
125
  if (
127
126
  column_info["type"] == "string"
@@ -1,6 +1,6 @@
1
1
  import re
2
2
  import yaml
3
- from typing import Any, Dict, Optional, List, Union
3
+ from typing import Any, Dict, Optional, List, Union, cast
4
4
  from datetime import datetime
5
5
  from api_foundry.utils.app_exception import ApplicationException
6
6
  from api_foundry.utils.spec_handler import SpecificationHandler
@@ -111,6 +111,8 @@ class SchemaObjectKey(SchemaObjectProperty):
111
111
  spec: Dict[str, Any],
112
112
  ):
113
113
  super().__init__(operation_id, name, properties, spec)
114
+ log.info(f"properties: {properties}")
115
+ log.info(f"key_type: {self.get('x-af-primary-key')}")
114
116
  self.key_type = self.get("x-af-primary-key", default="auto")
115
117
  if self.key_type not in ["manual", "uuid", "auto", "sequence"]:
116
118
  raise ApplicationException(
@@ -146,9 +148,15 @@ class SchemaObjectAssociation(OpenAPIElement):
146
148
  @property
147
149
  def child_property(self) -> "SchemaObjectProperty":
148
150
  child_property = self.get("x-af-child-property", None)
149
- if not child_property:
150
- return self.child_schema_object.primary_key
151
- return self.child_schema_object.get_property(child_property)
151
+
152
+ return cast(
153
+ SchemaObjectProperty,
154
+ (
155
+ self.child_schema_object.get_property(child_property)
156
+ if child_property
157
+ else self.child_schema_object.primary_key
158
+ ),
159
+ )
152
160
 
153
161
  @property
154
162
  def parent_property(self) -> "SchemaObjectProperty":
@@ -163,9 +171,14 @@ class SchemaObjectAssociation(OpenAPIElement):
163
171
  ),
164
172
  )
165
173
  parent = self.get("x-af-parent-property")
166
- if parent:
167
- return parent_schema_object.get_property(parent)
168
- return parent_schema_object.primary_key
174
+ return cast(
175
+ SchemaObjectProperty,
176
+ (
177
+ parent_schema_object.get_property(parent)
178
+ if parent
179
+ else parent_schema_object.primary_key
180
+ ),
181
+ )
169
182
 
170
183
  @property
171
184
  def child_schema_object(self) -> "SchemaObject":
@@ -190,29 +203,40 @@ class SchemaObject(OpenAPIElement):
190
203
  self, operation_id: str, schema_object: Dict[str, Any], spec: Dict[str, Any]
191
204
  ):
192
205
  super().__init__(schema_object, spec)
206
+ log.info(f"schema_object init: {schema_object}")
193
207
  self.operation_id = operation_id
194
208
  self.schema_object = schema_object
195
209
  database = schema_object.get("x-af-database")
196
210
  if database:
197
211
  self.database = database.lower()
198
- self.primary_key = None
199
212
 
200
213
  @property
201
214
  def properties(self) -> Dict[str, SchemaObjectProperty]:
215
+ log.info("properties")
202
216
  if not hasattr(self, "_properties"):
203
217
  self._resolve_properties()
204
218
  return self._properties
205
219
 
206
220
  @property
207
221
  def relations(self) -> Dict[str, SchemaObjectAssociation]:
222
+ log.info("relations")
208
223
  if not hasattr(self, "_relations"):
209
224
  self._resolve_properties()
210
225
  return self._relations
211
226
 
227
+ @property
228
+ def primary_key(self) -> SchemaObjectKey:
229
+ log.info("primary_key")
230
+ if not hasattr(self, "_primary_key"):
231
+ self._resolve_properties()
232
+ return self._primary_key
233
+
212
234
  def _resolve_properties(self):
235
+ log.info("resolve_properties")
213
236
  self._properties = dict()
214
237
  self._relations = dict()
215
- for property_name, prop in self.get("properties").items():
238
+ for property_name, prop in cast(dict, self.get("properties")).items():
239
+ log.info(f"name: {property_name}, prop: {prop}")
216
240
  assert prop is not None, (
217
241
  f"Property is none operation_id: {self.operation_id}, "
218
242
  + f"property: {property_name}"
@@ -222,6 +246,7 @@ class SchemaObject(OpenAPIElement):
222
246
  self._properties[property_name] = object_property
223
247
 
224
248
  def _resolve_property(self, property_name: str, prop: Dict[str, Any]):
249
+ log.info("resolve property")
225
250
  type = self.get("type", prop, None)
226
251
 
227
252
  if not type:
@@ -231,11 +256,16 @@ class SchemaObject(OpenAPIElement):
231
256
  )
232
257
 
233
258
  if type in ["object", "array"]:
259
+ log.info(f"relations: {property_name}")
234
260
  self._relations[property_name] = SchemaObjectAssociation(
235
261
  self.operation_id,
236
262
  property_name,
237
263
  {
238
- **(prop if type == "object" else self.get("items", prop)),
264
+ **(
265
+ prop
266
+ if type == "object"
267
+ else cast(dict, self.get("items", prop))
268
+ ),
239
269
  "type": type,
240
270
  },
241
271
  self.spec,
@@ -245,7 +275,7 @@ class SchemaObject(OpenAPIElement):
245
275
  self.operation_id, property_name, prop, self.spec
246
276
  )
247
277
  if object_property.is_primary_key:
248
- self.primary_key = SchemaObjectKey(
278
+ self._primary_key = SchemaObjectKey(
249
279
  self.operation_id, property_name, prop, self.spec
250
280
  )
251
281
  return object_property
@@ -375,7 +405,7 @@ class PathOperation(OpenAPIElement):
375
405
  return properties
376
406
 
377
407
  def _get_schema_properties(
378
- self, schema: Dict[str, Any], param_name: str = None
408
+ self, schema: Dict[str, Any], param_name: Optional[str] = None
379
409
  ) -> Dict[str, SchemaObjectProperty]:
380
410
  properties = {}
381
411
  schema_ref = schema.get("$ref")
@@ -387,14 +417,16 @@ class PathOperation(OpenAPIElement):
387
417
  self.path, prop_name, prop_spec, self.spec
388
418
  )
389
419
  elif param_name:
390
- properties[param_name] = SchemaObjectProperty(self.path, param_name, schema)
420
+ properties[param_name] = SchemaObjectProperty(
421
+ self.path, param_name, schema, self.spec
422
+ )
391
423
  return properties
392
424
 
393
425
 
394
426
  class ModelFactory:
395
427
  spec: dict
396
428
  schema_objects: Dict[str, SchemaObject] = {}
397
- path_operations: Dict[str, PathOperation] = []
429
+ path_operations: Dict[str, PathOperation] = {}
398
430
 
399
431
  @classmethod
400
432
  def load_yaml(cls, api_spec_path: str):
@@ -405,12 +437,14 @@ class ModelFactory:
405
437
 
406
438
  @classmethod
407
439
  def set_spec(cls, spec: dict):
440
+ log.info("set_spec")
408
441
  cls.spec = spec
409
442
  cls.schema_objects = {}
410
443
 
411
444
  schemas = cls.spec.get("components", {}).get("schemas", {})
412
445
  for name, schema in schemas.items():
413
446
  if "x-af-database" in schema:
447
+ log.info(f"schema_object: {name}")
414
448
  cls.schema_objects[name] = SchemaObject(name, schema, cls.spec)
415
449
 
416
450
  paths = cls.spec.get("paths", {})
@@ -4,7 +4,7 @@ build-backend = "hatchling.build"
4
4
 
5
5
  [project]
6
6
  name = "api-foundry"
7
- version = "0.1.13"
7
+ version = "0.1.15"
8
8
  authors = [
9
9
  { name="Daniel Repik", email="danrepik@icloud.com" },
10
10
  ]
@@ -38,7 +38,6 @@ include = ["api_foundry"]
38
38
  [project.scripts]
39
39
  postgres_to_openapi = "api_foundry.scripts.postgres_to_openapi:main"
40
40
  install_secret = "api_foundry.scripts.install_secret:main"
41
- install_secret_2 = "scripts.install_secret:main"
42
41
 
43
42
  [tool.pytest.ini_options]
44
43
  markers = [
@@ -1,216 +0,0 @@
1
- import psycopg2
2
- import psycopg2.extras
3
- import yaml
4
- import argparse
5
-
6
-
7
- class PostgresSchemaToOpenAPI:
8
- def __init__(self, host, database, user, password, schema="public"):
9
- self.connection = psycopg2.connect(
10
- host=host, database=database, user=user, password=password
11
- )
12
- self.schema = schema
13
- self.database = database
14
-
15
- def get_tables(self):
16
- query = """
17
- SELECT table_name
18
- FROM information_schema.tables
19
- WHERE table_schema = %s
20
- """
21
- with self.connection.cursor() as cursor:
22
- cursor.execute(query, (self.schema,))
23
- tables = cursor.fetchall()
24
- return [table[0] for table in tables]
25
-
26
- def get_columns(self, table_name):
27
- query = """
28
- SELECT column_name, data_type, is_nullable, character_maximum_length,
29
- (SELECT ccu.table_name
30
- FROM information_schema.table_constraints AS tc
31
- JOIN information_schema.constraint_column_usage AS ccu
32
- ON ccu.constraint_name = tc.constraint_name
33
- WHERE tc.constraint_type = 'FOREIGN KEY'
34
- AND tc.table_name = %s
35
- AND tc.table_schema = %s
36
- AND ccu.column_name = c.column_name
37
- LIMIT 1) AS foreign_table
38
- FROM information_schema.columns AS c
39
- WHERE table_schema = %s AND table_name = %s
40
- """
41
- with self.connection.cursor(
42
- cursor_factory=psycopg2.extras.DictCursor
43
- ) as cursor:
44
- cursor.execute(query, (table_name, self.schema, self.schema, table_name))
45
- columns = cursor.fetchall()
46
- return columns
47
-
48
- def get_primary_keys(self, table_name):
49
- query = """
50
- SELECT kcu.column_name, c.is_identity
51
- FROM information_schema.table_constraints tc
52
- JOIN information_schema.key_column_usage kcu
53
- ON tc.constraint_name = kcu.constraint_name
54
- AND tc.table_schema = kcu.table_schema
55
- JOIN information_schema.columns c
56
- ON c.table_schema = tc.table_schema
57
- AND c.table_name = tc.table_name
58
- AND c.column_name = kcu.column_name
59
- WHERE tc.constraint_type = 'PRIMARY KEY'
60
- AND tc.table_name = %s
61
- AND tc.table_schema = %s
62
- """
63
- with self.connection.cursor(
64
- cursor_factory=psycopg2.extras.DictCursor
65
- ) as cursor:
66
- cursor.execute(query, (table_name, self.schema))
67
- pks = cursor.fetchall()
68
-
69
- primary_keys = {}
70
- for pk in pks:
71
- primary_keys[pk["column_name"]] = (
72
- "auto" if pk["is_identity"] == "YES" else True
73
- )
74
-
75
- return primary_keys
76
-
77
- def map_data_type(self, data_type):
78
- type_mapping = {
79
- "character varying": {"type": "string"},
80
- "varchar": {"type": "string"},
81
- "character": {"type": "string"},
82
- "char": {"type": "string"},
83
- "text": {"type": "string"},
84
- "integer": {"type": "integer"},
85
- "bigint": {"type": "integer"},
86
- "smallint": {"type": "integer"},
87
- "numeric": {"type": "number"},
88
- "real": {"type": "number"},
89
- "double precision": {"type": "number"},
90
- "boolean": {"type": "boolean"},
91
- "date": {"type": "string", "format": "date"},
92
- "timestamp without time zone": {"type": "string", "format": "date-time"},
93
- "timestamp with time zone": {"type": "string", "format": "date-time"},
94
- "uuid": {"type": "string", "format": "uuid"},
95
- }
96
- return type_mapping.get(data_type, {"type": "string"})
97
-
98
- def generate_openapi_schema(self):
99
- openapi_schema = {
100
- "openapi": "3.0.0",
101
- "info": {"title": "Generated API", "version": "1.0.0"},
102
- "components": {"schemas": {}},
103
- }
104
-
105
- tables = self.get_tables()
106
- foreign_key_map = {} # To track foreign key relationships
107
-
108
- for table in tables:
109
- columns = self.get_columns(table)
110
- primary_keys = self.get_primary_keys(table)
111
- properties = {}
112
- required = []
113
-
114
- for column in columns:
115
- column_name = column["column_name"]
116
- column_info = self.map_data_type(column["data_type"])
117
-
118
- if column_name in primary_keys:
119
- column_info["x-af-primary-key"] = primary_keys[column_name]
120
- if primary_keys[column_name] == "auto":
121
- column_info[
122
- "description"
123
- ] = f"Unique identifier for the {table}."
124
- column_info["example"] = 1
125
-
126
- if (
127
- column_info["type"] == "string"
128
- and "character_maximum_length" in column
129
- and column["character_maximum_length"]
130
- ):
131
- column_info["maxLength"] = column["character_maximum_length"]
132
-
133
- if column["is_nullable"] == "NO":
134
- required.append(column_name)
135
-
136
- properties[column_name] = column_info
137
-
138
- if column["foreign_table"]:
139
- foreign_table = column["foreign_table"]
140
- properties[foreign_table] = {
141
- "$ref": f"#/components/schemas/{foreign_table}",
142
- "x-af-parent-property": column_name,
143
- "description": f"{foreign_table.capitalize()} associated with the {table}.",
144
- }
145
-
146
- # Track the foreign key relationship
147
- if foreign_table not in foreign_key_map:
148
- foreign_key_map[foreign_table] = []
149
- foreign_key_map[foreign_table].append((table, column_name))
150
-
151
- schema_object = {
152
- "type": "object",
153
- "properties": properties,
154
- "required": required,
155
- "x-af-database": self.database,
156
- }
157
- openapi_schema["components"]["schemas"][table] = schema_object
158
-
159
- # Add array properties for foreign key relationships
160
- for parent_table, foreign_keys in foreign_key_map.items():
161
- if parent_table in openapi_schema["components"]["schemas"]:
162
- for child_table, foreign_key in foreign_keys:
163
- openapi_schema["components"]["schemas"][parent_table]["properties"][
164
- f"{child_table}_items"
165
- ] = {
166
- "type": "array",
167
- "items": {
168
- "$ref": f"#/components/schemas/{child_table}",
169
- "x-af-child-property": foreign_key,
170
- },
171
- "description": f"List of {child_table} items associated with this {parent_table}.",
172
- }
173
-
174
- return openapi_schema
175
-
176
- def save_openapi_schema(self, file_path):
177
- openapi_schema = self.generate_openapi_schema()
178
- with open(file_path, "w") as file:
179
- yaml.dump(openapi_schema, file, sort_keys=False)
180
-
181
- def close_connection(self):
182
- self.connection.close()
183
-
184
-
185
- def main():
186
- parser = argparse.ArgumentParser(
187
- description="Generate OpenAPI schema from PostgreSQL database schema."
188
- )
189
- parser.add_argument("--host", required=True, help="PostgreSQL database host")
190
- parser.add_argument("--database", required=True, help="PostgreSQL database name")
191
- parser.add_argument("--user", required=True, help="PostgreSQL database user")
192
- parser.add_argument(
193
- "--password", required=True, help="PostgreSQL database password"
194
- )
195
- parser.add_argument(
196
- "--schema", default="public", help="PostgreSQL schema (default: public)"
197
- )
198
- parser.add_argument(
199
- "--output", required=True, help="Output file path for the OpenAPI schema"
200
- )
201
-
202
- args = parser.parse_args()
203
-
204
- converter = PostgresSchemaToOpenAPI(
205
- host=args.host,
206
- database=args.database,
207
- user=args.user,
208
- password=args.password,
209
- schema=args.schema,
210
- )
211
- converter.save_openapi_schema(args.output)
212
- converter.close_connection()
213
-
214
-
215
- if __name__ == "__main__":
216
- main()
@@ -1,67 +0,0 @@
1
- import abc
2
- from typing import Optional
3
-
4
- from api_foundry.operation import Operation
5
- from api_foundry.services.transactional_service import TransactionalService
6
- from api_foundry.utils.logger import logger
7
- from api_foundry.services.service import Service
8
-
9
- log = logger(__name__)
10
-
11
-
12
- class Adapter(metaclass=abc.ABCMeta):
13
- service: Service
14
-
15
- @classmethod
16
- def __subclasshook__(cls, __subclass: type) -> bool:
17
- return (
18
- hasattr(__subclass, "marshal")
19
- and callable(__subclass.marshal)
20
- and hasattr(__subclass, "umarshal")
21
- and callable(__subclass.unmarshal)
22
- )
23
-
24
- def __init__(self, service: Optional[Service] = None) -> None:
25
- self.service = service if service is not None else TransactionalService()
26
-
27
- def unmarshal(self, event) -> Operation:
28
- """
29
- Unmarshal the event in a tuple for processing
30
-
31
- Parameters:
32
- - event (dict): Lambda event object.
33
-
34
- Returns:
35
- - Operation containing the entity, action, and parameters
36
- """
37
- raise NotImplementedError
38
-
39
- def marshal(self, result: list[dict]):
40
- """
41
- Marshal the result into a event response
42
-
43
- Parameters:
44
- - result (list): the data set to return in the response
45
-
46
- Returns:
47
- - the event response
48
- """
49
- return result
50
-
51
- def process_event(self, event):
52
- """
53
- Process Lambda event using a domain function.
54
-
55
- Parameters:
56
- - service_function (callable): The service function to be executed.
57
- - event (dict): Lambda event object.
58
-
59
- Returns:
60
- - any: Result of the domain function.
61
- """
62
- operation = self.unmarshal(event)
63
-
64
- result = self.service.execute(operation)
65
- log.debug(f"adapter result: {result}")
66
-
67
- return self.marshal(result)
@@ -1,79 +0,0 @@
1
- from humps import camelize, decamelize
2
-
3
- from api_foundry.adapters.adapter import Adapter
4
- from api_foundry.utils.logger import logger
5
- from api_foundry.operation import Operation
6
-
7
- log = logger(__name__)
8
-
9
-
10
- class CaseChangeAdapter(Adapter):
11
- """
12
- Handles changing case from snake to camel and back
13
- """
14
-
15
- def unmarshal(self, event) -> Operation:
16
- """
17
- Unmarshal the event in a tuple for processing
18
-
19
- Parameters:
20
- - event (dict): Lambda event object.
21
-
22
- Returns:
23
- - tuple: Tuple containing entity operation, store_params, query_params and metadata params.
24
- """
25
- operation = super().unmarshal(event)
26
-
27
- # determine case
28
- self.camel_case = (
29
- operation.metadata_params.get("_case", "snake") == "camel"
30
- or self.__check_camel_case(operation.store_params)
31
- or self.__check_camel_case(operation.query_params)
32
- )
33
- log.info(f"camel_case: {self.camel_case}")
34
-
35
- if self.camel_case:
36
- return Operation(
37
- operation_id=operation.operation_id,
38
- action=operation.action,
39
- store_params=decamelize(operation.store_params),
40
- query_params=decamelize(operation.query_params),
41
- metadata_params=operation.metadata_params,
42
- )
43
-
44
- return operation
45
-
46
- def marshal(self, result: list[dict]):
47
- """
48
- Marshal the result into a event response
49
-
50
- Parameters:
51
- - result (list): the data set to return in the response
52
-
53
- Returns:
54
- - the event response
55
- """
56
- super().marshal(result)
57
-
58
- if not self.camel_case:
59
- return result
60
-
61
- converted_result = []
62
- for item in result:
63
- converted_result.append(camelize(item))
64
-
65
- # convert back to camel case if needed
66
- return converted_result
67
-
68
- def __check_camel_case(self, params: dict) -> bool:
69
- if params is not None:
70
- # check the keys for an upper case character
71
- for param in params:
72
- if (
73
- param != param.lower()
74
- and param != param.upper()
75
- and "_" not in param
76
- ):
77
- return True
78
-
79
- return False