Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing issues on #26 #30

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,7 @@ amplifyconfiguration.json
amplify-build-config.json
amplify-gradle-config.json
amplifytools.xcconfig

#Python APIs
__pycache__/
.pytest_cache/
69 changes: 0 additions & 69 deletions API/get_user_by_Id/get_user_by_Id.py

This file was deleted.

5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
The template is written with SAM.

##Tests
To Run the tests for APIs just run pytest -v from the root.
Empty file added SAM/API/__init__.py
Empty file.
Binary file added SAM/API/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
76 changes: 76 additions & 0 deletions SAM/API/get_user_by_Id/get_user_by_Id.py
Original file line number Diff line number Diff line change
@@ -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
Empty file added SAM/test_API/__init__.py
Empty file.
Binary file added SAM/test_API/__pycache__/__init__.cpython-36.pyc
Binary file not shown.
Empty file.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
#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
Binary file added __pycache__/conftest.cpython-36-pytest-5.4.3.pyc
Binary file not shown.