Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add domains.getInfo endpoint & support proxying API requests #36

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions namecheap.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ class Api(object):
def __init__(self, ApiUser, ApiKey, UserName, ClientIP,
sandbox=True, debug=True,
attempts_count=DEFAULT_ATTEMPTS_COUNT,
attempts_delay=DEFAULT_ATTEMPTS_DELAY):
attempts_delay=DEFAULT_ATTEMPTS_DELAY,
proxies=None):
self.ApiUser = ApiUser
self.ApiKey = ApiKey
self.UserName = UserName
Expand All @@ -44,6 +45,7 @@ def __init__(self, ApiUser, ApiKey, UserName, ClientIP,
self.payload_limit = 10 # After hitting this lenght limit script will move payload from POST params to POST data
self.attempts_count = attempts_count
self.attempts_delay = attempts_delay
self.proxies = proxies

# https://www.namecheap.com/support/api/methods/domains/create.aspx
def domains_create(
Expand Down Expand Up @@ -108,10 +110,12 @@ def _fetch_xml(self, payload, extra_payload = None):
"""Make network call and return parsed XML element"""
attempts_left = self.attempts_count
while attempts_left > 0:
kwargs = {'params': payload}
if extra_payload:
r = requests.post(self.endpoint, params=payload, data=extra_payload)
else:
r = requests.post(self.endpoint, params=payload)
kwargs['data'] = extra_payload
if self.proxies:
kwargs['proxies'] = self.proxies
r = requests.post(self.endpoint, **kwargs)
if 200 <= r.status_code <= 299:
break
if attempts_left <= 1:
Expand Down Expand Up @@ -469,3 +473,21 @@ def domains_getList(self, ListType=None, SearchTerm=None, PageSize=None, SortBy=
extra_payload['SortBy'] = SortBy
payload, extra_payload = self._payload('namecheap.domains.getList', extra_payload)
return self.LazyGetListIterator(self, payload)

def domains_getInfo(self, DomainName, HostName=None):
"""Returns information about the requested domain, for example
{
'Status': 'Ok',
'Id': '736542',
'DomainName': 'domain1.com',
'OwnerName': 'apiuser',
'IsOwner': 'true',
'IsPremium': 'true'
}"""
extra_payload = {'DomainName': DomainName}
if HostName:
extra_payload['HostName'] = HostName

xml = self._call('namecheap.domains.getInfo', extra_payload)
xpath = './/{%(ns)s}CommandResponse/{%(ns)s}DomainGetInfoResult' % {'ns': NAMESPACE}
return xml.find(xpath).attrib