Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Lots more documentation, some more minor code cleanup. Starting to ma…

…ke more sense now.
  • Loading branch information...
commit 4c9321a07ac83e49340718d671c21d79a154d8ec 1 parent 9d2f14d
@gtaylor gtaylor authored committed
View
2  paypal/sdk/__init__.py
@@ -1,3 +1,3 @@
-# coding=utf-8
+#coding=utf-8
from interface import PayPalInterface
View
21 paypal/sdk/interface.py
@@ -63,7 +63,7 @@ def _call(self, method, **kwargs):
"""
socket.setdefaulttimeout(self.config.HTTP_TIMEOUT)
- urlvalues = {
+ url_values = {
'METHOD': method,
'VERSION': self.config.API_VERSION
}
@@ -73,26 +73,27 @@ def _call(self, method, **kwargs):
# headers['X-PAYPAL-SECURITY-USERID'] = API_USERNAME
# headers['X-PAYPAL-SECURITY-PASSWORD'] = API_PASSWORD
# headers['X-PAYPAL-SECURITY-SIGNATURE'] = API_SIGNATURE
- urlvalues['USER'] = self.config.API_USERNAME
- urlvalues['PWD'] = self.config.API_PASSWORD
- urlvalues['SIGNATURE'] = self.config.API_SIGNATURE
+ url_values['USER'] = self.config.API_USERNAME
+ url_values['PWD'] = self.config.API_PASSWORD
+ url_values['SIGNATURE'] = self.config.API_SIGNATURE
elif(self.config.API_AUTHENTICATION_MODE == "UNIPAY"):
# headers['X-PAYPAL-SECURITY-SUBJECT'] = SUBJECT
- urlvalues['SUBJECT'] = self.config.SUBJECT
+ url_values['SUBJECT'] = self.config.SUBJECT
# headers['X-PAYPAL-REQUEST-DATA-FORMAT'] = 'NV'
# headers['X-PAYPAL-RESPONSE-DATA-FORMAT'] = 'NV'
# print(headers)
for k,v in kwargs.iteritems():
- urlvalues[k.upper()] = v
-
+ url_values[k.upper()] = v
+
+ # When in DEBUG level 2 or greater, print out the NVP pairs.
if self.config.DEBUG_LEVEL >= 2:
- k = urlvalues.keys()
+ k = url_values.keys()
k.sort()
for i in k:
- print " %-20s : %s" % (i , urlvalues[i])
+ print " %-20s : %s" % (i , url_values[i])
- u2 = self._encode_utf8( **urlvalues )
+ u2 = self._encode_utf8(**url_values)
data = urllib.urlencode(u2)
req = urllib2.Request(self.config.API_ENDPOINT, data, headers)
View
26 paypal/sdk/response.py
@@ -9,9 +9,20 @@
class PayPalResponse(object):
"""
- Parse and prepare the reponse from PayPal's API.
+ Parse and prepare the reponse from PayPal's API. Acts as somewhat of a
+ glorified dictionary for API responses.
+
+ NOTE: Don't access self.raw directly. Just do something like
+ PayPalResponse.someattr, going through PayPalResponse.__getattr__().
"""
def __init__(self, query_string, config):
+ """
+ query_string is the response from the API, in NVP format. This is
+ parseable by urlparse.parse_qs(), which sticks it into the self.raw
+ dict for retrieval by the user.
+ """
+ # A dict of NVP values. Don't access this directly, use
+ # PayPalResponse.attribname instead. See self.__getattr__().
self.raw = parse_qs(query_string)
self.config = config
@@ -19,6 +30,11 @@ def __str__(self):
return str(self.raw)
def __getattr__(self, key):
+ """
+ Handles the retrieval of attributes that don't exist on the object
+ already. This is used to get API response values.
+ """
+ # PayPal response names are always uppercase.
key = key.upper()
try:
value = self.raw[key]
@@ -26,11 +42,11 @@ def __getattr__(self, key):
return value[0]
return value
except KeyError:
- if self.config.KEY_ERROR == None:
- return None
- else:
+ if self.config.KEY_ERROR:
raise AttributeError(self)
-
+ else:
+ return None
+
def success(self):
"""
Checks for the presence of errors in the response. Returns True if
View
3  paypal/sdk/settings.py
@@ -65,6 +65,9 @@ class PayPalConfig(object):
RESPONSE_KEYERROR = "AttributeError"
+ # When True, return an AttributeError when the user tries to get an
+ # attribute on the response that does not exist. If False or None,
+ # return None for non-existant attribs.
KEY_ERROR = True
def __init__(self, **kwargs):
Please sign in to comment.
Something went wrong with that request. Please try again.