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

Demo of code to perform retries, internal to the Api object. #15

Merged
merged 1 commit into from
Aug 9, 2017
Merged
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
32 changes: 27 additions & 5 deletions namecheap.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
import time
import requests # pip install requests
from xml.etree.ElementTree import fromstring

Expand All @@ -15,6 +16,10 @@
}
NAMESPACE = "http://api.namecheap.com/xml.response"

# default values for the retry mechanism
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would like to rework those "retries" as "attempts" to replace following while True conditional with while attempts > 0, which looks more readable for me.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing! ... shall I provide additional commits to this PR, or do you have another preference?

(if you haven't guessed, I'm totally easy :-) ... and deferential to your design/goals for PyNamecheap)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh. Merged already ... 📦

DEFAULT_RETRY_COUNT = 0 # disabled
DEFAULT_RETRY_DELAY = 0.1 # in seconds


# https://www.namecheap.com/support/api/error-codes.aspx
class ApiError(Exception):
Expand All @@ -26,14 +31,19 @@ def __init__(self, number, text):

class Api(object):
# Follows API spec capitalization in variable names for consistency.
def __init__(self, ApiUser, ApiKey, UserName, ClientIP, sandbox=True, debug=True):
def __init__(self, ApiUser, ApiKey, UserName, ClientIP,
sandbox=True, debug=True,
retry_count=DEFAULT_RETRY_COUNT,
retry_delay=DEFAULT_RETRY_DELAY):
self.ApiUser = ApiUser
self.ApiKey = ApiKey
self.UserName = UserName
self.ClientIP = ClientIP
self.endpoint = ENDPOINTS['sandbox' if sandbox else 'production']
self.debug = debug
self.payload_limit = 10 # After hitting this lenght limit script will move payload from POST params to POST data
self.retry_count = retry_count
self.retry_delay = retry_delay

# https://www.namecheap.com/support/api/methods/domains/create.aspx
def domains_create(
Expand Down Expand Up @@ -90,10 +100,22 @@ def _payload(self, Command, extra_payload={}):

def _fetch_xml(self, payload, extra_payload = None):
"""Make network call and return parsed XML element"""
if extra_payload:
r = requests.post(self.endpoint, params=payload, data=extra_payload)
else:
r = requests.post(self.endpoint, params=payload)
retries_left = self.retry_count
while True:
if extra_payload:
r = requests.post(self.endpoint, params=payload, data=extra_payload)
else:
r = requests.post(self.endpoint, params=payload)
if 200 <= r.status_code <= 299:
break
if retries_left <= 0:
# Here we provide 0 error code which is not present in official docs
raise ApiError('0', 'Did not receive 200 (Ok) response')
if self.debug:
print('Received status %d ... retrying ...' % (r.status_code,))
time.sleep(self.retry_delay)
retries_left -= 1

if self.debug:
print("--- Request ---")
print(r.url)
Expand Down