Skip to content

Commit 1006b16

Browse files
committed
Fix linting errors
- Fix line length issues in client.py - Auto-format all files with ruff
1 parent aab276d commit 1006b16

File tree

6 files changed

+55
-55
lines changed

6 files changed

+55
-55
lines changed

tappay/__init__.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
1-
21
__version__ = "0.5.0"
32

43
from tappay.client import Client
4+
from tappay.exceptions import (
5+
AuthenticationError,
6+
ClientError,
7+
Error,
8+
Exceptions,
9+
ServerError,
10+
)
511
from tappay.models import Models
6-
from tappay.exceptions import Exceptions, Error, ClientError, ServerError, AuthenticationError
712

813
__all__ = [
914
"Client",

tappay/client.py

Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,23 @@
11
import logging
22
import os
33
from platform import python_version
4-
from typing import Optional, Dict, Any, Union
4+
from typing import Any, Dict, Optional
55

66
import requests
77

8-
from tappay.models import Models
98
from tappay.exceptions import Exceptions
9+
from tappay.models import Models
1010

1111
logger = logging.getLogger(__name__)
1212

13-
# We need to access __version__ from somewhere, commonly from a package level or hardcoded here then imported.
14-
# For now I will hardcode it here or pass it in.
15-
# Better yet, I will define __version__ in __init__.py and import it here? No, circular import.
16-
# I will put __version__ in a separate file or keep it in __init__ and pass it to Client if needed,
17-
# or just duplicate/move it.
18-
# Let's define it in client.py for now to avoid circular dependency if __init__ imports client.
19-
# Actually, the original code used it in User-Agent.
13+
# We need to access __version__ from somewhere, commonly from a package
14+
# level or hardcoded here then imported. For now I will hardcode it here
15+
# or pass it in. Better yet, I will define __version__ in __init__.py and
16+
# import it here? No, circular import. I will put __version__ in a
17+
# separate file or keep it in __init__ and pass it to Client if needed,
18+
# or just duplicate/move it. Let's define it in client.py for now to
19+
# avoid circular dependency if __init__ imports client. Actually, the
20+
# original code used it in User-Agent.
2021
VERSION = "0.5.0"
2122

2223

@@ -81,7 +82,8 @@ def pay_by_prime(
8182
"""
8283
if not isinstance(card_holder_data, Models.CardHolderData):
8384
raise TypeError(
84-
f"expected `CardHolderData` type for parameter `card_holder_data`, {type(card_holder_data)} found"
85+
f"expected `CardHolderData` type for parameter "
86+
f"`card_holder_data`, {type(card_holder_data)} found"
8587
)
8688

8789
params = {
@@ -126,9 +128,7 @@ def pay_by_token(
126128
"/tpc/payment/pay-by-token", params
127129
)
128130

129-
def refund(
130-
self, rec_trade_id: str, amount: int, **kwargs: Any
131-
) -> Dict[str, Any]:
131+
def refund(self, rec_trade_id: str, amount: int, **kwargs: Any) -> Dict[str, Any]:
132132
"""
133133
Refund a payment
134134
Ref: https://docs.tappaysdk.com/tutorial/zh/back.html#refund-api
@@ -196,9 +196,7 @@ def get_trade_history(self, rec_trade_id: str) -> Dict[str, Any]:
196196
"rec_trade_id": rec_trade_id,
197197
}
198198

199-
return self.__post_with_partner_key(
200-
"/tpc/transaction/trade-history", params
201-
)
199+
return self.__post_with_partner_key("/tpc/transaction/trade-history", params)
202200

203201
def bind_card(
204202
self,
@@ -212,7 +210,8 @@ def bind_card(
212210
"""
213211
if not isinstance(card_holder_data, Models.CardHolderData):
214212
raise TypeError(
215-
f"expected `CardHolderData` type for parameter `card_holder_data`, {type(card_holder_data)} found"
213+
f"expected `CardHolderData` type for parameter "
214+
f"`card_holder_data`, {type(card_holder_data)} found"
216215
)
217216

218217
params = {
@@ -224,9 +223,7 @@ def bind_card(
224223
if kwargs:
225224
params.update(**kwargs)
226225

227-
return self.__post_with_partner_key_and_merchant_id(
228-
"/tpc/card/bind", params
229-
)
226+
return self.__post_with_partner_key_and_merchant_id("/tpc/card/bind", params)
230227

231228
def remove_card(self, card_key: str, card_token: str) -> Dict[str, Any]:
232229
"""
@@ -240,9 +237,7 @@ def remove_card(self, card_key: str, card_token: str) -> Dict[str, Any]:
240237

241238
return self.__post_with_partner_key("/tpc/card/remove", params)
242239

243-
def cancel_refund(
244-
self, rec_trade_id: str, **kwargs: Any
245-
) -> Dict[str, Any]:
240+
def cancel_refund(self, rec_trade_id: str, **kwargs: Any) -> Dict[str, Any]:
246241
"""
247242
Cancel a single refund
248243
Ref: https://docs.tappaysdk.com/tutorial/zh/advanced.html#refund-cancel-api
@@ -254,9 +249,7 @@ def cancel_refund(
254249
if kwargs:
255250
params.update(**kwargs)
256251

257-
return self.__post_with_partner_key(
258-
"/tpc/transaction/refund/cancel", params
259-
)
252+
return self.__post_with_partner_key("/tpc/transaction/refund/cancel", params)
260253

261254
def __post_with_partner_key(
262255
self, request_uri: str, params: Dict[str, Any]
@@ -297,7 +290,7 @@ def __parse(self, response: requests.Response) -> Optional[Dict[str, Any]]:
297290
elif 500 <= response.status_code < 600:
298291
message = f"{response.status_code} response from {self.api_host}"
299292
raise Exceptions.ServerError(message)
300-
293+
301294
# Fallback for unexpected status codes
302295
message = f"Unexpected status code {response.status_code} from {self.api_host}"
303296
raise Exceptions.ServerError(message)

tappay/exceptions.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,22 @@
1-
21
class Error(Exception):
32
"""Base exception for all TapPay errors."""
3+
44
pass
55

66

77
class ClientError(Error):
88
"""Error raised when the client sends an invalid request."""
9+
910
pass
1011

1112

1213
class ServerError(Error):
1314
"""Error raised when the TapPay server encounters an issue."""
15+
1416
pass
1517

1618

1719
class AuthenticationError(ClientError):
1820
"""Error raised when authentication fails."""
21+
1922
pass

tappay/models.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1-
from typing import Optional, Dict, Any
1+
from typing import Any, Dict, Optional
2+
23

34
class Models:
45
"""Namespace for TapPay models."""
56

67
class Currencies:
78
"""Currency constants."""
9+
810
TWD = "TWD"
911

1012
class CardHolderData:

tests/conftest.py

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,22 @@
1-
21
import pytest
2+
33
from tappay.client import Client, Models
44

5+
56
@pytest.fixture
67
def sandbox_client():
7-
return Client(
8-
is_sandbox=True,
9-
partner_key="partner_key",
10-
merchant_id="merchant_id"
11-
)
8+
return Client(is_sandbox=True, partner_key="partner_key", merchant_id="merchant_id")
9+
1210

1311
@pytest.fixture
1412
def production_client():
1513
return Client(
16-
is_sandbox=False,
17-
partner_key="partner_key",
18-
merchant_id="merchant_id"
14+
is_sandbox=False, partner_key="partner_key", merchant_id="merchant_id"
1915
)
2016

17+
2118
@pytest.fixture
2219
def card_holder():
2320
return Models.CardHolderData(
24-
phone_number="0912345678",
25-
name="Wang Xiao Ming",
26-
21+
phone_number="0912345678", name="Wang Xiao Ming", email="[email protected]"
2722
)

tests/test_client.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
1+
from unittest.mock import Mock, patch
12

23
import pytest
3-
from unittest.mock import patch, Mock
4+
45
from tappay.client import Client, Models
56
from tappay.exceptions import AuthenticationError, ClientError, ServerError
67

8+
79
def test_client_initialization_sandbox():
810
client = Client(is_sandbox=True, partner_key="pk", merchant_id="mid")
911
assert client.api_host == "sandbox.tappaysdk.com"
1012
assert client.partner_key == "pk"
1113
assert client.merchant_id == "mid"
1214

15+
1316
def test_client_initialization_production():
1417
client = Client(is_sandbox=False, partner_key="pk", merchant_id="mid")
1518
assert client.api_host == "prod.tappaysdk.com"
1619

20+
1721
def test_pay_by_prime(sandbox_client, card_holder):
1822
with patch("tappay.client.requests.post") as mock_post:
1923
mock_response = Mock()
@@ -22,10 +26,7 @@ def test_pay_by_prime(sandbox_client, card_holder):
2226
mock_post.return_value = mock_response
2327

2428
response = sandbox_client.pay_by_prime(
25-
prime="test_prime",
26-
amount=100,
27-
details="test",
28-
card_holder_data=card_holder
29+
prime="test_prime", amount=100, details="test", card_holder_data=card_holder
2930
)
3031

3132
assert response["status"] == 0
@@ -35,15 +36,14 @@ def test_pay_by_prime(sandbox_client, card_holder):
3536
assert kwargs["json"]["amount"] == 100
3637
assert kwargs["json"]["currency"] == "TWD"
3738

39+
3840
def test_pay_by_prime_invalid_cardholder(sandbox_client):
3941
with pytest.raises(TypeError):
4042
sandbox_client.pay_by_prime(
41-
prime="p",
42-
amount=100,
43-
details="d",
44-
card_holder_data={}
43+
prime="p", amount=100, details="d", card_holder_data={}
4544
)
4645

46+
4747
def test_api_error_handling_401(sandbox_client):
4848
with patch("tappay.client.requests.post") as mock_post:
4949
mock_response = Mock()
@@ -55,17 +55,19 @@ def test_api_error_handling_401(sandbox_client):
5555
prime="p",
5656
amount=1,
5757
details="d",
58-
card_holder_data=Models.CardHolderData("p", "n", "e")
58+
card_holder_data=Models.CardHolderData("p", "n", "e"),
5959
)
6060

61+
6162
def test_api_error_handling_400(sandbox_client):
6263
with patch("tappay.client.requests.post") as mock_post:
6364
mock_response = Mock()
6465
mock_response.status_code = 400
6566
mock_post.return_value = mock_response
6667

6768
with pytest.raises(ClientError):
68-
sandbox_client.capture_today("id")
69+
sandbox_client.capture_today("id")
70+
6971

7072
def test_api_error_handling_500(sandbox_client):
7173
with patch("tappay.client.requests.post") as mock_post:
@@ -74,4 +76,4 @@ def test_api_error_handling_500(sandbox_client):
7476
mock_post.return_value = mock_response
7577

7678
with pytest.raises(ServerError):
77-
sandbox_client.capture_today("id")
79+
sandbox_client.capture_today("id")

0 commit comments

Comments
 (0)