-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
30 lines (23 loc) · 844 Bytes
/
utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import pymongo
from datetime import datetime
from functools import wraps
from flask import request
import config
# connect to the MongoDB server
client = pymongo.MongoClient(config.MONGO_URL)
db = client["endpoint_calls"]
collection = db["calls"]
# define the wrapper function
def log_endpoint_calls(func):
@wraps(func)
def wrapper(*args, **kwargs):
# get the current time and the endpoint URL
timestamp = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
url = request.url
# create a document to insert into the MongoDB collection
document = {"timestamp": timestamp, "url": url}
# insert the document into the collection
result = collection.insert_one(document)
# call the original function and return its result
return func(*args, **kwargs)
return wrapper