55import requests
66from .capcha_ocr import CapchaOCR , CapchaProcessing
77from .wasm_helper import wasm_encrypt
8+ from .modals import BalanceResponseModal , BalanceLoyaltyResponseModal , BankListResponseModal , \
9+ BeneficiaryListResponseModal , CardListResponseModal , AccountByPhoneResponseModal , UserInfoResponseModal , \
10+ LoanListResponseModal , SavingListResponseModal , InterestRateResponseModal , TransactionHistoryResponseModal \
11+ , CardTransactionsResponseModal , SavingDetailResponseModal
812
913headers_default = {
1014 'Cache-Control' : 'max-age=0' ,
@@ -101,8 +105,7 @@ def _req(self, url, *, json=None, headers=None):
101105 elif data_out ["result" ]["responseCode" ] == "GW200" :
102106 self ._authenticate ()
103107 else :
104- err_out = data_out ["result" ]
105- raise MBBankError (err_out )
108+ raise MBBankError (data_out ["result" ])
106109 return data_out
107110
108111 def _get_wasm_file (self ):
@@ -165,6 +168,7 @@ def login(self, captcha_text: str):
165168 data_out = r .json ()
166169 if data_out ["result" ]["ok" ]:
167170 self .sessionId = data_out ["sessionId" ]
171+ data_out .pop ("result" , None )
168172 self ._userinfo = data_out
169173 return
170174 else :
@@ -181,11 +185,11 @@ def _authenticate(self):
181185 return self .login (captcha_text )
182186 except MBBankError as e :
183187 if e .code == "GW283" :
184- continue # capcha error, try again
188+ continue # capcha error, try again
185189 raise e
186190
187191 def getTransactionAccountHistory (self , * , accountNo : str = None , from_date : datetime .datetime ,
188- to_date : datetime .datetime ):
192+ to_date : datetime .datetime ) -> TransactionHistoryResponseModal :
189193 """
190194 Get account transaction history
191195
@@ -195,7 +199,7 @@ def getTransactionAccountHistory(self, *, accountNo: str = None, from_date: date
195199 to_date (datetime.datetime): transaction to date
196200
197201 Returns:
198- success (dict ): account transaction history
202+ success (TransactionHistoryResponseModal ): account transaction history
199203
200204 Raises:
201205 MBBankError: if api response not ok
@@ -210,45 +214,45 @@ def getTransactionAccountHistory(self, *, accountNo: str = None, from_date: date
210214 data_out = self ._req (
211215 "https://online.mbbank.com.vn/api/retail-transactionms/transactionms/get-account-transaction-history" ,
212216 json = json_data )
213- return data_out
217+ return TransactionHistoryResponseModal . model_validate ( data_out , strict = True )
214218
215- def getBalance (self ):
219+ def getBalance (self ) -> BalanceResponseModal :
216220 """
217221 Get all main account and subaccount balance
218222
219223 Returns:
220- success (dict ): list account balance
224+ success (BalanceResponseModal ): balance model
221225
222226 Raises:
223227 MBBankError: if api response not ok
224228 """
225229 if self ._userinfo is None :
226230 self ._authenticate ()
227231 data_out = self ._req ("https://online.mbbank.com.vn/api/retail-web-accountms/getBalance" )
228- return data_out
232+ return BalanceResponseModal . model_validate ( data_out , strict = True )
229233
230- def getBalanceLoyalty (self ):
234+ def getBalanceLoyalty (self ) -> BalanceLoyaltyResponseModal :
231235 """
232236 Get Account loyalty rank and Member loyalty point
233237
234238 Returns:
235- success (dict ): loyalty point
239+ success (BalanceLoyaltyResponseModal ): loyalty balance model
236240
237241 Raises:
238242 MBBankError: if api response not ok
239243 """
240244 data_out = self ._req ("https://online.mbbank.com.vn/api/retail_web/loyalty/getBalanceLoyalty" )
241- return data_out
245+ return BalanceLoyaltyResponseModal . model_validate ( data_out , strict = True )
242246
243- def getInterestRate (self , currency : str = "VND" ):
247+ def getInterestRate (self , currency : str = "VND" ) -> InterestRateResponseModal :
244248 """
245249 Get saving interest rate
246250
247251 Args:
248252 currency (str, optional): currency ISO 4217 format. Defaults to "VND" (Vietnam Dong).
249253
250254 Returns:
251- success (dict ): interest rate
255+ success (InterestRateResponseModal ): interest rate
252256
253257 Raises:
254258 MBBankError: if api response not ok
@@ -258,10 +262,10 @@ def getInterestRate(self, currency: str = "VND"):
258262 "currency" : currency ,
259263 }
260264 data_out = self ._req ("https://online.mbbank.com.vn/api/retail_web/saving/getInterestRate" , json = json_data )
261- return data_out
265+ return InterestRateResponseModal . model_validate ( data_out , strict = True )
262266
263267 def getFavorBeneficiaryList (self , * , transactionType : typing .Literal ["TRANSFER" , "PAYMENT" ],
264- searchType : typing .Literal ["MOST" , "LATEST" ]):
268+ searchType : typing .Literal ["MOST" , "LATEST" ]) -> UserInfoResponseModal :
265269 """
266270 Get all favor or most transfer beneficiary list from your account
267271
@@ -270,7 +274,7 @@ def getFavorBeneficiaryList(self, *, transactionType: typing.Literal["TRANSFER",
270274 searchType (Literal["MOST", "LATEST"]): search type
271275
272276 Returns:
273- success (dict ): favor beneficiary list
277+ success (UserInfoResponseModal ): favor beneficiary list
274278
275279 Raises:
276280 MBBankError: if api response not ok
@@ -281,35 +285,35 @@ def getFavorBeneficiaryList(self, *, transactionType: typing.Literal["TRANSFER",
281285 }
282286 data_out = self ._req (
283287 "https://online.mbbank.com.vn/api/retail_web/internetbanking/getFavorBeneficiaryList" , json = json_data )
284- return data_out
288+ return BeneficiaryListResponseModal . model_validate ( data_out , strict = True )
285289
286- def getCardList (self ):
290+ def getCardList (self ) -> CardListResponseModal :
287291 """
288292 Get all card list from your account
289293
290294 Returns:
291- success (dict ): card list
295+ success (CardListResponseModal ): card list
292296
293297 Raises:
294298 MBBankError: if api response not ok
295299 """
296300 data_out = self ._req ("https://online.mbbank.com.vn/api/retail_web/card/getList" )
297- return data_out
301+ return CardListResponseModal . model_validate ( data_out , strict = True )
298302
299- def getSavingList (self ):
303+ def getSavingList (self ) -> SavingListResponseModal :
300304 """
301305 Get all saving list from your account
302306
303307 Returns:
304- success (dict ): saving list
308+ success (SavingListResponseModal ): saving list
305309
306310 Raises:
307311 MBBankError: if api response not ok
308312 """
309313 data_out = self ._req ("https://online.mbbank.com.vn/api/retail_web/saving/getList" )
310- return data_out
314+ return SavingListResponseModal . model_validate ( data_out , strict = True )
311315
312- def getSavingDetail (self , accNo : str , accType : typing .Literal ["OSA" , "SBA" ]):
316+ def getSavingDetail (self , accNo : str , accType : typing .Literal ["OSA" , "SBA" ]) -> SavingDetailResponseModal :
313317 """
314318 Get saving detail by account number
315319
@@ -318,7 +322,7 @@ def getSavingDetail(self, accNo: str, accType: typing.Literal["OSA", "SBA"]):
318322 accType (Literal["OSA", "SBA"]): saving account type | OSA: Online Saving Account, SBA: Saving Bank Account
319323
320324 Returns:
321- success (dict ): saving detail
325+ success (c ): saving detail
322326
323327 Raises:
324328 MBBankError: if api response not ok
@@ -328,22 +332,23 @@ def getSavingDetail(self, accNo: str, accType: typing.Literal["OSA", "SBA"]):
328332 "accType" : accType
329333 }
330334 data_out = self ._req ("https://online.mbbank.com.vn/api/retail_web/saving/getDetail" , json = json_data )
331- return data_out
335+ return SavingDetailResponseModal . model_validate ( data_out , strict = True )
332336
333- def getLoanList (self ):
337+ def getLoanList (self ) -> LoanListResponseModal :
334338 """
335339 Get all loan list from your account
336340
337341 Returns:
338- success (dict ): loan list
342+ success (LoanListResponseModal ): loan list
339343
340344 Raises:
341345 MBBankError: if api response not ok
342346 """
343347 data_out = self ._req ("https://online.mbbank.com.vn/api/retail-web-onlineloanms/loan/getList" )
344- return data_out
348+ return LoanListResponseModal . model_validate ( data_out , strict = True )
345349
346- def getCardTransactionHistory (self , cardNo : str , from_date : datetime .datetime , to_date : datetime .datetime ):
350+ def getCardTransactionHistory (self , cardNo : str , from_date : datetime .datetime ,
351+ to_date : datetime .datetime ) -> CardTransactionsResponseModal :
347352 """
348353 Get card transaction history
349354
@@ -353,7 +358,7 @@ def getCardTransactionHistory(self, cardNo: str, from_date: datetime.datetime, t
353358 to_date (datetime.datetime): to date
354359
355360 Returns:
356- success (dict ): card transaction history
361+ success (CardListResponseModal ): card transaction history
357362
358363 Raises:
359364 MBBankError: if api response not ok
@@ -367,56 +372,50 @@ def getCardTransactionHistory(self, cardNo: str, from_date: datetime.datetime, t
367372 "type" : "CARD" ,
368373 }
369374 data_out = self ._req ("https://online.mbbank.com.vn/api/retail_web/common/getTransactionHistory" , json = json_data )
370- return data_out
375+ return CardTransactionsResponseModal . model_validate ( data_out , strict = True )
371376
372- def getBankList (self ):
377+ def getBankList (self ) -> BankListResponseModal :
373378 """
374379 Get transfer all bank list
375380
376381 Returns:
377- success (dict ): bank list
382+ success (BankListResponseModal ): bank list
378383
379384 Raises:
380385 MBBankError: if api response not ok
381386 """
382387 data_out = self ._req ("https://online.mbbank.com.vn/api/retail_web/common/getBankList" )
383- return data_out
388+ return BankListResponseModal . model_validate ( data_out , strict = True )
384389
385- def getAccountByPhone (self , phone : str ):
390+ def getAccountByPhone (self , phone : str ) -> AccountByPhoneResponseModal :
386391 """
387392 Get transfer account info by phone (MBank internal account only)
388393
389394 Args:
390395 phone (str): MBBank account phone number
391396
392397 Returns:
393- success (dict ): account info
398+ success (AccountByPhoneResponseModal ): account info
394399
400+ Raises:
401+ MBBankError: if api response not ok
395402 """
396403 json_data = {
397404 "phone" : phone
398405 }
399406 data_out = self ._req ("https://online.mbbank.com.vn/api/retail_web/common/getAccountByPhone" , json = json_data )
400- return data_out
407+ return AccountByPhoneResponseModal . model_validate ( data_out , strict = True )
401408
402- def userinfo (self ):
409+ def userinfo (self ) -> UserInfoResponseModal :
403410 """
404411 Get current user info
405412
406413 Returns:
407- success (dict ): user info
414+ success (UserInfoResponseModal ): user info
408415
409416 Raises:
410417 MBBankError: if api response not ok
411418 """
412419 if self ._userinfo is None :
413420 self ._authenticate ()
414- else :
415- self .getBalance ()
416- return self ._userinfo
417-
418- def getBanks (self ):
419- data_out = self ._req ("https://online.mbbank.com.vn/api/retail_web/common/getBankList" )
420- return data_out
421-
422-
421+ return UserInfoResponseModal .model_validate (self ._userinfo , strict = True )
0 commit comments