-
Notifications
You must be signed in to change notification settings - Fork 4
/
api_output.py
120 lines (97 loc) · 3.87 KB
/
api_output.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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import requests
import json
import csv
import io
from functools import partial
import xml.etree.ElementTree as ET
from urllib.parse import urlparse
import threading
from sc_logging import logger
from text_detection_target import TextDetectionTargetWithResult
from storage import fetch_data, subscribe_to_data
out_api_url = fetch_data("scoresight.json", "out_api_url", None)
out_api_encoding = fetch_data("scoresight.json", "out_api_encoding", "JSON")
def is_valid_url_urllib(url):
try:
result = urlparse(url)
return all([result.scheme, result.netloc])
except ValueError:
return False
def setup_out_api_url(url):
global out_api_url
out_api_url = url
def setup_out_api_encoding(encoding):
global out_api_encoding
out_api_encoding = encoding
subscribe_to_data("scoresight.json", "out_api_url", setup_out_api_url)
subscribe_to_data("scoresight.json", "out_api_encoding", setup_out_api_encoding)
def update_out_api(data: list[TextDetectionTargetWithResult]):
if out_api_url is None or out_api_encoding is None:
logger.error(f"Output API not set up: {out_api_url}, {out_api_encoding}")
return
# validate the URL
if not is_valid_url_urllib(out_api_url):
logger.error(f"Invalid URL: {out_api_url}")
return
logger.debug(f"Sending data to output API: {out_api_url}")
def send_data():
try:
if out_api_encoding == "JSON":
response = send_json(data)
elif out_api_encoding == "XML":
response = send_xml(data)
elif out_api_encoding == "CSV":
response = send_csv(data)
else:
logger.error("Invalid encoding: %s", out_api_encoding)
return
if response.status_code != 200:
logger.error(
f"Error sending data to output API: {out_api_url}, {response.status_code}"
)
except Exception as e:
logger.error(f"Error sending data to output API: {out_api_url}, {e}")
thread = threading.Thread(target=send_data)
thread.start()
def send_json(data: list[TextDetectionTargetWithResult]):
headers = {"Content-Type": "application/json"}
response = requests.post(
out_api_url,
headers=headers,
data=json.dumps([result.to_dict() for result in data]),
)
return response
def send_xml(data: list[TextDetectionTargetWithResult]):
headers = {"Content-Type": "application/xml"}
root = ET.Element("root")
for targetWithResult in data:
resultEl = ET.SubElement(root, "result")
resultEl.set("name", targetWithResult.name)
resultEl.set("result", targetWithResult.result)
resultEl.set("result_state", targetWithResult.result_state.name)
resultEl.set("x", str(targetWithResult.x()))
resultEl.set("y", str(targetWithResult.y()))
resultEl.set("width", str(targetWithResult.width()))
resultEl.set("height", str(targetWithResult.height()))
xml_data = ET.tostring(root, encoding="utf-8")
response = requests.post(out_api_url, headers=headers, data=xml_data)
return response
def send_csv(data: list[TextDetectionTargetWithResult]):
headers = {"Content-Type": "text/csv"}
output = io.StringIO()
writer = csv.writer(output)
writer.writerow(["Name", "Text", "State", "X", "Y", "Width", "Height"])
for result in data:
writer.writerow(
[
result.name,
result.result,
result.result_state.name,
result.x(),
result.y(),
result.width(),
result.height(),
]
)
response = requests.post(out_api_url, headers=headers, data=output.getvalue())
return response