Skip to content
This repository has been archived by the owner on May 12, 2020. It is now read-only.

Commit

Permalink
Merge pull request #32 from thefourtheye/patch-2
Browse files Browse the repository at this point in the history
Changes looks good now. Merging.
  • Loading branch information
avidas committed Feb 1, 2014
2 parents 5663649 + 8382eab commit 95e6929
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 30 deletions.
39 changes: 14 additions & 25 deletions paypalrestsdk/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@ class Api:
# api = paypalrestsdk.Api( mode="sandbox",
# client_id='CLIENT_ID', client_secret='CLIENT_SECRET', ssl_options={} )
def __init__(self, options=None, **args):
options = options or {}
args = util.merge_dict(options, args)
args = util.merge_dict(options or {}, args)

self.mode = args.get("mode", "sandbox")
self.endpoint = args.get("endpoint", self.default_endpoint())
self.token_endpoint = args.get("token_endpoint", self.endpoint)
self.client_id = args.get("client_id")
self.client_secret = args.get("client_secret")
self.client_id = args["client_id"] # Mandatory parameter, so not using `dict.get`
self.client_secret = args["client_secret"] # Mandatory parameter, so not using `dict.get`
self.ssl_options = args.get("ssl_options", {})

self.token_hash = None
self.token_request_at = None

if args.get("token"):
self.token_hash = {"access_token": args.get("token"), "token_type": "Bearer"}
self.token_hash = {"access_token": args["token"], "token_type": "Bearer"}

self.options = args

Expand Down Expand Up @@ -90,8 +90,7 @@ def get_token_type(self):
# api.request("https://api.sandbox.paypal.com/v1/payments/payment?count=10", "GET", {})
# api.request("https://api.sandbox.paypal.com/v1/payments/payment", "POST", "{}", {} )
def request(self, url, method, body=None, headers=None):
headers = headers or {}
http_headers = util.merge_dict(self.headers(), headers)
http_headers = util.merge_dict(self.headers(), headers or {})

if http_headers.get('PayPal-Request-Id'):
logging.info('PayPal-Request-Id: %s' % (http_headers['PayPal-Request-Id']))
Expand All @@ -103,7 +102,7 @@ def request(self, url, method, body=None, headers=None):
except BadRequest as error:
return {"error": json.loads(error.content)}

# Handle Exipre token
# Handle Expired token
except UnauthorizedAccess as error:
if(self.token_hash and self.client_id):
self.token_hash = None
Expand All @@ -127,10 +126,7 @@ def handle_response(self, response, content):
if status in (301, 302, 303, 307):
raise Redirection(response, content)
elif 200 <= status <= 299:
if content:
return json.loads(content)
else:
return {}
return json.loads(content) if content else {}
elif status == 400:
raise BadRequest(response, content)
elif status == 401:
Expand All @@ -147,9 +143,9 @@ def handle_response(self, response, content):
raise ResourceGone(response, content)
elif status == 422:
raise ResourceInvalid(response, content)
elif status >= 401 and status <= 499:
elif 401 <= status <= 499:
raise ClientError(response, content)
elif status >= 500 and status <= 599:
elif 500 <= status <= 599:
raise ServerError(response, content)
else:
raise ConnectionError(response, content, "Unknown response code: #{response.code}")
Expand All @@ -168,28 +164,22 @@ def headers(self):
# api.get("v1/payments/payment?count=1")
# api.get("v1/payments/payment/PAY-1234")
def get(self, action, headers=None):
headers = headers or {}
return self.request(util.join_url(self.endpoint, action), 'GET', headers=headers)
return self.request(util.join_url(self.endpoint, action), 'GET', headers=headers or {})

# Make POST request
# == Example
# api.post("v1/payments/payment", { 'indent': 'sale' })
# api.post("v1/payments/payment/PAY-1234/execute", { 'payer_id': '1234' })
def post(self, action, params=None, headers=None):
params = params or {}
headers = headers or {}
return self.request(util.join_url(self.endpoint, action), 'POST', body=json.dumps(params), headers=headers)
return self.request(util.join_url(self.endpoint, action), 'POST', body=json.dumps(params or {}), headers=headers or {})

# Make DELETE request
def delete(self, action, headers=None):
headers = headers or {}
return self.request(util.join_url(self.endpoint, action), 'DELETE', headers=headers)
return self.request(util.join_url(self.endpoint, action), 'DELETE', headers=headers or {})


global __api__
__api__ = None


# Get default api
def default():
global __api__
Expand All @@ -206,9 +196,8 @@ def default():

# Create new default api object with given configuration
def set_config(options=None, **config):
options = options or {}
global __api__
__api__ = Api(options, **config)
__api__ = Api(options or {}, **config)
return __api__

configure = set_config
Expand Down
9 changes: 4 additions & 5 deletions test/unit_tests/api_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ def setUp(self):


def test_endpoint(self):

new_api = paypal.Api(mode="live")
new_api = paypal.Api(mode="live", client_id="dummy", client_secret="dummy")
self.assertEqual(new_api.endpoint, "https://api.paypal.com")
self.assertEqual(new_api.token_endpoint, "https://api.paypal.com")
new_api = paypal.Api(mode="sandbox")

new_api = paypal.Api(mode="sandbox", client_id="dummy", client_secret="dummy")
self.assertEqual(new_api.endpoint, "https://api.sandbox.paypal.com")
self.assertEqual(new_api.token_endpoint, "https://api.sandbox.paypal.com")

new_api = paypal.Api(endpoint="https://custom-endpoint.paypal.com")
new_api = paypal.Api(endpoint="https://custom-endpoint.paypal.com", client_id="dummy", client_secret="dummy")
self.assertEqual(new_api.endpoint, "https://custom-endpoint.paypal.com")
self.assertEqual(new_api.token_endpoint, "https://custom-endpoint.paypal.com")

Expand Down

0 comments on commit 95e6929

Please sign in to comment.