From 71007dc1b9ae463dfccc067fcb99c3ddadaa1d38 Mon Sep 17 00:00:00 2001 From: Marcin Czerniak Date: Thu, 15 Feb 2024 20:34:18 +0100 Subject: [PATCH] Initial commit --- README.md | 96 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ test.py | 59 ++++++++++++++++++++++++++++++++++ 2 files changed, 155 insertions(+) create mode 100644 README.md create mode 100644 test.py diff --git a/README.md b/README.md new file mode 100644 index 0000000..adaa8ea --- /dev/null +++ b/README.md @@ -0,0 +1,96 @@ +# REST API na środoiwsku AWS Lambda i DynamoDB + +# Konfiguracja +Poradnik jak skonfigurować środowisko można znaleźć pod adresem: https://docs.aws.amazon.com/apigateway/latest/developerguide/http-api-dynamo-db.html + +# Kod +Poniżej znajduje się kod funkcji Lambda, która obsługuje zapytania HTTP i komunikuje się z bazą danych DynamoDB - wykonany na potrzebę zadania z przedmiotu *Chmury obliczeniowe* (zad. `6.2`) + +```js +import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; +import { + DynamoDBDocumentClient, + PutCommand, + GetCommand, + DeleteCommand, +} from "@aws-sdk/lib-dynamodb"; +import { randomUUID } from 'crypto'; + +const client = new DynamoDBClient({}); +const dynamo = DynamoDBDocumentClient.from(client); +const tableName = "posts"; + +const DB = { + async create({ userId, title, body }) { + const id = randomUUID(); + await dynamo.send( + new PutCommand({ + TableName: tableName, + Item: { id, userId, title, body }, + }) + ); + return await DB.get(id); + }, + async get(id) { + return (await dynamo.send( + new GetCommand({ + TableName: tableName, + Key: { id }, + }) + ))?.Item; + }, + async update(id, { userId, title, body }) { + await dynamo.send( + new PutCommand({ + TableName: tableName, + Item: { id, userId, title, body }, + }) + ); + return await DB.get(id); + }, + async delete(id) { + _result = await DB.get(id); + await dynamo.send( + new DeleteCommand({ + TableName: tableName, + Key: { id }, + }) + ); + return _result; + }, +} + +export const handler = async (event, context) => { + let body; + + try { + switch (event.routeKey) { + case "POST /": + body = await DB.create(JSON.parse(event.body)); + break; + case "DELETE /{id}": + body = await DB.delete(event.pathParameters.id); + break; + case "GET /{id}": + body = await DB.get(event.pathParameters.id); + break; + case "PUT /{id}": + body = await DB.update(event.pathParameters.id, JSON.parse(event.body)); + break; + default: + throw new Error(`Unsupported route: "${event.routeKey}"`); + } + + return { + statusCode: 200, + headers: { "Content-Type": "application/json" }, + body: JSON.stringify(body), + }; + } catch (err) { + return { + statusCode: 400, + body: JSON.stringify(err.message), + }; + } +}; +``` \ No newline at end of file diff --git a/test.py b/test.py new file mode 100644 index 0000000..8214008 --- /dev/null +++ b/test.py @@ -0,0 +1,59 @@ +WEBSERVICE_URL = "https://my3s18krif.execute-api.us-east-1.amazonaws.com" + +import requests +from faker import Faker +fake = Faker() + +n = 3 + +outs = [] +outs2 = [] + +OK = True + +for i in range(n): + try: + outs.append(requests.post(WEBSERVICE_URL, json={'userId': 1, 'title': fake.catch_phrase(), 'body': fake.text()}).json()) + except: + print("Nie udało się dodać nowego zasobu") + OK = False + +for i in range(n): + try: + tmp = requests.get(WEBSERVICE_URL+'/'+str(outs[i]['id'])).json() + if tmp != outs[i]: + print("Pobrany zasób nie jest zgodny ze wzrocem") + OK = False + except: + print("Nie udało się pobrać zasobu: "+WEBSERVICE_URL+'/'+str(outs[i]['id'])) + OK = False + +for i in range(n): + try: + outs2.append(requests.put(WEBSERVICE_URL+'/'+str(outs[i]['id']), json={'userId': 1, 'title': fake.catch_phrase(), 'body': fake.text()}).json()) + except: + OK = False + print("Nie udało się zmodyfikować zasobu: "+WEBSERVICE_URL+'/'+str(outs[i]['id'])) + +for i in range(n): + try: + tmp = requests.get(WEBSERVICE_URL+'/'+str(outs[i]['id'])).json() + if tmp != outs2[i]: + print("Pobrany zasób nie jest zgodny ze zaktualizowanym wzrocem") + OK = False + except: + print("Nie udało się pobrać zasobu: "+WEBSERVICE_URL+'/'+str(outs[i]['id'])) + OK = False + +for i in range(n): + try: + requests.delete(WEBSERVICE_URL+'/'+str(outs[i]['id']), json={'userId': 1, 'title': fake.catch_phrase(), 'body': fake.text()}).json() + except Exception as e: + print(e) + print("Nie udało się usunąć zasobu: "+WEBSERVICE_URL+'/'+str(outs[i]['id'])) + OK = False + +if OK: + print("==================\nOK zaliczone") +else: + print("==================\nNie zaliczone") \ No newline at end of file