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

Changed over to requests from httplib2 #37

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 11 additions & 10 deletions paypalrestsdk/api.py
Expand Up @@ -2,7 +2,7 @@

import base64
import datetime
import httplib2
import requests
import json
import logging
import os
Expand All @@ -16,7 +16,7 @@
class Api:

# User-Agent for HTTP request
library_details = "httplib2 %s; python %s" % (httplib2.__version__, platform.python_version())
library_details = "requests %s; python %s" % (requests.__version__, platform.python_version())
user_agent = "PayPalSDK/rest-sdk-python %s (%s)" % (__version__, library_details)

def __init__(self, options=None, **args):
Expand All @@ -35,7 +35,7 @@ def __init__(self, options=None, **args):
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.proxies = args.get("proxies", None)
self.token_hash = None
self.token_request_at = None

Expand Down Expand Up @@ -64,7 +64,7 @@ def get_token_hash(self):
if self.token_hash is None:
self.token_request_at = datetime.datetime.now()
self.token_hash = self.http_call(util.join_url(self.token_endpoint, "/v1/oauth2/token"), "POST",
body="grant_type=client_credentials",
data="grant_type=client_credentials",
headers={
"Authorization": ("Basic %s" % self.basic_auth()),
"Content-Type": "application/x-www-form-urlencoded",
Expand Down Expand Up @@ -106,7 +106,7 @@ def request(self, url, method, body=None, headers=None):
logging.info('PayPal-Request-Id: %s' % (http_headers['PayPal-Request-Id']))

try:
return self.http_call(url, method, body=body, headers=http_headers)
return self.http_call(url, method, data=body, headers=http_headers)

# Format Error message for bad request
except BadRequest as error:
Expand All @@ -116,7 +116,7 @@ def request(self, url, method, body=None, headers=None):
except UnauthorizedAccess as error:
if(self.token_hash and self.client_id):
self.token_hash = None
return self.request(url, method, body, headers)
return self.request(url, method, data, headers)
else:
raise error

Expand All @@ -125,17 +125,18 @@ def http_call(self, url, method, **args):
Makes a http call. Logs response information.
"""
logging.info('Request[%s]: %s' % (method, url))
http = httplib2.Http(**self.ssl_options)
#http = httplib2.Http(**self.ssl_options)
start_time = datetime.datetime.now()
response, content = http.request(url, method, **args)
r = requests.request(method, url, proxies=self.proxies, **args)
response, content = r, r.content
duration = datetime.datetime.now() - start_time
logging.info('Response[%d]: %s, Duration: %s.%ss' % (response.status, response.reason, duration.seconds, duration.microseconds))
logging.info('Response[%d]: %s, Duration: %s.%ss' % (response.status_code, response.reason, duration.seconds, duration.microseconds))
return self.handle_response(response, content.decode('utf-8'))

def handle_response(self, response, content):
"""Validate HTTP response
"""
status = response.status
status = response.status_code
if status in (301, 302, 303, 307):
raise Redirection(response, content)
elif 200 <= status <= 299:
Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Expand Up @@ -5,6 +5,7 @@ docopt==0.6.1
httplib2==0.8
mock==1.0.1
nose==1.3.0
paypalrestsdk==0.7.0
Copy link
Contributor

Choose a reason for hiding this comment

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

this should not be in the requirements as it is a circular reference

requests==2.1.0
sh==1.09
six==1.5.2
2 changes: 1 addition & 1 deletion setup.py
Expand Up @@ -20,7 +20,7 @@
1. https://github.com/paypal/rest-api-sdk-python/ - README and Samples
2. https://developer.paypal.com/webapps/developer/docs/api/ - API Reference
""",
install_requires=['httplib2'],
install_requires=['requests'],
classifiers=[
'Intended Audience :: Developers',
'Natural Language :: English',
Expand Down