Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
moved network functions into their own file - part of splitting up a …
Browse files Browse the repository at this point in the history
…large file
  • Loading branch information
mahtin committed Jun 24, 2020
1 parent dd5e51f commit 5f3a974
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 57 deletions.
69 changes: 12 additions & 57 deletions CloudFlare/cloudflare.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import json
import requests

from .network import CFnetwork
from .logging_helper import CFlogger
from .utils import user_agent, sanitize_secrets
from .read_configs import read_configs
Expand Down Expand Up @@ -41,7 +42,7 @@ def __init__(self, config):
self.raw = config['raw']
self.use_sessions = config['use_sessions']
self.profile = config['profile']
self.session = None
self.network = CFnetwork(use_sessions=self.use_sessions)
self.user_agent = user_agent()

if 'debug' in config and config['debug']:
Expand Down Expand Up @@ -155,53 +156,7 @@ def call_with_certauth(self, method, parts,
identifier1, identifier2, identifier3,
params, data, files)

def _connection(self, method, url, headers=None, params=None, data=None, files=None):
""" Cloudflare v4 API"""

if self.use_sessions:
if self.session is None:
self.session = requests.Session()
else:
self.session = requests

method = method.upper()

if method == 'GET':
return self.session.get(url,
headers=headers, params=params, data=data)
if method == 'POST':
if isinstance(data, str):
return self.session.post(url,
headers=headers, params=params, data=data, files=files)
else:
return self.session.post(url,
headers=headers, params=params, json=data, files=files)
if method == 'PUT':
if isinstance(data, str):
return self.session.put(url,
headers=headers, params=params, data=data)
else:
return self.session.put(url,
headers=headers, params=params, json=data)
if method == 'DELETE':
if isinstance(data, str):
return self.session.delete(url,
headers=headers, params=params, data=data)
else:
return self.session.delete(url,
headers=headers, params=params, json=data)
if method == 'PATCH':
if isinstance(data, str):
return self.session.request('PATCH', url,
headers=headers, params=params, data=data)
else:
return self.session.request('PATCH', url,
headers=headers, params=params, json=data)

# should never happen
raise CloudFlareAPIError(0, 'method not supported')

def _network(self, method, headers, parts,
def _call_network(self, method, headers, parts,
identifier1=None, identifier2=None, identifier3=None,
params=None, data=None, files=None):
""" Cloudflare v4 API"""
Expand Down Expand Up @@ -258,7 +213,7 @@ def _network(self, method, headers, parts,
try:
if self.logger:
self.logger.debug('Call: doit!')
response = self._connection(method, url, headers, params, data, files)
response = self.network(method, url, headers, params, data, files)
if self.logger:
self.logger.debug('Call: done!')
except Exception as e:
Expand Down Expand Up @@ -335,13 +290,13 @@ def _raw(self, method, headers, parts,
params=None, data=None, files=None):
""" Cloudflare v4 API"""

[response_type, response_code, response_data] = self._network(method,
headers, parts,
identifier1,
identifier2,
identifier3,
params, data, files)

[response_type, response_code, response_data] = self._call_network(method,
headers, parts,
identifier1,
identifier2,
identifier3,
params, data, files)
if response_type == 'application/json':
# API says it's JSON; so it better be parsable as JSON
# NDJSON is returned by Enterprise Log Share i.e. /zones/:id/logs/received
Expand Down Expand Up @@ -607,7 +562,7 @@ def _api_from_web(self):
try:
if self.logger:
self.logger.debug('Call: doit!')
response = self._connection("GET", url)
response = self.network.do__connection("GET", url)
if self.logger:
self.logger.debug('Call: done!')
except Exception as e:
Expand Down
68 changes: 68 additions & 0 deletions CloudFlare/network.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
""" Network for Cloudflare API"""
from __future__ import absolute_import

import requests

from .exceptions import CloudFlareAPIError

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

def __init__(self, use_sessions=True):
""" Network for Cloudflare API"""

self.use_sessions = use_sessions
self.session = None

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

if self.use_sessions:
if self.session is None:
self.session = requests.Session()
else:
self.session = requests

method = method.upper()

if method == 'GET':
return self.session.get(url,
headers=headers, params=params, data=data)
if method == 'POST':
if isinstance(data, str):
return self.session.post(url,
headers=headers, params=params, data=data, files=files)
else:
return self.session.post(url,
headers=headers, params=params, json=data, files=files)
if method == 'PUT':
if isinstance(data, str):
return self.session.put(url,
headers=headers, params=params, data=data)
else:
return self.session.put(url,
headers=headers, params=params, json=data)
if method == 'DELETE':
if isinstance(data, str):
return self.session.delete(url,
headers=headers, params=params, data=data)
else:
return self.session.delete(url,
headers=headers, params=params, json=data)
if method == 'PATCH':
if isinstance(data, str):
return self.session.request('PATCH', url,
headers=headers, params=params, data=data)
else:
return self.session.request('PATCH', url,
headers=headers, params=params, json=data)

# should never happen
raise CloudFlareAPIError(0, 'method not supported')

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

if self.use_sessions and self.session:
self.session.close()
self.session = None

0 comments on commit 5f3a974

Please sign in to comment.