-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move the request wrappers to a separate class
- Loading branch information
1 parent
24ca865
commit 443a019
Showing
2 changed files
with
73 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import requests | ||
import os | ||
|
||
|
||
# ---------------------------------------------------------------------------- | ||
# Wrappers around the requests for better error handling | ||
class Wrappers(): | ||
|
||
def post_request(self, post_data, path, headers): | ||
proxy_con = os.getenv('PROXY_CON') | ||
proxy_url = os.getenv('PROXY_URL') | ||
if proxy_con and proxy_url: | ||
proxies = {proxy_con: proxy_url} | ||
elif self.get_setting('PROXY_CON') != '' and self.get_setting('PROXY_URL') != '': | ||
proxies = {self.get_setting('PROXY_CON'): self.get_setting('PROXY_URL')} | ||
else: | ||
proxies = {} | ||
try: | ||
response = requests.post(path, | ||
verify=False, | ||
proxies=proxies, | ||
data=post_data, | ||
timeout=5, | ||
headers=headers | ||
) | ||
except Exception as e: | ||
self.status_code = e.args | ||
raise ConnectionError | ||
if response.status_code != 200: | ||
self.status_code = response.status_code | ||
self.message = response.content | ||
return (response) | ||
self.status_code = 200 | ||
self.message = 'OK' | ||
return (response) | ||
|
||
def get_request(self, path, headers): | ||
proxy_con = os.getenv('PROXY_CON') | ||
proxy_url = os.getenv('PROXY_URL') | ||
if proxy_con and proxy_url: | ||
proxies = {proxy_con: proxy_url} | ||
elif self.get_setting('PROXY_CON') != '' and self.get_setting('PROXY_URL') != '': | ||
proxies = {self.get_setting('PROXY_CON'): self.get_setting('PROXY_URL')} | ||
else: | ||
proxies = {} | ||
try: | ||
response = requests.get(path, | ||
verify=False, | ||
proxies=proxies, | ||
timeout=5, | ||
headers=headers | ||
) | ||
except Exception as e: | ||
self.status_code = e.args | ||
raise ConnectionError | ||
if response.status_code != 200: | ||
self.status_code = response.status_code | ||
self.message = response.content | ||
return (response) | ||
self.status_code = 200 | ||
self.message = 'OK' | ||
return (response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters