Skip to content

Commit

Permalink
moved all requests code/exceptions into network where it belongs, imp…
Browse files Browse the repository at this point in the history
…ort cleanup, exception handling cleanup
  • Loading branch information
mahtin committed Feb 27, 2024
1 parent dbaf7d6 commit e7cc964
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 32 deletions.
21 changes: 6 additions & 15 deletions CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
""" Cloudflare v4 API"""
import json
import keyword
from requests import RequestException as requests_RequestException, ConnectionError as requests_ConnectionError, exceptions as requests_exceptions

from .network import CFnetwork
from .network import CFnetwork, CFnetworkError
from .logging_helper import CFlogger
from .utils import user_agent, build_curl
from .read_configs import read_configs, ReadConfigError
Expand Down Expand Up @@ -299,22 +298,14 @@ def _call_network(self, method, headers, parts, identifiers, params, data_str, d

try:
response = self.network(method, url, headers, params, data_str, data_json, files)
except requests_ConnectionError as e:
except CFnetworkError as e:
if self.logger:
self.logger.debug('Call: requests connection exception! "%s"', e)
raise CloudFlareAPIError(0, 'connection error') from None
except requests_exceptions.Timeout as e:
if self.logger:
self.logger.debug('Call: requests timeout exception! "%s"', e)
raise CloudFlareAPIError(0, 'connection timeout') from None
except requests_RequestException as e:
if self.logger:
self.logger.debug('Call: requests exception! "%s"', e)
raise CloudFlareAPIError(0, e) from None
self.logger.debug('Call: network error: %s', e)
raise CloudFlareAPIError(0, str(e)) from None
except Exception as e:
if self.logger:
self.logger.debug('Call: exception! "%s"', e)
raise
self.logger.debug('Call: network exception! %s', e)
raise CloudFlareAPIError(0, 'network exception: %s' % (e)) from None

# Create response_{type|code|data}
try:
Expand Down
46 changes: 29 additions & 17 deletions CloudFlare/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,52 @@

from urllib.parse import urlparse

import requests
from requests import Session, RequestException, ConnectionError
from requests.exceptions import Timeout
from requests.adapters import HTTPAdapter

from .exceptions import CloudFlareAPIError
class CFnetworkError(Exception):
""" errors for network calls """

class CFnetwork():
"""Network for Cloudflare API"""
""" CFnetwork """

def __init__(
self, use_sessions=True, global_request_timeout=5, max_request_retries=5
):
"""Network for Cloudflare API"""
def __init__(self, use_sessions=True, global_request_timeout=5, max_request_retries=5):
""" CFnetwork """

self.use_sessions = use_sessions
self.global_request_timeout = global_request_timeout
self.max_request_retries = max_request_retries
self.session = None

def __call__(self, method, url, headers=None, params=None, data_str=None, data_json=None, files=None):
"""Network for Cloudflare API"""
""" __call__ """

if self.use_sessions:
if self.session is None:
s = requests.Session()
s = Session()
if self.max_request_retries is not None:
hostname = urlparse(url).netloc
s.mount(
f"https://{hostname}",
HTTPAdapter(max_retries=self.max_request_retries),
)
prefix = 'https://%s' % (urlparse(url).netloc),
s.mount(prefix, HTTPAdapter(max_retries=self.max_request_retries))
self.session = s
else:
# only now do we import all of requests ... it's a rare case
import requests
self.session = requests

try:
r = self._do_network(method, url, headers, params, data_str, data_json, files)
except RequestException as e:
raise CFnetworkError('network request exception error: %s' % (e)) from None
except Timeout as e:
raise CFnetworkError('network request timeout error: %s' % (e)) from None
except ConnectionError as e:
raise CFnetworkError('network request connection error: %s' % (e)) from None

return r

def _do_network(self, method, url, headers, params, data_str, data_json, files):
""" _do_network """
method = method.upper()

# https://docs.python-requests.org/en/latest/user/quickstart/#post-a-multipart-encoded-file
Expand Down Expand Up @@ -91,12 +103,12 @@ def __call__(self, method, url, headers=None, params=None, data_str=None, data_j
)
else:
# should never happen
raise CloudFlareAPIError(0, 'method not supported')

raise CFnetworkError('internal error - http method invalid: %s' % (method))
# success!
return r

def __del__(self):
"""Network for Cloudflare API"""
""" __del__ """

if self.use_sessions and self.session:
self.session.close()
Expand Down

0 comments on commit e7cc964

Please sign in to comment.