Skip to content

Commit

Permalink
Made Client raise a custom exception if the Response had an error sta…
Browse files Browse the repository at this point in the history
…tus code.
  • Loading branch information
MouaYing committed Sep 21, 2016
1 parent cdf9405 commit 50667e9
Show file tree
Hide file tree
Showing 12 changed files with 113 additions and 20 deletions.
8 changes: 6 additions & 2 deletions examples/us_street_multiple_addresses_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from smartystreets_python_sdk import StaticCredentials
from smartystreets_python_sdk import StaticCredentials, exceptions
from smartystreets_python_sdk.us_street import ClientBuilder, Batch, Lookup


Expand Down Expand Up @@ -28,7 +28,11 @@ def run():

assert len(batch) == 4

client.send_batch(batch)
try:
client.send_batch(batch)
except exceptions.SmartyException as e:
print e.message
return

for i, lookup in enumerate(batch):
candidates = lookup.result
Expand Down
8 changes: 6 additions & 2 deletions examples/us_street_single_address_example.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from smartystreets_python_sdk import StaticCredentials
from smartystreets_python_sdk import StaticCredentials, exceptions
from smartystreets_python_sdk.us_street import ClientBuilder, Lookup


Expand All @@ -15,7 +15,11 @@ def run():
lookup.city = "Mountain View"
lookup.state = "CA"

client.send_lookup(lookup)
try:
client.send_lookup(lookup)
except exceptions.SmartyException as e:
print e.message
return

result = lookup.result

Expand Down
8 changes: 6 additions & 2 deletions examples/us_zipcode_multiple_lookups_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from smartystreets_python_sdk import StaticCredentials
from smartystreets_python_sdk import StaticCredentials, exceptions
from smartystreets_python_sdk.us_zipcode import ClientBuilder, Batch, Lookup


Expand All @@ -23,7 +23,11 @@ def run():

assert len(batch) == 3

client.send_batch(batch)
try:
client.send_batch(batch)
except exceptions.SmartyException as e:
print e.message
return

for i, lookup in enumerate(batch):
result = lookup.result
Expand Down
8 changes: 6 additions & 2 deletions examples/us_zipcode_single_lookup_example.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import os

from smartystreets_python_sdk import StaticCredentials
from smartystreets_python_sdk import StaticCredentials, exceptions
from smartystreets_python_sdk.us_zipcode import ClientBuilder, Lookup


Expand All @@ -15,7 +15,11 @@ def run():
lookup.city = "Mountain View"
lookup.state = "California"

client.send_lookup(lookup)
try:
client.send_lookup(lookup)
except exceptions.SmartyException as e:
print e.message
return

result = lookup.result
zipcodes = result.zipcodes
Expand Down
1 change: 1 addition & 0 deletions smartystreets_python_sdk/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
import errors
import us_street
import us_zipcode
import exceptions

__version__ = '0.0.0'
30 changes: 30 additions & 0 deletions smartystreets_python_sdk/exceptions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class SmartyException(Exception):
pass


class BadCredentialsError(SmartyException):
pass


class PaymentRequiredError(SmartyException):
pass


class RequestEntityTooLargeError(SmartyException):
pass


class BadRequestError(SmartyException):
pass


class TooManyRequestsError(SmartyException):
pass


class InternalServerError(SmartyException):
pass


class ServiceUnavailableError(SmartyException):
pass
5 changes: 4 additions & 1 deletion smartystreets_python_sdk/requests_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ def send(self, smarty_request):
request = build_request(smarty_request)
prepped_request = self.session.prepare_request(request)

response = self.session.send(prepped_request, timeout=self.max_timeout)
try:
response = self.session.send(prepped_request, timeout=self.max_timeout)
except Exception as e:
return Response(None, None, e)

return build_smarty_response(response)

Expand Down
4 changes: 2 additions & 2 deletions smartystreets_python_sdk/response.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Response:
def __init__(self, payload, status_code):
def __init__(self, payload, status_code, error=None):
self.payload = payload
self.status_code = status_code
self.status = None
self.error = error
2 changes: 2 additions & 0 deletions smartystreets_python_sdk/retry_sender.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,7 @@ def send(self, request):


def backoff(attempt):
print "There was an error processing the request. Retrying in {} seconds...".format(
min(attempt, RetrySender.MAX_BACKOFF_DURATION))
sleep(min(attempt, RetrySender.MAX_BACKOFF_DURATION))
return
53 changes: 44 additions & 9 deletions smartystreets_python_sdk/status_code_sender.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import errors
import exceptions


class StatusCodeSender:
Expand All @@ -8,16 +9,50 @@ def __init__(self, inner):
def send(self, request):
response = self.inner.send(request)

response.status = statuses[response.status_code]
if not response.error:
response.error = statuses.get(response.status_code)

return response

statuses = {200: "OK",
401: errors.BAD_CREDENTIALS,
402: errors.PAYMENT_REQUIRED,
413: errors.REQUEST_ENTITY_TOO_LARGE,
400: errors.BAD_REQUEST,
429: errors.TOO_MANY_REQUESTS,
500: errors.INTERNAL_SERVER_ERROR,
503: errors.SERVICE_UNAVAILABLE

def ok():
return None


def bad_credentials():
return exceptions.BadCredentialsError(errors.BAD_CREDENTIALS)


def payment_required():
return exceptions.PaymentRequiredError(errors.PAYMENT_REQUIRED)


def request_entity_too_large():
return exceptions.RequestEntityTooLargeError(errors.REQUEST_ENTITY_TOO_LARGE)


def bad_request():
return exceptions.BadRequestError(errors.BAD_REQUEST)


def too_many_requests():
return exceptions.TooManyRequestsError(errors.TOO_MANY_REQUESTS)


def internal_server_error():
return exceptions.InternalServerError(errors.INTERNAL_SERVER_ERROR)


def service_unavailable():
return exceptions.ServiceUnavailableError(errors.SERVICE_UNAVAILABLE)


statuses = {200: ok(),
401: bad_credentials(),
402: payment_required(),
413: request_entity_too_large(),
400: bad_request(),
429: too_many_requests(),
500: internal_server_error(),
503: service_unavailable()
}
3 changes: 3 additions & 0 deletions smartystreets_python_sdk/us_street/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def send_batch(self, batch):

response = self.sender.send(smartyrequest)

if response.error:
raise response.error

candidates = self.serializer.deserialize(response.payload)
if candidates is None:
candidates = []
Expand Down
3 changes: 3 additions & 0 deletions smartystreets_python_sdk/us_zipcode/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ def send_batch(self, batch):

response = self.sender.send(smartyrequest)

if response.error:
raise response.error

results = self.serializer.deserialize(response.payload)
if results is None:
results = []
Expand Down

0 comments on commit 50667e9

Please sign in to comment.