Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add verbose logs on every response #226

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions webhooks/middleware/protocols/crop_plate.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
import json
import logging
import os
from io import BytesIO
from typing import Any

import requests
from PIL import Image

logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)


def crop_image(image_data, crop_box):
image = Image.open(BytesIO(image_data))
Expand All @@ -19,6 +24,7 @@ def process_request(
json_data: dict[str, Any], upload_file: bytes | None = None
) -> tuple[str, int]:
if not upload_file:
logging.error("No file uploaded.")
return "No file uploaded.", 400

data = json_data["data"]["results"][0]
adolfoarmas marked this conversation as resolved.
Show resolved Hide resolved
Expand All @@ -41,6 +47,8 @@ def process_request(
response = requests.post(os.getenv("WEBHOOK_URL", ""), data=data, files=files)

if response.status_code == 200:
logging.info("Webhook request sent successfully.")
return "Webhook request sent successfully.", response.status_code
else:
logging.error(f"Webhook request failed. Response code: {response.status_code}")
return "Webhook request failed.", response.status_code
adolfoarmas marked this conversation as resolved.
Show resolved Hide resolved
4 changes: 2 additions & 2 deletions webhooks/middleware/protocols/nx.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,10 +145,10 @@ def process_request(
try:
response = session.post(url, json=payload, headers=headers, verify=ssl)
response.raise_for_status()
logging.info("Request was successful.")
logging.info(f"Vehicle: {plate}. Request was successful.")
return "Request was successful", response.status_code
except requests.exceptions.RequestException as err:
logging.error(f"Error processing the request: {err}")
logging.error(f"Vehicle: {plate}.Error processing the request: {err}")
return f"Failed to process the request: {err}", 400


Expand Down
10 changes: 9 additions & 1 deletion webhooks/middleware/protocols/openeye.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import json
import logging
import os
from datetime import datetime
from typing import Any

import requests

logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)


def convert_to_timestamp_microseconds(time_string):
datetime_object = datetime.strptime(time_string, "%Y-%m-%dT%H:%M:%S.%fZ")
Expand Down Expand Up @@ -64,7 +69,10 @@ def process_request(
try:
response = requests.post(url, headers=headers, data=payload)
response.raise_for_status()
logging.info(
f"Vehicle:{plate}. Response sent successfully: {response.status_code}"
)
AashishDhakal marked this conversation as resolved.
Show resolved Hide resolved
return "Request sent successfully.", response.status_code
except requests.exceptions.HTTPError as err:
print(err)
logging.error(f"Vehicle:{plate}. Failed to send request: {err}")
AashishDhakal marked this conversation as resolved.
Show resolved Hide resolved
return f"Failed to send request: {err}", 400
9 changes: 5 additions & 4 deletions webhooks/middleware/protocols/salient.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,13 +35,13 @@ def notify_salient(
)
res.raise_for_status()
except requests.exceptions.HTTPError as errh:
lgr.error("Http Error:", errh)
lgr.error(f"Vehicle: {description}. Http Error:", errh)
except requests.exceptions.ConnectionError as errc:
lgr.error("Error Connecting:", errc)
lgr.error(f"Vehicle: {description}. Error Connecting:", errc)
except requests.exceptions.Timeout as errt:
lgr.error("Timeout Error:", errt)
lgr.error(f"Vehicle: {description}. Timeout Error:", errt)
except requests.exceptions.RequestException as err:
lgr.error("Oops: Something Else", err)
lgr.error(f"Vehicle: {description}. Oops: Something Else", err)


def process_request(
Expand Down Expand Up @@ -69,6 +69,7 @@ def process_request(
username, password, vms_api_url, camera_uid, camera_id, plate, timestamp
)
adolfoarmas marked this conversation as resolved.
Show resolved Hide resolved
except Exception as e:
logging.error(f"Vehicle: {plate}. Failed to notify Salient: {e}")
return f"Failed to notify Salient: {e}", 400

return "OK", 200
7 changes: 7 additions & 0 deletions webhooks/middleware/protocols/soap.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import base64
import logging
import os
from datetime import datetime
from typing import Any

from zeep import Client, Transport

logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)


def forward_to_SOAP_service(json_data, image):
# Retrieve environment variables for the SOAP service
Expand Down Expand Up @@ -43,8 +48,10 @@ def forward_to_SOAP_service(json_data, image):
response = client.service.PostImage(**request_data)

if response:
logging.info(f"Vehicle: {plate}. SOAP request successful.")
return "SOAP request successful.", 200
else:
logging.error(f"Vehicle: {plate}. SOAP request failed.")
AashishDhakal marked this conversation as resolved.
Show resolved Hide resolved
return "SOAP request failed.", 400


Expand Down
10 changes: 10 additions & 0 deletions webhooks/middleware/protocols/synology_rest.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import logging
import os
from typing import Any
from urllib import parse

import dateutil.parser as dp
import requests

logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)


def process_request(
json_data: dict[str, Any], upload_file: bytes | None = None
Expand All @@ -20,6 +25,7 @@ def process_request(
parsed_date_time = dp.parse(timestamp)
timestamp = str(int(parsed_date_time.timestamp()))
except ValueError:
logging.error(f"Vehicle: {plate}. Invalid timestamp format.")
return "Invalid timestamp format.", 400

request_data = {
Expand All @@ -33,8 +39,12 @@ def process_request(
response = requests.post(url=url, headers=HEADERS, data=data, verify=False)

if response.status_code == 200:
logging.info(f"Vehicle: {plate}. REST request successful.")
return "REST request successful.", response.status_code
else:
AashishDhakal marked this conversation as resolved.
Show resolved Hide resolved
logging.error(
f"Vehicle: {plate}. REST request failed. Response code: {response.status_code}"
)
return (
f"REST request failed. Response code: {response.status_code}",
response.status_code,
Expand Down
Loading