From 5a83ab3456721de9999805e927333b13369f1ad2 Mon Sep 17 00:00:00 2001 From: Claire Date: Tue, 31 Oct 2023 19:59:31 -0500 Subject: [PATCH 1/6] fix body issues --- .DS_Store | Bin 6148 -> 6148 bytes code/mapper.py | 8 ++++---- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.DS_Store b/.DS_Store index 4549ab94f863a3e315adb5a601173d4edfcea652..11017da76373ae5dc017e658fb5813111a6c07d3 100644 GIT binary patch literal 6148 zcmeHL%}OId5U$oC#zaMM4}0)17ePb#Za#~|zB4q5KMixXIJ&Snp9l~Dr z5%#oRb}#z?E53qn;8)$9q&t5udk~Q>=>Dd=s^+WynCZ@hh{SrUvPLvdL|HV(Act;< zv7g(3O_-TQpx}EnYHP(knKTC?{e)M*EASQ-;CDBm0@dh%PMr5Qv+>dPB{Gi74Ozxq z_I_pc_usESKe_(OSbwu_?G5<|sWjE$r8>2!?66aQb1=fLVd3-T*{_{T*4 z%BhD`JsH}g26U*>VV@S$d0w~Uw`=)OxS4uts#^MDdXxUE|24;1ht@N|IwaOmb!*huDbr7hQs_W?<4CMfK!6|*yWv>wS6}4bp5LHEOm2j4Tkw$UOqp;d#awDQ=2z4 z`+dpw*}Q+w)A*tD9+_O5cbMPi&D`BG@BbRkY_@D(qwl=}UIDK_R{`E1LNvz6V5(7V z9q1-7WPiZOhHJGK0z)&745k|4feD=o)TzRJVhEj%`Ow5g22+hXorGe>eGIcOUnoMc zV?I>jBqEKz_X>CgTm>feaGuZq4ZOo6zW?1MKk^EA1>ThcB8ZFeI+kS4)`7+GSsS8# sL1W{%RHI5kXO3fa!ACLwcc2WukS~Cd!Bit=VE%`IlEHUgfmc=F8Ba~;0{{R3 delta 114 zcmZoMXfc=|#>CJzF;Q%yo}wrt0|NsP3otO`Go&!2rW7aVB;`*`RF?rsuruf}lra=D zBqK}a=P+&-O?J diff --git a/code/mapper.py b/code/mapper.py index 28c45c4..bcfbfb6 100644 --- a/code/mapper.py +++ b/code/mapper.py @@ -1,6 +1,6 @@ from student.upload import get_upload_url from recruiter.get import get_resume_url -from code.student.user import get_user, update_user, register_user +from student.user import get_user, update_user, register_user import json import traceback @@ -27,7 +27,7 @@ def badRequest(message): def getUploadUrl(context, queryParams, body): rval = {} try: - url: str = get_upload_url(f"resume_{context['uid']}.pdf") + url: str = get_upload_url(f"resume_{queryParams['uid']}.pdf") rval = { "statusCode": 200, "body": json.dumps({ @@ -78,7 +78,7 @@ def updateUser(context, queryParams, body): if body == "": return try: - update_user(queryParams['uid']) + update_user(queryParams['uid'], body) except: rval = serverError("Could not get user.") traceback.print_exc() @@ -100,7 +100,7 @@ def updateUser(context, queryParams, body): def execute(method: str, path: str, queryParams: dict, context: dict, body: str) -> dict: try: func: function = find_handler[method][path] - return func(context, queryParams) + return func(context, queryParams, body) except KeyError as e: print(f"ERROR: No handler found for method {method} and path {path}.") return notImplemented(context, queryParams) \ No newline at end of file From b04a50ee99694c28b70e87dbb62c2bac99424173 Mon Sep 17 00:00:00 2001 From: Claire Date: Wed, 1 Nov 2023 14:37:01 -0500 Subject: [PATCH 2/6] fix update_user and get_user --- code/api.py | 7 +++++-- code/mapper.py | 2 +- code/student/user.py | 31 +++++++++++++++++++------------ test.json | 23 +++++++++++++++++++---- 4 files changed, 44 insertions(+), 19 deletions(-) diff --git a/code/api.py b/code/api.py index 2f00da8..d2bd708 100644 --- a/code/api.py +++ b/code/api.py @@ -3,10 +3,13 @@ def lambda_handler(event, context): method = event['httpMethod'] path = event['path'] queryParams = event["queryStringParameters"] + body = event["body"] if not queryParams: queryParams = {} + if not body: + body = {} print(f"INFO: Processing request: method {method}, path {path}.") try: - return mapper.execute(method, path, queryParams, event['requestContext']['authorizer'], event['body']) + return mapper.execute(method, path, queryParams, event['requestContext']['authorizer'], body) except KeyError: - return mapper.execute(method, path, queryParams, {}, "") \ No newline at end of file + return mapper.execute(method, path, queryParams, {}, body) \ No newline at end of file diff --git a/code/mapper.py b/code/mapper.py index bcfbfb6..b17e78d 100644 --- a/code/mapper.py +++ b/code/mapper.py @@ -78,7 +78,7 @@ def updateUser(context, queryParams, body): if body == "": return try: - update_user(queryParams['uid'], body) + update_user(queryParams["uid"], body) except: rval = serverError("Could not get user.") traceback.print_exc() diff --git a/code/student/user.py b/code/student/user.py index 142ca04..2381947 100644 --- a/code/student/user.py +++ b/code/student/user.py @@ -32,35 +32,43 @@ def __init__(self, uid, name, email, linkedin, degree, majors, minors, gpa, year self.work_auth: work_auth self.sponsor: sponsor -client = boto3.client('dynamodb', region_name=os.environ.get('AWS_REGION', 'us-east-2')) -dynamo_table = 'infra-resume-book-users' +client = boto3.client('dynamodb', region_name='us-east-2') +dynamo_table = "infra-resume-book-users" def get_user(id: int) -> str | None: response = client.get_item( TableName=dynamo_table, Key={ - 'id': id + "uin": id } ) + print(response["Item"]) -def update_user(id, body): +def update_user(id: int, body: str) -> str | None: temp = {} - attributes = {"name", "email", "linkedin", "degree", "majors", "minors", "gpa", "year", "bio", "skills", "position", "sponsor"} + attributes = {"name", "email", "linkedin", "degree", "majors", "minors", "gpa", "year", "bio", "skills", "position", "work_auth", "sponsor"} for key, value in body.items(): if key in attributes: temp[key] = value + # print(temp) + + update_expression = 'SET {}'.format(','.join(f'#{k}=:{k}' for k in temp)) + expression_attribute_values = {f':{k}': v for k, v in temp.items()} + expression_attribute_names = {f'#{k}': k for k in temp} response = client.update_item( TableName=dynamo_table, Key={ - 'id': id - }, - AttributeUpdates = { - 'id': { - 'Value' : temp, - }, + 'uin': id }, + UpdateExpression=update_expression, + ExpressionAttributeValues=expression_attribute_values, + ExpressionAttributeNames=expression_attribute_names, + ReturnValues='UPDATED_NEW', + ) + # print(response("Item")) + # ExpressionAttributeNames={ # '#n': "name", # '#e': "email", @@ -90,7 +98,6 @@ def update_user(id, body): # ':w': {'BOOL': work_auth}, # ':ma': {'BOOL': sponsor}, # } -) def register_user(uid, name, email, linkedin, degree, majors, minors, gpa, year, bio, skills, position, work_auth, sponsor): client.put_item( diff --git a/test.json b/test.json index 6ea9aa5..0f4a33d 100644 --- a/test.json +++ b/test.json @@ -1,7 +1,7 @@ { "resource": "/myresource", - "path": "/api/v1/healthz", - "httpMethod": "GET", + "path": "/api/v1/student", + "httpMethod": "PUT", "headers": { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", "Accept-Encoding": "gzip, deflate, sdch", @@ -9,10 +9,25 @@ }, "queryStringParameters": { "param1": "value1", - "param2": "value2" + "param2": "value2", + "uid": {"N": "671901702"} }, "pathParameters": null, "stageVariables": null, "requestContext": {}, - "body": "request body goes here" + "body": { + "name": {"S": "Bipity Bopity"}, + "email": {"S": "clairechenisawesome@gmail.com"}, + "linkedin": {"S": "linkein.com/in/bipitybopity" }, + "degree": {"S": "Bachelors"}, + "majors": {"SS": ["Crop Sciences"]}, + "minors": {"SS": ["Geology"]}, + "gpa": {"N": "4"}, + "year": {"S": "05/2029"}, + "bio": {"S": "slay every day"}, + "skills": {"SS": ["live", "laugh", "love"]}, + "position": {"S": "CEO"}, + "work_auth": {"BOOL": true}, + "sponsor": {"BOOL": false} + } } \ No newline at end of file From eba5c4b07ecf2a7518f2ea49610b6a21fdf9816e Mon Sep 17 00:00:00 2001 From: Jish2 Date: Thu, 2 Nov 2023 14:40:50 -0500 Subject: [PATCH 3/6] ignore .ds_store --- .DS_Store | Bin 6148 -> 0 bytes .gitignore | 3 ++- 2 files changed, 2 insertions(+), 1 deletion(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 11017da76373ae5dc017e658fb5813111a6c07d3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHL%}OId5U$oC#zaMM4}0)17ePb#Za#~|zB4q5KMixXIJ&Snp9l~Dr z5%#oRb}#z?E53qn;8)$9q&t5udk~Q>=>Dd=s^+WynCZ@hh{SrUvPLvdL|HV(Act;< zv7g(3O_-TQpx}EnYHP(knKTC?{e)M*EASQ-;CDBm0@dh%PMr5Qv+>dPB{Gi74Ozxq z_I_pc_usESKe_(OSbwu_?G5<|sWjE$r8>2!?66aQb1=fLVd3-T*{_{T*4 z%BhD`JsH}g26U*>VV@S$d0w~Uw`=)OxS4uts#^MDdXxUE|24;1ht@N|IwaOmb!*huDbr7hQs_W?<4CMfK!6|*yWv>wS6}4bp5LHEOm2j4Tkw$UOqp;d#awDQ=2z4 z`+dpw*}Q+w)A*tD9+_O5cbMPi&D`BG@BbRkY_@D(qwl=}UIDK_R{`E1LNvz6V5(7V z9q1-7WPiZOhHJGK0z)&745k|4feD=o)TzRJVhEj%`Ow5g22+hXorGe>eGIcOUnoMc zV?I>jBqEKz_X>CgTm>feaGuZq4ZOo6zW?1MKk^EA1>ThcB8ZFeI+kS4)`7+GSsS8# sL1W{%RHI5kXO3fa!ACLwcc2WukS~Cd!Bit=VE%`IlEHUgfmc=F8Ba~;0{{R3 diff --git a/.gitignore b/.gitignore index 06304c4..901a23b 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ .aws-sam/ venv/ -__pycache__ \ No newline at end of file +__pycache__ +.DS_Store \ No newline at end of file From adf545868b11e8a1ec85bc0a267e6ada78f8473e Mon Sep 17 00:00:00 2001 From: Jish2 Date: Thu, 2 Nov 2023 17:43:28 -0500 Subject: [PATCH 4/6] env example --- .env.example | 1 + 1 file changed, 1 insertion(+) create mode 100644 .env.example diff --git a/.env.example b/.env.example new file mode 100644 index 0000000..470e4d5 --- /dev/null +++ b/.env.example @@ -0,0 +1 @@ +AWS_REGION = us-east-2 \ No newline at end of file From c7d8960f2cd33142c27715e3ad2931314fb745f2 Mon Sep 17 00:00:00 2001 From: Jish2 Date: Thu, 2 Nov 2023 17:43:35 -0500 Subject: [PATCH 5/6] updated pk --- code/student/user.py | 17 +++++++++-------- create_table.py | 31 ------------------------------- 2 files changed, 9 insertions(+), 39 deletions(-) delete mode 100644 create_table.py diff --git a/code/student/user.py b/code/student/user.py index 2381947..d4c1f0c 100644 --- a/code/student/user.py +++ b/code/student/user.py @@ -1,7 +1,7 @@ import boto3, os class Student: - uid: int + object_id: str name: str email: str linkedin: str @@ -16,7 +16,7 @@ class Student: work_auth: bool sponsor: bool - def __init__(self, uid, name, email, linkedin, degree, majors, minors, gpa, year, bio, skills, position, work_auth, sponsor): + def __init__(self, object_id, name, email, linkedin, degree, majors, minors, gpa, year, bio, skills, position, work_auth, sponsor): self.uid: uid self.name: name self.email: email @@ -32,20 +32,21 @@ def __init__(self, uid, name, email, linkedin, degree, majors, minors, gpa, year self.work_auth: work_auth self.sponsor: sponsor -client = boto3.client('dynamodb', region_name='us-east-2') +client = boto3.client('dynamodb', region_name=os.environ.get('AWS_REGION', 'us-east-2')) + dynamo_table = "infra-resume-book-users" def get_user(id: int) -> str | None: response = client.get_item( TableName=dynamo_table, Key={ - "uin": id + "object_id": id } ) print(response["Item"]) -def update_user(id: int, body: str) -> str | None: +def update_user(id: str, body: str) -> str | None: temp = {} attributes = {"name", "email", "linkedin", "degree", "majors", "minors", "gpa", "year", "bio", "skills", "position", "work_auth", "sponsor"} for key, value in body.items(): @@ -60,7 +61,7 @@ def update_user(id: int, body: str) -> str | None: response = client.update_item( TableName=dynamo_table, Key={ - 'uin': id + 'object_id': id }, UpdateExpression=update_expression, ExpressionAttributeValues=expression_attribute_values, @@ -99,11 +100,11 @@ def update_user(id: int, body: str) -> str | None: # ':ma': {'BOOL': sponsor}, # } -def register_user(uid, name, email, linkedin, degree, majors, minors, gpa, year, bio, skills, position, work_auth, sponsor): +def register_user(id, name, email, linkedin, degree, majors, minors, gpa, year, bio, skills, position, work_auth, sponsor): client.put_item( TableName=dynamo_table, Item={ - "uid": {'N': uid}, + "object_id": {'N': id}, "name": {'S': name}, "email": {'S': email}, "linkedin": {'S': linkedin}, diff --git a/create_table.py b/create_table.py deleted file mode 100644 index 458fec1..0000000 --- a/create_table.py +++ /dev/null @@ -1,31 +0,0 @@ -import boto3 - -def create_devices_table(dynamodb=None): - dynamodb = boto3.resource( - 'dynamodb', region_name='us-east-2') - table = dynamodb.create_table( - TableName='infra-resume-book-users', - KeySchema=[ - { - 'AttributeName': 'uin', - 'KeyType': 'HASH' # Partition key - } - ], - AttributeDefinitions=[ - { - 'AttributeName': 'uin', - 'AttributeType': 'N' - } - ], - ProvisionedThroughput={ - 'ReadCapacityUnits': 10, - 'WriteCapacityUnits': 10 - } - ) - return table - - -if __name__ == '__main__': - device_table = create_devices_table() - # Print tablle status - print("Status:", device_table.table_status) \ No newline at end of file From 6f541a69597c5397da8699407e043d0a6af327dd Mon Sep 17 00:00:00 2001 From: Jish2 Date: Thu, 2 Nov 2023 17:52:26 -0500 Subject: [PATCH 6/6] update --- code/student/user.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/code/student/user.py b/code/student/user.py index d4c1f0c..a2d1d24 100644 --- a/code/student/user.py +++ b/code/student/user.py @@ -104,7 +104,7 @@ def register_user(id, name, email, linkedin, degree, majors, minors, gpa, year, client.put_item( TableName=dynamo_table, Item={ - "object_id": {'N': id}, + "object_id": {'S': id}, "name": {'S': name}, "email": {'S': email}, "linkedin": {'S': linkedin},