How to retrieve and store data into a db from a public api in a AWS lambda function

Viewed 163

Using an open API (I'm using New York Times book API) create an API Gateway endpoint that does the following:

Retrieves data from an API. Stores the data in a database.

I'm able to retrieve the data and store it in a string but I'm having difficulties extracting that data from the string and storing it into a dynamo DB. Any help is appreciated thank you.

Here is my code:

using System;
using System.Collections.Generic;
using System.Dynamic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Amazon.Lambda.Core;
using Newtonsoft.Json.Linq;
using Amazon.DynamoDBv2;
using Amazon.DynamoDBv2.DocumentModel;
using Amazon.Lambda.APIGatewayEvents;
using Amazon.DynamoDBv2.Model;

// Assembly attribute to enable the Lambda function's JSON input to be converted into a .NET class.
[assembly: LambdaSerializer(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]

namespace last
{
    public class Result
    {
        public string url { get; set; }
        public string publication_dt { get; set; }
        public string byline { get; set; }
        public string book_title { get; set; }
        public string book_author { get; set; }
        public string summary { get; set; }
        public List<string> isbn13 { get; set; }
    }

    public class Root
    {
        public string status { get; set; }
        public string copyright { get; set; }
        public int num_results { get; set; }
        public List<Result> results { get; set; }
    }

    public class Function
    {
        private static AmazonDynamoDBClient client1 = new AmazonDynamoDBClient();
        public static readonly HttpClient client = new HttpClient();       

        public async Task<ExpandoObject> FunctionHandler(string input, ILambdaContext context)
        {
            string tblName = "last";
       
            string url = "https://api.nytimes.com/svc/books/v3/reviews.json?title=Becoming&api-key=myKey";
            string message = await client.GetStringAsync(url);
            Result myDeserializedClass = JsonConvert.DeserializeObject<Result>(message);

            var request = new PutItemRequest
            {
                TableName = tblName,
                Item = new Dictionary<string, AttributeValue>()
              {
                  { "bookId", new AttributeValue { S = "202" }},
                  { "publication_dt", new AttributeValue { S = myDeserializedClass.publication_dt.ToString() }},
                  { "byline", new AttributeValue { S = myDeserializedClass.byline.ToString() }},
                  { "book_title", new AttributeValue { S = myDeserializedClass.book_title.ToString()}},
                  { "book_author", new AttributeValue { S = myDeserializedClass.book_author.ToString() }},
                  { "summary", new AttributeValue { S = myDeserializedClass.summary.ToString() }},
                  { "isbn13", new AttributeValue { S = myDeserializedClass.isbn13.ToString()}},
              }
            };
            await client1.PutItemAsync(request);

            //Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(message);

            //await tblName.PutItemAsync((Document)myDeserializedClass.ToString());

            return JsonConvert.DeserializeObject<ExpandoObject>(message);
        }
    }
}
0 Answers
Related