Skip to content

Commit ffd65d3

Browse files
committed
Make response more user friendly and abstract
1 parent 0184023 commit ffd65d3

34 files changed

+44
-37
lines changed

iyzipay/iyzipay_resource.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@
88
import string
99

1010
import iyzipay
11-
11+
from iyzipay.iyzipay_response import IyzipayResponse
1212

1313
class IyzipayResource:
1414
RANDOM_STRING_SIZE = 8
1515
RE_SEARCH_V2 = r'/v2/'
1616
header = {
17-
"Accept": "application/json",
17+
"Accept": "application/json",
1818
"Content-type": "application/json",
1919
'x-iyzi-client-version': 'iyzipay-python-1.0.40'
2020
}
@@ -27,7 +27,11 @@ def connect(self, method, url, options, request_body_dict=None, pki=None):
2727
body_str = json.dumps(request_body_dict)
2828
header = self.get_http_header(url, options, body_str, pki)
2929
connection.request(method, url, body_str, header)
30-
return connection.getresponse()
30+
try:
31+
body = json.loads(response.read().decode('utf-8'))
32+
except:
33+
body = None
34+
return IyzipayResponse(response.status, body)
3135

3236
def get_http_header(self, url, options=None, body_str=None, pki_string=None):
3337
random_str = self.generate_random_string(self.RANDOM_STRING_SIZE)
@@ -866,7 +870,7 @@ def encode(file_path):
866870
class IyziLinkProduct(IyzipayResource):
867871
def create(self, request, options):
868872
return self.connect('POST', '/v2/iyzilink/products/', options, request)
869-
873+
870874
def retrieve(self, request, options):
871875
if request.get('token') is None:
872876
raise Exception('token must be in request')
@@ -883,7 +887,7 @@ def update(self, request, options):
883887
raise Exception('token must be in request')
884888
token = str(request.get('token'))
885889
return self.connect('PUT', '/v2/iyzilink/products/' + token, options, request)
886-
890+
887891
def delete(self, request, options):
888892
if request.get('token') is None:
889893
raise Exception('token must be in request')

iyzipay/iyzipay_response.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from collections import namedtuple
2+
3+
IyzipayResponse = namedtuple('IyzipayResponse', ('status', 'body'))

samples/api_test.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66

77
api_test = iyzipay.ApiTest().retrieve(options)
88

9-
print(api_test.read().decode('utf-8'))
9+
print(api_test.body)

samples/approve.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515
approval = iyzipay.Approval().create(request, options)
1616

17-
print(approval.read().decode('utf-8'))
17+
print(approval.body)

samples/cancel.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,4 @@
1717

1818
cancel = iyzipay.Cancel().create(request, options)
1919

20-
print(cancel.read().decode('utf-8'))
20+
print(cancel.body)

samples/create_card.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@
2323

2424
card = iyzipay.Card().create(request, options)
2525

26-
print(card.read().decode('utf-8'))
26+
print(card.body)

samples/create_limited_company_sub_merchant.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@
2424

2525
sub_merchant = iyzipay.SubMerchant().create(request, options)
2626

27-
print(sub_merchant.read().decode('utf-8'))
27+
print(sub_merchant.body)

samples/create_marketplace_payment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,4 +91,4 @@
9191

9292
payment = iyzipay.Payment().create(request, options)
9393

94-
print(payment.read().decode('utf-8'))
94+
print(payment.body)

samples/create_payment.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,4 +85,4 @@
8585

8686
payment = iyzipay.Payment().create(request, options)
8787

88-
print(payment.read().decode('utf-8'))
88+
print(payment.body)

samples/create_payment_with_registered_card.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,4 @@
8181

8282
payment = iyzipay.Payment().create(request, options)
8383

84-
print(payment.read().decode('utf-8'))
84+
print(payment.body)

0 commit comments

Comments
 (0)