Skip to content

Commit 2aa7409

Browse files
committed
refactor: remove six dependency and update code for Python 3 compatibility
1 parent 74f61b3 commit 2aa7409

File tree

9 files changed

+28
-46
lines changed

9 files changed

+28
-46
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,10 @@ source code") for `payjp-python`, and then run:
1919

2020
Please see our official [documentation](https://pay.jp/docs/api).
2121

22-
## Dependencies
22+
## Dependencies
2323

2424
- requests
25-
- six
2625

2726
Install dependencies from using [pip](http://www.pip-installer.org/en/latest/):
28-
27+
2928
pip install -r requirements.txt

payjp/api_requestor.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
import platform
99
import time
1010
import random
11-
12-
from six import PY3
13-
from six.moves.urllib.parse import urlencode, urlsplit, urlunsplit
11+
from urllib.parse import urlencode, urlsplit, urlunsplit
1412

1513
import payjp
1614
from . import (
@@ -128,12 +126,9 @@ def request_raw(self, method, url, params=None, supplied_headers=None):
128126
val = '!! %s' % (e,)
129127
ua[attr] = val
130128

131-
if PY3:
132-
encoded_api_key = str(
133-
base64.b64encode(
134-
bytes(''.join([my_api_key, ':']), 'utf-8')), 'utf-8')
135-
else:
136-
encoded_api_key = base64.b64encode(''.join([my_api_key, ':']))
129+
encoded_api_key = str(
130+
base64.b64encode(
131+
bytes(''.join([my_api_key, ':']), 'utf-8')), 'utf-8')
137132

138133
headers = {
139134
'X-Payjp-Client-User-Agent': json.dumps(ua),
@@ -215,4 +210,3 @@ def _build_api_url(url, query):
215210
query = '%s&%s' % (base_query, query)
216211

217212
return urlunsplit((scheme, netloc, path, query, fragment))
218-

payjp/error.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(self, message=None, http_body=None, http_status=None,
1616
self.http_body = http_body
1717

1818
self.http_status = http_status
19-
self.json_body = json_body
19+
self.json_body = json_body
2020

2121

2222
class APIError(PayjpException):
@@ -33,7 +33,7 @@ def __init__(self, message, param, code, http_body=None,
3333
super(CardError, self).__init__(message,
3434
http_body, http_status, json_body)
3535
self.param = param
36-
self.code = code
36+
self.code = code
3737

3838

3939
class AuthenticationError(PayjpException):

payjp/resource.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,7 @@
33
import json
44
import logging
55
import sys
6-
7-
from six import string_types
8-
from six.moves.urllib.parse import quote_plus
6+
from urllib.parse import quote_plus
97

108
from payjp import api_requestor, error, util
119

@@ -34,7 +32,7 @@ def convert_to_payjp_object(resp, api_key, account, api_base=None):
3432
elif isinstance(resp, dict) and not isinstance(resp, PayjpObject):
3533
resp = resp.copy()
3634
klass_name = resp.get('object')
37-
if isinstance(klass_name, string_types):
35+
if isinstance(klass_name, str):
3836
klass = types.get(klass_name, PayjpObject)
3937
else:
4038
klass = PayjpObject
@@ -206,10 +204,10 @@ def request(self, method, url, params=None, headers=None):
206204
def __repr__(self):
207205
ident_parts = [type(self).__name__]
208206

209-
if isinstance(self.get('object'), string_types):
207+
if isinstance(self.get('object'), str):
210208
ident_parts.append(self.get('object'))
211209

212-
if isinstance(self.get('id'), string_types):
210+
if isinstance(self.get('id'), str):
213211
ident_parts.append('id=%s' % (self.get('id'),))
214212

215213
unicode_repr = '<%s at %s> JSON: %s' % (

payjp/test/helper.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
from mock import patch, Mock
1010

11-
from six import string_types
12-
1311
import payjp
1412

1513
NOW = datetime.datetime.now()
@@ -72,7 +70,7 @@ def assertRaisesRegexp(self, exception, regexp, callable, *args, **kwargs):
7270
if regexp is None:
7371
return True
7472

75-
if isinstance(regexp, string_types):
73+
if isinstance(regexp, str):
7674
regexp = re.compile(regexp)
7775
if not regexp.search(str(err)):
7876
raise self.failureException('"%s" does not match "%s"' %

payjp/test/test_integration.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import unittest
44
import payjp
55

6-
from six import string_types
7-
86
from payjp.test.helper import (PayjpTestCase, NOW, DUMMY_CARD)
97

108

@@ -17,7 +15,7 @@ def test_invalid_credentials(self):
1715
payjp.Customer.create()
1816
except payjp.error.AuthenticationError as e:
1917
self.assertEqual(401, e.http_status)
20-
self.assertTrue(isinstance(e.http_body, string_types))
18+
self.assertTrue(isinstance(e.http_body, str))
2119
self.assertTrue(isinstance(e.json_body, dict))
2220
finally:
2321
payjp.api_key = key
@@ -33,7 +31,7 @@ def test_invalid_card_props(self):
3331
payjp.Charge.create(amount=100, currency='jpy', card=EXPIRED_CARD)
3432
except payjp.error.InvalidRequestError as e:
3533
self.assertEqual(400, e.http_status)
36-
self.assertTrue(isinstance(e.http_body, string_types))
34+
self.assertTrue(isinstance(e.http_body, str))
3735
self.assertTrue(isinstance(e.json_body, dict))
3836

3937

@@ -44,15 +42,15 @@ def test_nonexistent_object(self):
4442
payjp.Charge.retrieve('invalid')
4543
except payjp.error.InvalidRequestError as e:
4644
self.assertEqual(404, e.http_status)
47-
self.assertTrue(isinstance(e.http_body, string_types))
45+
self.assertTrue(isinstance(e.http_body, str))
4846
self.assertTrue(isinstance(e.json_body, dict))
4947

5048
def test_invalid_data(self):
5149
try:
5250
payjp.Charge.create()
5351
except payjp.error.InvalidRequestError as e:
5452
self.assertEqual(400, e.http_status)
55-
self.assertTrue(isinstance(e.http_body, string_types))
53+
self.assertTrue(isinstance(e.http_body, str))
5654
self.assertTrue(isinstance(e.json_body, dict))
5755

5856

payjp/test/test_requestor.py

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@
33
import base64
44
import datetime
55
import unittest
6+
from urllib.parse import parse_qsl, urlsplit
67

78
from mock import Mock, MagicMock, patch
8-
from six.moves.urllib.parse import parse_qsl, urlsplit
9-
from six import PY3
109

1110
import payjp
1211

@@ -45,12 +44,9 @@ def __eq__(self, other):
4544
self._extra_match(other))
4645

4746
def _encode(self, api_key):
48-
if PY3:
49-
return str(
50-
base64.b64encode(
51-
bytes(''.join([api_key, ':']), 'utf-8')), 'utf-8')
52-
else:
53-
return base64.b64encode(''.join([api_key, ':']))
47+
return str(
48+
base64.b64encode(
49+
bytes(''.join([api_key, ':']), 'utf-8')), 'utf-8')
5450

5551
def _keys_match(self, other):
5652
expected_keys = self.EXP_KEYS + list(self.extra.keys())
@@ -413,7 +409,7 @@ def test_retry_disabled(self):
413409
with self.request_raw_patch:
414410
with self.assertRaises(payjp.error.APIError) as error:
415411
self.requestor.request('get', '/test', {})
416-
412+
417413
self.assertEqual(error.exception.http_status, 499)
418414

419415
def test_no_retry(self):
@@ -423,7 +419,7 @@ def test_no_retry(self):
423419
with self.request_raw_patch:
424420
with self.assertRaises(payjp.error.APIError) as error:
425421
self.requestor.request('get', '/test', {})
426-
422+
427423
self.assertEqual(error.exception.http_status, 599)
428424

429425
def test_full_retry(self):
@@ -434,7 +430,7 @@ def test_full_retry(self):
434430
with self.request_raw_patch:
435431
with self.assertRaises(payjp.error.APIError) as error:
436432
self.requestor.request('get', '/test', {})
437-
433+
438434
self.assertEqual(error.exception.http_status, 429)
439435

440436
def test_success_at_halfway_of_retries(self):
@@ -464,7 +460,7 @@ def test_retry_initial_delay(self):
464460
self.assertTrue(16 <= self.requestor._get_retry_delay(10) <= 32)
465461

466462

467-
463+
468464

469465
if __name__ == '__main__':
470466
unittest.main()

requirements.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1 @@
11
requests
2-
six

setup.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,10 @@
66

77
install_requires = []
88

9-
if sys.version_info < (2, 7):
10-
raise DeprecationWarning('Python 2.6 and older are no longer supported by PAY.JP. ')
9+
if sys.version_info < (3, 0):
10+
raise DeprecationWarning('Python 2 is no longer supported by PAY.JP. Please use Python 3.')
1111

1212
install_requires.append('requests >= 2.7.0')
13-
install_requires.append('six >= 1.9.0')
1413

1514
setup(
1615
name="payjp",
@@ -21,4 +20,5 @@
2120
packages=['payjp', 'payjp.test'],
2221
url='https://github.com/payjp/payjp-python',
2322
install_requires=install_requires,
23+
python_requires='>=3.0',
2424
)

0 commit comments

Comments
 (0)