forked from ehs5/crmscript_fetcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
79 lines (66 loc) · 2.94 KB
/
fetch.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
from data_creator import DataCreator
from typing import Optional
import json
import requests
CURRENT_CRMSCRIPT_VERSION: int = 2
class Fetch:
"""
Responsible for getting JSON from SuperOffice, then calling DataCreator
"""
def __init__(self, tenant: dict):
self.data: Optional[dict] = None
self.tenant: dict = tenant
self.script_url: str = self.build_script_url()
self.crmscript_version: Optional[int] = None
def build_script_url(self) -> str:
script_url: str = f"{self.tenant.get('url')}/scripts/customer.fcgi?action=safeParse" \
f"&includeId={self.tenant.get('include_id')}" \
f"&key={self.tenant.get('key')}"
# Append fetch options to URL
for key in self.tenant["fetch_options"]:
value: str = str(self.tenant['fetch_options'][key])
script_url += f"&{key}={value}"
return script_url
def get_json_from_superoffice(self) -> None:
"""Does a request to the SuperOffice CRMScript endpoint and saves the JSON response"""
try:
response = requests.get(self.script_url)
except requests.HTTPError as e:
print(f"Could not get data from SuperOffice: {e}")
except requests.ReadTimeout as e:
print(f"Could not get data from SuperOffice: {e}")
except requests.Timeout as e:
print(f"Could not get data from SuperOffice: {e}")
except requests.TooManyRedirects as e:
print(f"Could not get data from SuperOffice: {e}")
except requests.ConnectionError as e:
print(f"Could not get data from SuperOffice: {e}")
except requests.RequestException as e:
print(f"Could not get data from SuperOffice: {e}")
else:
try:
data: dict = json.loads(response.text)
print("JSON fetched!")
except json.JSONDecodeError:
print("Invalid JSON file")
else:
self.data = data
def determine_script_version(self) -> int:
""" Returns and sets version of fetcher CRMScript from returned JSON """
self.crmscript_version: int = self.data.get("script_version")
if not self.crmscript_version:
self.crmscript_version = 1 # Version 1 had no script_version key in JSON
return self.crmscript_version
def fetch(self) -> bool:
"""
Main fetch method
Returns true if CRMScripts were fetched and folders/files were created successfully
"""
print(f"Getting JSON data from SuperOffice using endpoint: {self.script_url}")
self.get_json_from_superoffice()
if not self.data:
return False
self.determine_script_version()
# Create data in local directory based on data fetched from SuperOffice
data_creator = DataCreator(self.data, self.crmscript_version, self.tenant)
return data_creator.create()