Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

webhooks/insping: Use datetime_to_global_time and ISO 8601 syntax.

Niloth P997bae423953
diff --git a/zerver/webhooks/insping/tests.py b/zerver/webhooks/insping/tests.py
index c3f6ff592af..1d36cf789b0 100644
--- a/zerver/webhooks/insping/tests.py
+++ b/zerver/webhooks/insping/tests.py
@@ -12,7 +12,7 @@ class InspingHookTests(WebhookTestCase):
State changed to **Available**:
* **URL**: http://privisus.zulipdev.org:9991
* **Response time**: 223 ms
-* **Timestamp**: <time:Fri Dec 29 17:23:46 2017>
+* **Timestamp**: <time:2017-12-29T17:23:46+00:00>
""".strip()
self.check_webhook(
@@ -28,7 +28,7 @@ State changed to **Available**:
State changed to **Not Responding**:
* **URL**: http://privisus.zulipdev.org:9991
* **Response time**: 942 ms
-* **Timestamp**: <time:Fri Dec 29 17:13:46 2017>
+* **Timestamp**: <time:2017-12-29T17:13:46+00:00>
""".strip()
self.check_webhook(
diff --git a/zerver/webhooks/insping/view.py b/zerver/webhooks/insping/view.py
index a8e24fb991e..acf2de302da 100644
--- a/zerver/webhooks/insping/view.py
+++ b/zerver/webhooks/insping/view.py
@@ -1,9 +1,10 @@
-import time
+from datetime import datetime
from django.http import HttpRequest, HttpResponse
from zerver.decorator import webhook_view
from zerver.lib.response import json_success
+from zerver.lib.timestamp import datetime_to_global_time
from zerver.lib.typed_endpoint import JsonBodyPayload, typed_endpoint
from zerver.lib.validator import WildValue, check_int, check_string
from zerver.lib.webhooks.common import check_send_webhook_message
@@ -13,10 +14,15 @@ MESSAGE_TEMPLATE = """
State changed to **{state}**:
* **URL**: {url}
* **Response time**: {response_time} ms
-* **Timestamp**: <time:{timestamp}>
+* **Timestamp**: {timestamp}
""".strip()
+def get_global_time(dt_str: str) -> str:
+ dt = datetime.fromisoformat(dt_str)
+ return datetime_to_global_time(dt)
+
+
@webhook_view("Insping")
@typed_endpoint
def api_insping_webhook(
@@ -30,15 +36,13 @@ def api_insping_webhook(
state_name = data["check_state_name"].tame(check_string)
url_tested = data["request_url"].tame(check_string)
response_time = data["response_time"].tame(check_int)
- timestamp = data["request_start_time"].tame(check_string)
-
- time_formatted = time.strftime("%c", time.strptime(timestamp, "%Y-%m-%dT%H:%M:%S.%f+00:00"))
+ timestamp = get_global_time(data["request_start_time"].tame(check_string))
body = MESSAGE_TEMPLATE.format(
state=state_name,
url=url_tested,
response_time=response_time,
- timestamp=time_formatted,
+ timestamp=timestamp,
)
topic_name = "insping"