Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __request_helper(request):
"""Handles firing off requests and exception raising."""
try:
session = Session()
handle = session.send(request)
except ConnectionError:
raise DNSimpleException('Failed to reach a server.')
except HTTPError:
raise DNSimpleException('Invalid response.')
# 204 code means no content
if handle.status_code == 204:
# Small patch for second param
return {}, None, None
response = handle.json()
if 400 <= handle.status_code:
raise DNSimpleException(response)
return response['data'], handle.headers, response.get('pagination')
module.fail_json(msg="Missing the following records: %s" % difference)
else:
module.exit_json(changed=False)
elif state == 'absent':
difference = list(set(wanted_records) & set(current_records))
if difference:
if not module.check_mode:
for rid in difference:
client.delete_record(str(domain), rid)
module.exit_json(changed=True)
else:
module.exit_json(changed=False)
else:
module.fail_json(msg="'%s' is an unknown value for the state argument" % state)
except DNSimpleException:
e = get_exception()
module.fail_json(msg="Unable to contact DNSimple: %s" % e.message)
module.fail_json(msg="Unknown what you wanted me to do")
module.fail_json(msg="Missing the following records: %s" % difference)
else:
module.exit_json(changed=False)
# state is absent
else:
difference = list(set(wanted_records) & set(current_records))
if difference:
if not module.check_mode:
for rid in difference:
client.delete_record(str(domain), rid)
module.exit_json(changed=True)
else:
module.exit_json(changed=False)
except DNSimpleException as e:
module.fail_json(msg="Unable to contact DNSimple: %s" % e.message)
module.fail_json(msg="Unknown what you wanted me to do")
if self._account_id is None:
data = self.__rest_helper('/whoami', account_link=False)
account_info = data['account']
# This means that user use user token
if account_info is None:
data = self.__rest_helper('/accounts', account_link=False)
if len(data) == 1:
self._account_id = data[0]['id']
elif self.user_account_id is None:
raise DNSimpleException('Found many accounts for this user access token. Specify account_id.')
else:
ids = [el['id'] for el in data]
if self.user_account_id in ids:
self._account_id = self.user_account_id
else:
raise DNSimpleException('Account {} not found. Possible variants: {}'.format(self._account_id,
', '.join(ids)))
else:
self._account_id = account_info['id']
return self._account_id
except ConnectionError:
raise DNSimpleException('Failed to reach a server.')
except HTTPError:
raise DNSimpleException('Invalid response.')
# 204 code means no content
if handle.status_code == 204:
# Small patch for second param
return {}, None, None
response = handle.json()
if 400 <= handle.status_code:
raise DNSimpleException(response)
return response['data'], handle.headers, response.get('pagination')
def account_id(self):
if self._account_id is None:
data = self.__rest_helper('/whoami', account_link=False)
account_info = data['account']
# This means that user use user token
if account_info is None:
data = self.__rest_helper('/accounts', account_link=False)
if len(data) == 1:
self._account_id = data[0]['id']
elif self.user_account_id is None:
raise DNSimpleException('Found many accounts for this user access token. Specify account_id.')
else:
ids = [el['id'] for el in data]
if self.user_account_id in ids:
self._account_id = self.user_account_id
else:
raise DNSimpleException('Account {} not found. Possible variants: {}'.format(self._account_id,
', '.join(ids)))
else:
self._account_id = account_info['id']
return self._account_id
def register(self, domain_name, registrant_id=None):
"""
Register a domain name with DNSimple and the appropriate
domain registry.
"""
if not registrant_id:
# Get the registrant ID from the first domain in the account
try:
registrant_id = self.getdomains()[0]['domain']['registrant_id']
except Exception:
raise DNSimpleException('Could not find registrant_id! Please specify manually.')
data = {
'name': domain_name,
'registrant_id': registrant_id
}
return self.__rest_helper('/domain_registrations', data=data, method='POST')
def __request_helper(request):
"""Handles firing off requests and exception raising."""
try:
session = Session()
handle = session.send(request)
except ConnectionError:
raise DNSimpleException('Failed to reach a server.')
except HTTPError:
raise DNSimpleException('Invalid response.')
# 204 code means no content
if handle.status_code == 204:
# Small patch for second param
return {}, None, None
response = handle.json()
if 400 <= handle.status_code:
raise DNSimpleException(response)
return response['data'], handle.headers, response.get('pagination')
from requests.auth import AuthBase, HTTPBasicAuth
except ImportError:
pass # Issues with setup.py needed to import module but the `request` dependency hasn't been installed yet.
except NameError:
pass # Issues with setup.py needed to import module but the `request.auth` dependency hasn't been installed yet.
__version__ = get_versions()['version']
class DNSimpleException(Exception):
"""
The main exception class for all exceptions we raise
"""
pass
class DNSimpleAuthException(DNSimpleException):
"""
Only raise this on authentication issues
"""
pass
class DNSimpleTokenAuth(AuthBase):
"""
Define DNSimple's token based auth
https://developer.dnsimple.com/v2/#authentication
"""
def __init__(self, token):
self.token = token
def __eq__(self, other):
return all([