def post(self): body = request.get_json() args = {} for table, items in body["request_items"].items(): keys = [] for item in items: keys.append(serialize(item)) args[table] = {"Keys": keys} kwargs = {"RequestItems": args} try: data = db.batchGet(**kwargs) except ClientError as e: return Response(response=json.dumps({"error": str(e)}), status=500, mimetype='application/json') if len(items) == 0: return Response(response=json.dumps({"message": "Not found"}), status=404, mimetype='application/json') resp = {} for k, vals in data.items(): resp[k] = [deserialize(v) for v in vals] return resp, 200
def post(self): body = request.get_json() kwargs = { "TableName": self.TableName, "IndexName": "country_age_index", "KeyConditionExpression": "country = :a and age BETWEEN :t1 and :t2", "ExpressionAttributeValues": { ":a": { "S": body["country"] }, ":t1": {"N": str(body["age"]["from"])}, ":t2": {"N": str(body["age"]["to"])}, } } try: items = db.query(**kwargs) except ClientError as e: return Response( response=json.dumps({"error": str(e)}), status=500, mimetype='application/json') if len(items) == 0: return Response(response=json.dumps({"message": "Not found"}), status=404, mimetype='application/json') users = [deserialize(item) for item in items] return users, 200
def post(self): body = request.get_json() kwargs = { "TableName": self.TableName, "KeyConditionExpression": "first_name = :a and email = :b", "ExpressionAttributeValues": { ":a": { "S": body["first_name"] }, ":b": { "S": body["email"] } } } try: items = db.query(**kwargs) except ClientError as e: return Response( response=json.dumps({"error": str(e)}), status=500, mimetype='application/json') if len(items) == 0: return Response(response=json.dumps({"message": "Not found"}), status=404, mimetype='application/json') users = [deserialize(item) for item in items] return users[0], 200
def get(self): body = request.get_json() try: user = db.get(self.TableName, key=serialize(body)) except ClientError as e: return Response( response=json.dumps({"error": str(e)}), status=500, mimetype='application/json') if len(user.items()) == 0: return Response(response=json.dumps({"message": "Not found"}), status=404, mimetype='application/json') return deserialize(user), 200
def delete(self): body = request.get_json() kwargs = { "Key": { "first_name": {"S": body["first_name"]}, "email": {"S": body["email"]}, } } try: users = db.delete(self.TableName, **kwargs) except ClientError as e: return Response( response=json.dumps({"error": str(e)}), status=500, mimetype='application/json') return deserialize(users), 204
def put(self): body = request.get_json() kwargs = { "Key": { "first_name": {"S": body["first_name"]}, "email": {"S": body["email"]}, }, "UpdateExpression": "SET country = :val", "ExpressionAttributeValues": { ":val": {"S": body["country"]}, } } try: user = db.update(self.TableName, **kwargs) except ClientError as e: return Response( response=json.dumps({"error": str(e)}), status=500, mimetype='application/json') if len(user) == 0: return Response(response=json.dumps({"message": "Not found"}), status=404, mimetype='application/json') return deserialize(user), 201
def delete(self): body = request.get_json() kwargs = { "Key": { "first_name": {"S": body["first_name"]}, "email": {"S": body["email"]}, }, "ConditionExpression": "age < :a", "ExpressionAttributeValues": { ":a": {"N": str(body["age"])}, } } try: users = db.delete(self.TableName, **kwargs) except ClientError as e: if e.response['Error']['Code'] == 'ConditionalCheckFailedException': return Response( response=json.dumps({"message": "Invalid input for operation"}), status=400, mimetype='application/json') return Response( response=json.dumps({"error": str(e)}), status=500, mimetype='application/json') return deserialize(users), 204
def put(self): body = request.get_json() kwargs = { "Key": { "first_name": {"S": body["first_name"]}, "email": {"S": body["email"]}, }, "ConditionExpression": "attribute_exists(first_name)", "UpdateExpression": "SET last_name = :a", "ExpressionAttributeValues": { ":a": {"S": body["last_name"]}, } } try: users = db.update(self.TableName, **kwargs) except ClientError as e: if e.response['Error']['Code'] != 'ConditionalCheckFailedException': return Response( response=json.dumps({"error": "Key invalid"}), status=404, mimetype='application/json') return Response( response=json.dumps({"error": str(e)}), status=500, mimetype='application/json') return deserialize(users), 201