import json import requests import boto3 import uuid from datetime import datetime from init import dynamodb_service def send_data_to_dynamodb(user_data): try: lambda_url = 'https://n0u3bakgmf.execute-api.us-east-1.amazonaws.com/dev/post-item' headers = { 'Content-Type': 'application/json' } response = requests.post(lambda_url, data=json.dumps(user_data), headers=headers) if response.status_code == 200: print("Data sent to DynamoDB successfully!") return response.json() else: print(f"Error: {response.status_code}, {response.text}") return None except Exception as e: print(f"An error occurred while sending data: {str(e)}") return None def lambda_handler(event, context): try: body = json.loads(event['body']) title = body.get('title') keywords = body.get('keywords') if not title or not keywords: return { 'statusCode': 400, 'body': json.dumps({ 'message': 'Missing title or keywords' }) } item_id = str(uuid.uuid4()) timestamp = datetime.now().isoformat() response = dynamodb_service.put_item( Item={ 'id': item_id, 'title': title, 'keywords': keywords, 'createdAt': timestamp } ) return { 'statusCode': 200, 'body': json.dumps({ 'message': 'Data successfully inserted', 'itemId': item_id }) } except Exception as e: return { 'statusCode': 500, 'body': json.dumps({ 'message': 'An error occurred', 'error': str(e) }) }