This repository has been archived by the owner on May 7, 2024. It is now read-only.
forked from cloudflare/python-cloudflare
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
moved network functions into their own file - part of splitting up a …
…large file
- Loading branch information
Showing
2 changed files
with
80 additions
and
57 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
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,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 |