33
44from requests .auth import HTTPBasicAuth
55import requests
6- from pathlib import Path
76
8- import json
97import logging
108
11- from dataclasses import dataclass
129from 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 """
0 commit comments