Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
eladcon committed May 28, 2024
1 parent 4c0ca82 commit badde83
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 10 deletions.
4 changes: 2 additions & 2 deletions python/test-assets/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def handler(event, context):
)

table = lifted("table")
response = table.get(Key={'id':{'S':"test"}})
table_value = response.get("Item").get("body").get("S")
response = table.get(Key={"id":"test"})
table_value = response["Item"]["body"]

bucket = lifted("bucket")
value = bucket.get("test.txt")
Expand Down
49 changes: 42 additions & 7 deletions python/test-assets/wing.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import boto3
from botocore.exceptions import ClientError
import json
from typing import Optional, List, TypedDict
from typing import Optional, List, TypedDict, Any
import os
import random

Expand Down Expand Up @@ -105,29 +105,30 @@ class Connection(TypedDict):
clientConfig: ClientConfig

class DynamodbTableClient_base:
def __init__(self, connection: Connection, dynamodb_client: boto3.client):
def __init__(self, connection: Connection, resource: boto3.resource = None):
self.table_name = connection["tableName"]
self.connection = connection
self.dynamodb_client = dynamodb_client or boto3.client('dynamodb')
self.resource = resource or boto3.resource('dynamodb')
self.table = self.resource.Table(self.table_name)

def get(self, **kwargs):
return self.dynamodb_client.get_item(TableName=self.table_name, **kwargs)
return self.table.get_item(**kwargs)

def put(self, **kwargs):
return self.dynamodb_client.put_item(TableName=self.table_name, **kwargs)
return self.table.put_item(**kwargs)

def read_write_connection(self):
return self.connection

class DynamodbTableClient_aws(DynamodbTableClient_base):
def __init__(self, props: dict):
connection: Connection = props["connection"]
super().__init__(connection, boto3.client('dynamodb'))
super().__init__(connection, boto3.resource('dynamodb'))

class DynamodbTableClient_sim(DynamodbTableClient_base):
def __init__(self, props: dict):
connection: Connection = props["connection"]
super().__init__(connection, boto3.client(
super().__init__(connection, boto3.resource(
'dynamodb',
region_name=connection["clientConfig"]["region"],
endpoint_url=connection["clientConfig"]["endpoint"],
Expand Down Expand Up @@ -324,3 +325,37 @@ def from_api_response(res = None):
response["headers"] = res["headers"]

return response

class Aws:
@staticmethod
def try_from_api_event(event: dict[str, Any]):
try:
return Aws.from_api_event(event)
except Exception as e:
return event

@staticmethod
def from_api_event(event: dict[str, Any]):
target = os.getenv(f"WING_TARGET")
if target == "tf-aws":
return req
elif target == "sim":
data = event["payload"]
body = data["body"]
req = {
'httpMethod': data["method"],
'path': data["path"],
'queryStringParameters': data["query"],
'headers': data["headers"],
'body': None if body == "" else body,
'pathParameters': data["vars"],
'requestContext': {
'http': {
'method': data["method"],
'path': data["path"]
}
},
}
return req
else:
raise Exception(f"Unsupported target: {target}")
2 changes: 1 addition & 1 deletion python/util.extern.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export class Node {
readonly setContext: (key: string, value?: any) => void;
/** Return a direct child by id, or undefined.
@returns the child if found, or undefined */
readonly tryFindChild: (id: string) => (IConstruct) | undefined;
readonly tryFindChild: (id: string) => IConstruct | void;
/** Retrieves a value from tree context.
Context is usually initialized at the root, but can be overridden at any point in the tree.
@returns The context value or `undefined` if there is no context value for this key. */
Expand Down

0 comments on commit badde83

Please sign in to comment.