diff --git a/.gitignore b/.gitignore index cae7478..96453f9 100644 --- a/.gitignore +++ b/.gitignore @@ -91,3 +91,7 @@ amplifyconfiguration.json amplify-build-config.json amplify-gradle-config.json amplifytools.xcconfig + +#Python APIs +__pycache__/ +.pytest_cache/ \ No newline at end of file diff --git a/API/get_user_by_Id/get_user_by_Id.py b/API/get_user_by_Id/get_user_by_Id.py deleted file mode 100644 index 1ee74c0..0000000 --- a/API/get_user_by_Id/get_user_by_Id.py +++ /dev/null @@ -1,69 +0,0 @@ -import boto3 -from botocore.exceptions import ClientError -import logging -import json -import os - -dynamodb = boto3.resource('dynamodb') - - -def lambda_handler(event, context): - """ - A Lambda function used by the APIs from 100daysofcloud.com to get user details by Id - - Parameters: - event["queryStringParameters"]["username"] (string): 100daysofcloud user name - - Returns: - object: Returns user details from 100daysofcloud account if user is valid. - - """ - logging.basicConfig( - level=logging.INFO, - format=f'%(asctime)s %(levelname)s %(message)s' - ) - - logger = logging.getLogger() - - git_user="" - resp={} - status_code=404 - - - - #checking if the query params from the API call has a username - if "username" in event["queryStringParameters"]: - git_user=event["queryStringParameters"]['username'] - - if git_user=="": - resp['message']='Please query for a valid user' - logger.error('Username in request found to be empty') - else: - try: - table = dynamodb.Table(os.environ['databaseName']) - #querying the dynamoDb with the primary key github_username - response = table.get_item(Key={'github_username': git_user}) - #If there is an entry there exists a user with that username, return user details wrapped in an object - if "Item" in response: - resp= response['Item'] - status_code=200 - #If there is no 'Item' found in response it means that there was no data for that primary key and user does not exist - else: - status_code=404 - resp['message']='User Not found' - logger.error('User '+str(git_user)+' not found') - - except ClientError as e: - status_code=500 - logger.error(e.response['Error']['Message']) - resp['message']=e.response['Error']['Message'] - - return { - 'headers': { - 'Access-Control-Allow-Headers': 'Content-Type', - 'Access-Control-Allow-Origin': '*', - 'Access-Control-Allow-Methods': 'OPTIONS,POST,GET' - } - 'statusCode': status_code, - 'body': json.dumps(resp) - } diff --git a/README.md b/README.md index d7032d2..90f7485 100644 --- a/README.md +++ b/README.md @@ -7,4 +7,7 @@ to level 100(Introductory) and level 200 (Intermediate) cloud content. This repository holds the backend infrastructure for the 100DaysOfCloud.com website. ## SAM -The template is written with SAM. \ No newline at end of file +The template is written with SAM. + +##Tests +To Run the tests for APIs just run pytest -v from the root. \ No newline at end of file diff --git a/SAM/API/__init__.py b/SAM/API/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/SAM/API/__pycache__/__init__.cpython-36.pyc b/SAM/API/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..4749eb2 Binary files /dev/null and b/SAM/API/__pycache__/__init__.cpython-36.pyc differ diff --git a/SAM/API/__pycache__/conftest.cpython-36-pytest-5.4.3.pyc b/SAM/API/__pycache__/conftest.cpython-36-pytest-5.4.3.pyc new file mode 100644 index 0000000..0e4ae14 Binary files /dev/null and b/SAM/API/__pycache__/conftest.cpython-36-pytest-5.4.3.pyc differ diff --git a/SAM/API/get_user_by_Id/__init__.py b/SAM/API/get_user_by_Id/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/SAM/API/get_user_by_Id/__pycache__/__init__.cpython-36.pyc b/SAM/API/get_user_by_Id/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..7394c1d Binary files /dev/null and b/SAM/API/get_user_by_Id/__pycache__/__init__.cpython-36.pyc differ diff --git a/SAM/API/get_user_by_Id/__pycache__/app.cpython-36.pyc b/SAM/API/get_user_by_Id/__pycache__/app.cpython-36.pyc new file mode 100644 index 0000000..d9a7f90 Binary files /dev/null and b/SAM/API/get_user_by_Id/__pycache__/app.cpython-36.pyc differ diff --git a/SAM/API/get_user_by_Id/__pycache__/conftest.cpython-36-pytest-5.4.3.pyc b/SAM/API/get_user_by_Id/__pycache__/conftest.cpython-36-pytest-5.4.3.pyc new file mode 100644 index 0000000..71567ed Binary files /dev/null and b/SAM/API/get_user_by_Id/__pycache__/conftest.cpython-36-pytest-5.4.3.pyc differ diff --git a/SAM/API/get_user_by_Id/__pycache__/get_user_by_Id.cpython-36.pyc b/SAM/API/get_user_by_Id/__pycache__/get_user_by_Id.cpython-36.pyc new file mode 100644 index 0000000..c0bdc72 Binary files /dev/null and b/SAM/API/get_user_by_Id/__pycache__/get_user_by_Id.cpython-36.pyc differ diff --git a/SAM/API/get_user_by_Id/__pycache__/test_getUserId.cpython-36-pytest-5.4.3.pyc b/SAM/API/get_user_by_Id/__pycache__/test_getUserId.cpython-36-pytest-5.4.3.pyc new file mode 100644 index 0000000..cd1f76f Binary files /dev/null and b/SAM/API/get_user_by_Id/__pycache__/test_getUserId.cpython-36-pytest-5.4.3.pyc differ diff --git a/SAM/API/get_user_by_Id/__pycache__/test_get_user_id.cpython-36-pytest-5.4.3.pyc b/SAM/API/get_user_by_Id/__pycache__/test_get_user_id.cpython-36-pytest-5.4.3.pyc new file mode 100644 index 0000000..665477a Binary files /dev/null and b/SAM/API/get_user_by_Id/__pycache__/test_get_user_id.cpython-36-pytest-5.4.3.pyc differ diff --git a/SAM/API/get_user_by_Id/get_user_by_Id.py b/SAM/API/get_user_by_Id/get_user_by_Id.py new file mode 100644 index 0000000..f5434d3 --- /dev/null +++ b/SAM/API/get_user_by_Id/get_user_by_Id.py @@ -0,0 +1,76 @@ +import boto3 +from botocore.exceptions import ClientError +import logging +import json +import os + +dynamodb = boto3.resource('dynamodb',os.environ['AWS_REGION']) +logging.basicConfig( + level=logging.INFO, + format=f'%(asctime)s %(levelname)s %(message)s' +) + +logger = logging.getLogger() + + +def lambda_handler(event, context): + """ + A Lambda function used by the APIs from 100daysofcloud.com to get user details by Id + + Parameters: + event["queryStringParameters"]["username"] (string): 100daysofcloud user name + + Returns: + object: Returns user details from 100daysofcloud account if user is valid. + + """ + + + git_user="" + resp={} + status_code=404 + + + + #checking if the query params from the API call has a username + if "username" in event["queryStringParameters"]: + git_user=event["queryStringParameters"]['username'] + + if git_user=="": + resp['message']='Please query for a valid user' + logger.error('Username in request found to be empty') + else: + table = dynamodb.Table(os.environ['databaseName']) + resp=get_user_details_by_Id(table,git_user) + + return { + 'headers': { + 'Access-Control-Allow-Headers': 'Content-Type', + 'Access-Control-Allow-Origin': '*', + 'Access-Control-Allow-Methods': 'OPTIONS,POST,GET' + }, + 'statusCode': status_code, + 'body': json.dumps(resp) + } + + +def get_user_details_by_Id(table,git_user): + resp={} + try: + #querying the dynamoDb with the primary key github_username + response = table.get_item(Key={'github_username': git_user}) + #If there is an entry there exists a user with that username, return user details wrapped in an object + if "Item" in response: + resp= response['Item'] + status_code=200 + #If there is no 'Item' found in response it means that there was no data for that primary key and user does not exist + else: + status_code=404 + resp['message']='User Not found' + logger.error('User '+str(git_user)+' not found') + + except ClientError as e: + status_code=500 + logger.error(e.response['Error']['Message']) + resp['message']=e.response['Error']['Message'] + return resp \ No newline at end of file diff --git a/SAM/test_API/__init__.py b/SAM/test_API/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/SAM/test_API/__pycache__/__init__.cpython-36.pyc b/SAM/test_API/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..42ffaf6 Binary files /dev/null and b/SAM/test_API/__pycache__/__init__.cpython-36.pyc differ diff --git a/SAM/test_API/test_get_user_by_Id/__init__.py b/SAM/test_API/test_get_user_by_Id/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/SAM/test_API/test_get_user_by_Id/__pycache__/__init__.cpython-36.pyc b/SAM/test_API/test_get_user_by_Id/__pycache__/__init__.cpython-36.pyc new file mode 100644 index 0000000..e615c14 Binary files /dev/null and b/SAM/test_API/test_get_user_by_Id/__pycache__/__init__.cpython-36.pyc differ diff --git a/SAM/test_API/test_get_user_by_Id/__pycache__/test_get_user_by_ID.cpython-36-pytest-5.4.3.pyc b/SAM/test_API/test_get_user_by_Id/__pycache__/test_get_user_by_ID.cpython-36-pytest-5.4.3.pyc new file mode 100644 index 0000000..3dc2a21 Binary files /dev/null and b/SAM/test_API/test_get_user_by_Id/__pycache__/test_get_user_by_ID.cpython-36-pytest-5.4.3.pyc differ diff --git a/test_API/test_get_user_by_Id/test_get_user_by_ID.py b/SAM/test_API/test_get_user_by_Id/test_get_user_by_ID.py similarity index 81% rename from test_API/test_get_user_by_Id/test_get_user_by_ID.py rename to SAM/test_API/test_get_user_by_Id/test_get_user_by_ID.py index 782892e..19cf597 100644 --- a/test_API/test_get_user_by_Id/test_get_user_by_ID.py +++ b/SAM/test_API/test_get_user_by_Id/test_get_user_by_ID.py @@ -2,9 +2,7 @@ from moto import mock_dynamodb2 import boto3 import pytest - - - +from API.get_user_by_Id import get_user_by_Id @pytest.fixture(scope='function') @@ -52,5 +50,7 @@ def dynamodb_table(dynamodb): def test_get_user_by_ID(dynamodb,dynamodb_table): table=dynamodb_table table.put_item(Item={"github_username":'johndoe'}) - response = table.get_item(Key={'github_username': 'johndoe'}) - assert len(response['Item']) > 0 \ No newline at end of file + #response = table.get_item(Key={'github_username': 'johndoe'}) local test + response=get_user_by_Id.get_user_details_by_Id(table,'johndoe') + #assert True if 'github_username' in response else False + assert 'github_username' in response \ No newline at end of file diff --git a/__pycache__/conftest.cpython-36-pytest-5.4.3.pyc b/__pycache__/conftest.cpython-36-pytest-5.4.3.pyc new file mode 100644 index 0000000..96ac300 Binary files /dev/null and b/__pycache__/conftest.cpython-36-pytest-5.4.3.pyc differ