11""" Zohal RestAPI Client """
2- from typing import Optional , Union
2+ from typing import Union
3+ import io
4+ from .types import RequestData
35import logging
46
57from requests import Session
68from requests .exceptions import (Timeout , HTTPError as RHTTPError ,
79 JSONDecodeError )
810
9- from .types import RequestData , ResponseData
10- from .types .national_identity import NationalIdentity
11- from .types .shahkar import Shahkar
12-
13- from .models .shahkar import ShahkarRequest
14- from .models .national_identity_inquery import NationalIdentityInqueryRequest
1511from .exceptions import HTTPError , TimeoutError , ZohalError
12+ from .services import InquiryMixin
1613
1714logger = logging .getLogger (__name__ )
1815
19- BASE_URL = "https://service.zohal.io/api"
20-
2116
22- class Client :
17+ class Client ( InquiryMixin ) :
2318
2419 def __init__ (self , token : str ):
2520 if token is None :
@@ -29,17 +24,22 @@ def __init__(self, token: str):
2924 self .session = Session ()
3025 self .session .headers ['Authorization' ] = f'Bearer { self .token } '
3126
32- def _request (self ,
33- url : str ,
34- data : Union [RequestData , dict , None ] = None ,
35- method : str = "GET" ):
27+ def request (self ,
28+ url : str ,
29+ data : Union [RequestData , dict , None ] = None ,
30+ files = None ,
31+ method : str = "GET" ):
3632 logger .debug (f"request to { url = } with { method = } " )
3733 try :
3834 kwargs = {"url" : url , "method" : method }
3935 if method in ["POST" , "PUT" , "PATCH" ] and isinstance (
4036 data , RequestData ):
41- data = data .to_dict ()
42- kwargs ['json' ] = data
37+ if data :
38+ data = data .to_dict ()
39+ kwargs ['json' ] = data
40+
41+ if files :
42+ kwargs ['files' ] = files
4343
4444 response = self .session .request (** kwargs )
4545 logger .debug (
@@ -63,23 +63,3 @@ def _request(self,
6363 f"raised error while loads json data { e .response .status_code } cause { e .response .text } "
6464 )
6565 raise e
66-
67- def national_identity_inquiry (self ,
68- national_code : str ,
69- birth_date : str ,
70- gender : Optional [bool ] = None ):
71- data = NationalIdentityInqueryRequest (national_code = national_code ,
72- birth_date = birth_date ,
73- gender = gender )
74- response = self ._request (
75- f"{ BASE_URL } /v0/services/inquiry/national_identity_inquiry" ,
76- data ,
77- method = "POST" )
78- return ResponseData .serialize (NationalIdentity , response )
79-
80- def shahkar (self , national_code : str , mobile : str ):
81- data = ShahkarRequest (national_code = national_code , mobile = mobile )
82- response = self ._request (f"{ BASE_URL } /v0/services/inquiry/shahkar" ,
83- data ,
84- method = "POST" )
85- return ResponseData .serialize (Shahkar , response )
0 commit comments