Skip to content

Commit ae52c26

Browse files
authored
Merge pull request #14 from f-klubben/refactor/split-modules
Divide accounting into multiple files
2 parents cc4a272 + 7042ddb commit ae52c26

File tree

4 files changed

+62
-55
lines changed

4 files changed

+62
-55
lines changed

src/vipps_api/accounting.py

Lines changed: 8 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3,64 +3,18 @@
33

44
from requests.auth import HTTPBasicAuth
55
import requests
6-
from pathlib import Path
76

8-
import json
97
import logging
108

11-
from dataclasses import dataclass
129
from typing import Any, Tuple
10+
from dataclasses import dataclass
1311

14-
15-
class TokenFileException(Exception):
16-
pass
17-
18-
19-
@dataclass(frozen=True)
20-
class AccountingAPIKeys:
21-
"""
22-
The lowest level API keys, which only provide access to ReportAPI.
23-
https://developer.vippsmobilepay.com/docs/partner/partner-keys/
24-
"""
25-
26-
client_id: str
27-
client_secret: str
28-
29-
30-
class Utils(object):
31-
logger = logging.getLogger(__name__)
32-
33-
tokens_file = (Path(__file__).parent / 'vipps-tokens.json').as_posix()
34-
tokens_file_backup = (Path(__file__).parent / 'vipps-tokens.json.bak').as_posix()
35-
36-
@staticmethod
37-
def load_accounting_keys_from_file(path: str) -> AccountingAPIKeys:
38-
with open(path, 'r') as json_file:
39-
raw_tokens = json.load(json_file)
40-
41-
if raw_tokens is None:
42-
raise TokenFileException("Token file is None")
43-
44-
if 'client_id' not in raw_tokens:
45-
raise TokenFileException("client_id missing")
46-
47-
if 'client_secret' not in raw_tokens:
48-
raise TokenFileException("client_secret missing")
49-
50-
return AccountingAPIKeys(client_id=raw_tokens['client_id'], client_secret=raw_tokens['client_secret'])
51-
52-
@classmethod
53-
def load_accounting_api_keys(cls) -> AccountingAPIKeys:
54-
try:
55-
return Utils.load_accounting_keys_from_file(cls.tokens_file)
56-
except:
57-
cls.logger.error("tokens file not correct. Reverting to backup")
58-
59-
return Utils.load_accounting_keys_from_file(cls.tokens_file_backup)
12+
from .keys import AccountingAPIKeys
13+
from .utils import Utils
6014

6115

6216
@dataclass
63-
class ReportAPIAccessToken:
17+
class AccessToken:
6418
"""
6519
The session access token used in report API. Expires every 15 minutes.
6620
https://developer.vippsmobilepay.com/docs/APIs/access-token-api/partner-authentication/
@@ -75,7 +29,7 @@ class ReportAPI:
7529
logger = logging.getLogger(__name__)
7630

7731
tokens: AccountingAPIKeys
78-
session: ReportAPIAccessToken
32+
session: AccessToken
7933
ledger_id: int | None
8034
cursor: str | None
8135

@@ -86,7 +40,7 @@ def __init__(self, api_keys: AccountingAPIKeys, myshop_number: int):
8640
def load(self):
8741
self.session = self.__retrieve_access_token()
8842

89-
def __retrieve_new_session(self) -> ReportAPIAccessToken:
43+
def __retrieve_new_session(self) -> AccessToken:
9044
"""
9145
Fetches a new access token using the refresh token.
9246
:return: Tuple of (access_token, access_token_timeout)
@@ -110,7 +64,7 @@ def __retrieve_new_session(self) -> ReportAPIAccessToken:
11064
access_token = json_response['access_token']
11165
access_token_timeout = expire_time.isoformat(timespec='milliseconds')
11266

113-
return ReportAPIAccessToken(access_token=access_token, access_token_timeout=access_token_timeout)
67+
return AccessToken(access_token=access_token, access_token_timeout=access_token_timeout)
11468

11569
def get_ledger_info(self, myshop_number: int):
11670
"""
@@ -162,7 +116,7 @@ def __refresh_expired_token(self):
162116
self.session = self.__retrieve_new_session()
163117

164118
if self.ledger_id is None:
165-
__refresh_ledger_id()
119+
self.__refresh_ledger_id()
166120

167121
def get_transactions_historic(self, transaction_date: date) -> list:
168122
"""

src/vipps_api/keys.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
from dataclasses import dataclass
2+
3+
4+
@dataclass(frozen=True)
5+
class AccountingAPIKeys:
6+
"""
7+
The lowest level API keys, which only provide access to ReportAPI.
8+
https://developer.vippsmobilepay.com/docs/partner/partner-keys/
9+
"""
10+
11+
client_id: str
12+
client_secret: str

src/vipps_api/utils.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
import logging
2+
from pathlib import Path
3+
import json
4+
5+
from .keys import AccountingAPIKeys
6+
7+
8+
class TokenFileException(Exception):
9+
pass
10+
11+
12+
class Utils(object):
13+
logger = logging.getLogger(__name__)
14+
15+
tokens_file = (Path(__file__).parent / 'vipps-tokens.json').as_posix()
16+
tokens_file_backup = (Path(__file__).parent / 'vipps-tokens.json.bak').as_posix()
17+
18+
@staticmethod
19+
def load_accounting_keys_from_file(path: str) -> AccountingAPIKeys:
20+
with open(path, 'r') as json_file:
21+
raw_tokens = json.load(json_file)
22+
23+
if raw_tokens is None:
24+
raise TokenFileException("Token file is None")
25+
26+
if 'client_id' not in raw_tokens:
27+
raise TokenFileException("client_id missing")
28+
29+
if 'client_secret' not in raw_tokens:
30+
raise TokenFileException("client_secret missing")
31+
32+
return AccountingAPIKeys(client_id=raw_tokens['client_id'], client_secret=raw_tokens['client_secret'])
33+
34+
@classmethod
35+
def load_accounting_api_keys(cls) -> AccountingAPIKeys:
36+
try:
37+
return Utils.load_accounting_keys_from_file(cls.tokens_file)
38+
except:
39+
cls.logger.error("tokens file not correct. Reverting to backup")
40+
41+
return Utils.load_accounting_keys_from_file(cls.tokens_file_backup)

tests/test_accounting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def test_ledger_id_parsed(self, mock_ledger_info_response):
3131

3232
report = ReportAPI(AccountingAPIKeys("<<client_id>>", "<<client_secret>>"), 23456)
3333
report.tokens_file = (Path(__file__).parent / 'fixtures' / 'vipps-tokens.valid.json').as_posix()
34-
report.session = ReportAPIAccessToken("_access_token_", "")
34+
report.session = AccessToken("_access_token_", "")
3535

3636
actualLedgerId = report.get_ledger_id(0)
3737

0 commit comments

Comments
 (0)