changeset 3:5c36f51105c2

fetch tweets working
author Dennis Concepcion Martin <dennisconcepcionmartin@gmail.com>
date Thu, 16 Sep 2021 16:51:59 +0200
parents 561bc303784f
children cfd876570008
files .idea/tweet-analysis.iml README.md src/handlers/sentiment.py src/requirements.txt template.yaml
diffstat 5 files changed, 93 insertions(+), 6 deletions(-) [+]
line wrap: on
line diff
--- a/.idea/tweet-analysis.iml	Thu Sep 16 11:36:21 2021 +0200
+++ b/.idea/tweet-analysis.iml	Thu Sep 16 16:51:59 2021 +0200
@@ -7,4 +7,7 @@
     <orderEntry type="inheritedJdk" />
     <orderEntry type="sourceFolder" forTests="false" />
   </component>
+  <component name="PackageRequirementsSettings">
+    <option name="requirementsPath" value="$MODULE_DIR$/src/requirements.txt" />
+  </component>
 </module>
\ No newline at end of file
--- a/README.md	Thu Sep 16 11:36:21 2021 +0200
+++ b/README.md	Thu Sep 16 16:51:59 2021 +0200
@@ -11,6 +11,17 @@
 These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS 
 resources through the same deployment process that updates your application code.
 
+## Secrets
+There are some process that must be made manually, such us secrets creation in AWS Secrets Manager. Before deploying
+the application do the following: 
+
+**Create Twitter keys**:   
+- Go to AWS Secrets Manager Console
+- Create `Other type of secrets`
+- Create two keys called `KEY` and `BEARER`
+- Add the values for each one
+- Name the secret `tweet-analysis-keys`
+
 ## Deploy the application
 
 The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality
--- a/src/handlers/sentiment.py	Thu Sep 16 11:36:21 2021 +0200
+++ b/src/handlers/sentiment.py	Thu Sep 16 16:51:59 2021 +0200
@@ -1,9 +1,12 @@
 import json
+import requests
+import boto3
+import base64
+from botocore.exceptions import ClientError
 
 
 def get_tweet_sentiment(event, context):
     """
-
     :param event: dict, required
         API Gateway Lambda Proxy Input Format
     :param context: object, required
@@ -12,11 +15,80 @@
         API Gateway Lambda Proxy Output Format
     """
 
-    print('hello world')
+    twitter_url = create_twitter_url(user='dennisconcep')
+    twitter_key = get_twitter_key()
+    bearer_token = twitter_key['BEARER']
+    twitter_header = {"Authorization": "Bearer {}".format(bearer_token)}  # Auth header
+    twitter_response = requests.request("GET", twitter_url, headers=twitter_header)
+    print(twitter_response.json())
 
     return {
         "statusCode": 200,
         "body": json.dumps({
-            "message": "hello world"
+            "message": "hello world",
         }),
     }
+
+
+def create_twitter_url(user, max_results=100):
+    """
+    Create url to fetch `max_results` of tweets from `user`
+    :param user: string, required
+    :param max_results: int, optional, default 100
+    :return: string url
+    """
+
+    formatted_max_results = 'max_results={}'.format(max_results)
+    formatted_user = 'query=from:{}'.format(user)
+    url = "https://api.twitter.com/2/tweets/search/recent?{}&{}".format(formatted_max_results, formatted_user)
+
+    return url
+
+
+def get_twitter_key():
+    """
+    Get Twitter Api Key from AWS Secrets Manager
+    :return:
+    """
+    secret_name = "tweet-analysis-keys"
+    region_name = "eu-west-2"
+
+    # Create a Secrets Manager client
+    session = boto3.session.Session()
+    client = session.client(
+        service_name='secretsmanager',
+        region_name=region_name
+    )
+
+    try:
+        get_secret_value_response = client.get_secret_value(SecretId=secret_name)
+    except ClientError as e:
+        if e.response['Error']['Code'] == 'DecryptionFailureException':
+            # Secrets Manager can't decrypt the protected secret text using the provided KMS key.
+            # Deal with the exception here, and/or rethrow at your discretion.
+            raise e
+        elif e.response['Error']['Code'] == 'InternalServiceErrorException':
+            # An error occurred on the server side.
+            # Deal with the exception here, and/or rethrow at your discretion.
+            raise e
+        elif e.response['Error']['Code'] == 'InvalidParameterException':
+            # You provided an invalid value for a parameter.
+            # Deal with the exception here, and/or rethrow at your discretion.
+            raise e
+        elif e.response['Error']['Code'] == 'InvalidRequestException':
+            # You provided a parameter value that is not valid for the current state of the resource.
+            # Deal with the exception here, and/or rethrow at your discretion.
+            raise e
+        elif e.response['Error']['Code'] == 'ResourceNotFoundException':
+            # We can't find the resource that you asked for.
+            # Deal with the exception here, and/or rethrow at your discretion.
+            raise e
+    else:
+        # Decrypts secret using the associated KMS CMK.
+        # Depending on whether the secret is a string or binary, one of these fields will be populated.
+        if 'SecretString' in get_secret_value_response:
+            secret = get_secret_value_response['SecretString']
+        else:
+            secret = base64.b64decode(get_secret_value_response['SecretBinary'])
+
+        return json.loads(secret)
--- a/src/requirements.txt	Thu Sep 16 11:36:21 2021 +0200
+++ b/src/requirements.txt	Thu Sep 16 16:51:59 2021 +0200
@@ -1,1 +1,2 @@
-requests
\ No newline at end of file
+requests
+boto3
\ No newline at end of file
--- a/template.yaml	Thu Sep 16 11:36:21 2021 +0200
+++ b/template.yaml	Thu Sep 16 16:51:59 2021 +0200
@@ -157,14 +157,14 @@
       CodeUri: src/
       Handler: handlers/sentiment.get_tweet_sentiment
       Runtime: python3.9
-      Tags:
-        Name: "tweet-analysis::get-tweet-sentiment-function"
       Events:
         CallGetTweetSentiment:
           Type: Api
           Properties:
             Path: /sentiment
             Method: get
+      Tags:
+        Name: "tweet-analysis::get-tweet-sentiment-function"
 
   ##
   ### END FUNCTION CONFIGURATION ###