Skip to content

Commit b798735

Browse files
committed
Updates SSL context options to find default system certificates
Updates dependencies Formatting with black
1 parent a03c5d4 commit b798735

File tree

7 files changed

+163
-173
lines changed

7 files changed

+163
-173
lines changed

Pipfile.lock

Lines changed: 101 additions & 101 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

mygeotab/altitude/__init__.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
from .wrapper import AltitudeAPI
22

3-
from .daas_definition import DaasResult, DaasGetJobStatusResult, DaasGetQueryResult, DaasError, NOT_FULL_API_CALL_EXCEPTION
3+
from .daas_definition import (
4+
DaasResult,
5+
DaasGetJobStatusResult,
6+
DaasGetQueryResult,
7+
DaasError,
8+
NOT_FULL_API_CALL_EXCEPTION,
9+
)
410

511
__all__ = [
612
"AltitudeAPI",
Lines changed: 30 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
1-
21
class DaasError:
32
def __init__(self, error: dict):
43
self.error = error
5-
self.code = self.error['code']
6-
self.domain = self.error['domain']
7-
self.message = self.error['message']
4+
self.code = self.error["code"]
5+
self.domain = self.error["domain"]
6+
self.message = self.error["message"]
7+
88

99
NOT_FULL_API_CALL_EXCEPTION = Exception("api return did not have all expected attributes, please retry again.")
10+
11+
1012
class DaasResult:
1113
"""DaasResult class, the base class for all results returned from calling our application from the gateway
1214
@@ -19,10 +21,9 @@ class DaasResult:
1921
api_result_errors (list): possible errors list that happened on the altitude application.
2022
api_result_error_message (str): possible single error message that happened on the altitude application.
2123
api_result_error (DaasError): possible single error object that happened on the altitude application.
22-
errors (list): list of all the errors (gateway and altitude application) combined together.
24+
errors (list): list of all the errors (gateway and altitude application) combined together.
2325
"""
2426

25-
2627
def __init__(self, call_result: dict):
2728

2829
if not call_result:
@@ -32,10 +33,7 @@ def __init__(self, call_result: dict):
3233
self.call_result = call_result
3334

3435
self.daas_errors = [DaasError(error) for error in self.call_result.get("errors", [])]
35-
self.errors = [
36-
Exception(error.message) for error in self.daas_errors
37-
]
38-
36+
self.errors = [Exception(error.message) for error in self.daas_errors]
3937

4038
if "apiResult" not in call_result:
4139
self.errors += [Exception("apiResult not present"), NOT_FULL_API_CALL_EXCEPTION]
@@ -45,33 +43,24 @@ def __init__(self, call_result: dict):
4543
self.jobs = self.api_result["results"]
4644
self.job = self.jobs[0]
4745

48-
4946
self.api_result_errors = [DaasError(error) for error in self.api_result.get("errors", [])]
5047
self.api_result_error_message = self.api_result.get("errorMessage", None)
5148
self.api_result_error = None
5249

50+
self.errors += [Exception(error.message) for error in self.api_result_errors]
5351

54-
55-
56-
self.errors += [
57-
Exception(error.message) for error in self.api_result_errors
58-
]
59-
60-
61-
if "error" in self.api_result and self.api_result["error"]:
52+
if "error" in self.api_result and self.api_result["error"]:
6253
self.api_result_error = DaasError(self.api_result["error"])
63-
self.errors += [
64-
Exception(self.api_result_error.message)
65-
]
54+
self.errors += [Exception(self.api_result_error.message)]
6655

67-
if self.api_result_error_message and isinstance(self.api_result_error_message, str) and len(self.api_result_error_message):
68-
self.errors += [
69-
Exception(self.api_result_error_message)
70-
]
71-
elif self.api_result_error_message and isinstance(self.api_result_error_message, dict):
72-
self.errors += [
73-
Exception(self.api_result_error_message["message"])
74-
]
56+
if (
57+
self.api_result_error_message
58+
and isinstance(self.api_result_error_message, str)
59+
and len(self.api_result_error_message)
60+
):
61+
self.errors += [Exception(self.api_result_error_message)]
62+
elif self.api_result_error_message and isinstance(self.api_result_error_message, dict):
63+
self.errors += [Exception(self.api_result_error_message["message"])]
7564

7665

7766
class DaasGetJobStatusResult(DaasResult):
@@ -82,23 +71,22 @@ class DaasGetJobStatusResult(DaasResult):
8271
status (dict): the status of the job
8372
state (str): the state of the job (from the status object)
8473
"""
85-
86-
74+
8775
def __init__(self, call_result: dict):
8876
super().__init__(call_result)
89-
self.id = self.job['id']
90-
self.status = self.job.get("status", {'state': 'FAILED'})
77+
self.id = self.job["id"]
78+
self.status = self.job.get("status", {"state": "FAILED"})
9179
self.state = self.status.get("state", "FAILED")
80+
9281
def has_finished(self):
93-
if self.state == 'DONE':
82+
if self.state == "DONE":
9483
return True
95-
elif self.state != 'FAILED':
84+
elif self.state != "FAILED":
9685
return False
97-
elif self.state == 'FAILED' and self.errors and len(self.errors) > 0:
86+
elif self.state == "FAILED" and self.errors and len(self.errors) > 0:
9887
return False
9988
else:
10089
raise Exception("got to failed state with no error, please reach out.")
101-
10290

10391

10492
class DaasGetQueryResult(DaasResult):
@@ -109,12 +97,9 @@ class DaasGetQueryResult(DaasResult):
10997
rows (list): the rows including the data
11098
pageToken (str): the token of the page
11199
"""
112-
113-
100+
114101
def __init__(self, call_result: dict):
115102
super().__init__(call_result)
116-
self.total_rows = self.job.get('totalRows', None)
117-
self.rows = self.job.get('rows', None)
118-
self.page_token = self.job.get('pageToken', None)
119-
120-
103+
self.total_rows = self.job.get("totalRows", None)
104+
self.rows = self.job.get("rows", None)
105+
self.page_token = self.job.get("pageToken", None)

mygeotab/altitude/wrapper.py

Lines changed: 12 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -49,13 +49,14 @@ def __init__(
4949
proxies=proxies,
5050
cert=cert,
5151
)
52-
_ = logging.basicConfig(stream=sys.stdout, level=logging.INFO, format='%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s', datefmt='%Y-%m-%d %H:%M:%S')
53-
54-
52+
_ = logging.basicConfig(
53+
stream=sys.stdout,
54+
level=logging.INFO,
55+
format="%(asctime)s.%(msecs)03d %(levelname)-8s %(message)s",
56+
datefmt="%Y-%m-%d %H:%M:%S",
57+
)
5558

56-
def _call_api(
57-
self, service_name: str, function_name: str, function_parameters: dict
58-
) -> dict:
59+
def _call_api(self, service_name: str, function_name: str, function_parameters: dict) -> dict:
5960
results = self.call(
6061
method="GetBigDataResults",
6162
serviceName=service_name,
@@ -64,12 +65,10 @@ def _call_api(
6465
)
6566
return results
6667

67-
68-
69-
def call_api(self, function_name: str, params: dict) -> dict :
70-
'''
68+
def call_api(self, function_name: str, params: dict) -> dict:
69+
"""
7170
Supports getJobStatus calls, and getQueryResults calls. Retries in case of errors like connection rest.
72-
'''
71+
"""
7372
assert function_name in ["getJobStatus", "getQueryResults", "createQueryJob"]
7473
max_tries = 5
7574
for try_index in range(max_tries):
@@ -87,7 +86,7 @@ def call_api(self, function_name: str, params: dict) -> dict :
8786
raise e
8887
else:
8988
print(f"encountered error trying to parse to api response {str(call_result)}, retrying....")
90-
time.sleep((try_index + 1)*10)
89+
time.sleep((try_index + 1) * 10)
9190
else:
9291
raise e
9392

@@ -139,8 +138,6 @@ def wait_for_job_to_complete(self, params: dict) -> dict:
139138
raise e
140139
return daas_status.job
141140

142-
143-
144141
def fetch_data(self, params: dict) -> dict:
145142
"""
146143
fetch data for the given params. jobId needs to be included in params.
@@ -194,7 +191,7 @@ def get_data(self, params: dict) -> list:
194191
logging.error(f"error: error while combining data:: {e}")
195192
raise e
196193
return data
197-
194+
198195
def do(self, params: dict) -> list:
199196
"""
200197
given the parameters, will call the request, wait on it to finish and return the combined data.
@@ -210,13 +207,3 @@ def do(self, params: dict) -> list:
210207
data = self.get_data(params)
211208
logging.info(f"data gathered")
212209
return data
213-
214-
215-
216-
217-
218-
219-
220-
221-
222-

mygeotab/api.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,10 +351,12 @@ class GeotabHTTPAdapter(HTTPAdapter):
351351

352352
def init_poolmanager(self, connections, maxsize, block=False, **pool_kwargs):
353353
ssl_context = create_urllib3_context(ssl_version=ssl.PROTOCOL_TLS)
354+
ssl_context.load_default_certs()
354355
ssl_context.options |= ssl.OP_NO_SSLv2
355356
ssl_context.options |= ssl.OP_NO_SSLv3
356357
ssl_context.options |= ssl.OP_NO_TLSv1
357358
ssl_context.options |= ssl.OP_NO_TLSv1_1
359+
ssl_context.options |= ssl.OP_ENABLE_MIDDLEBOX_COMPAT
358360
self.poolmanager = urllib3.poolmanager.PoolManager(
359361
num_pools=connections, maxsize=maxsize, block=block, ssl_context=ssl_context, **pool_kwargs
360362
)

mygeotab/api_async.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,10 +203,12 @@ async def _query(server, method, parameters, timeout=DEFAULT_TIMEOUT, verify_ssl
203203
ssl_context = False
204204
if verify_ssl or cert:
205205
ssl_context = ssl.SSLContext(ssl.PROTOCOL_TLS)
206+
ssl_context.load_default_certs()
206207
ssl_context.options |= ssl.OP_NO_SSLv2
207208
ssl_context.options |= ssl.OP_NO_SSLv3
208209
ssl_context.options |= ssl.OP_NO_TLSv1
209210
ssl_context.options |= ssl.OP_NO_TLSv1_1
211+
ssl_context.options |= ssl.OP_ENABLE_MIDDLEBOX_COMPAT
210212
if cert:
211213
if isinstance(cert, str):
212214
ssl_context.load_cert_chain(cert)

setup.py

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,15 @@
4242
packages=packages,
4343
package_data={"": ["LICENSE"]},
4444
license="Apache 2.0",
45-
install_requires=["requests", "urllib3", "click", "pytz", "arrow", "aiohttp>=3.8.1,<4", "python-rapidjson"],
45+
install_requires=[
46+
"requests",
47+
"urllib3",
48+
"click",
49+
"pytz",
50+
"arrow",
51+
"aiohttp>=3.8.1,<4",
52+
"python-rapidjson",
53+
],
4654
setup_requires=["wheel"],
4755
entry_points="""
4856
[console_scripts]

0 commit comments

Comments
 (0)