Initial commit
This commit is contained in:
commit
71007dc1b9
96
README.md
Normal file
96
README.md
Normal file
@ -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),
|
||||
};
|
||||
}
|
||||
};
|
||||
```
|
59
test.py
Normal file
59
test.py
Normal file
@ -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")
|
Loading…
Reference in New Issue
Block a user