66import pyodata .v2 .model
77import pyodata .v2 .service
88from pyodata .exceptions import PyODataException , HttpError
9+ from pyodata .v2 .response import Response
10+
11+
12+ async def _async_fetch_metadata (connection , url , logger ):
13+ logger .info ('Fetching metadata' )
14+
15+ async with connection .get (url + '$metadata' ) as async_response :
16+ resp = Response ()
17+ resp .url = async_response .url
18+ resp .headers = async_response .headers
19+ resp .status_code = async_response .status
20+ resp .content = await async_response .read ()
21+
22+ return _common_fetch_metadata (resp , logger )
923
1024
1125def _fetch_metadata (connection , url , logger ):
1226 # download metadata
1327 logger .info ('Fetching metadata' )
1428 resp = connection .get (url + '$metadata' )
1529
30+ return _common_fetch_metadata (resp , logger )
31+
32+
33+ def _common_fetch_metadata (resp , logger ):
1634 logger .debug ('Retrieved the response:\n %s\n %s' ,
1735 '\n ' .join ((f'H: { key } : { value } ' for key , value in resp .headers .items ())),
1836 resp .content )
@@ -37,6 +55,25 @@ class Client:
3755
3856 ODATA_VERSION_2 = 2
3957
58+ @staticmethod
59+ async def build_async_client (url , connection , odata_version = ODATA_VERSION_2 , namespaces = None ,
60+ config : pyodata .v2 .model .Config = None , metadata : str = None ):
61+ """Create instance of the OData Client for given URL"""
62+
63+ logger = logging .getLogger ('pyodata.client' )
64+
65+ if odata_version == Client .ODATA_VERSION_2 :
66+
67+ # sanitize url
68+ url = url .rstrip ('/' ) + '/'
69+
70+ if metadata is None :
71+ metadata = await _async_fetch_metadata (connection , url , logger )
72+ else :
73+ logger .info ('Using static metadata' )
74+ return Client ._build_service (logger , url , connection , odata_version , namespaces , config , metadata )
75+ raise PyODataException (f'No implementation for selected odata version { odata_version } ' )
76+
4077 def __new__ (cls , url , connection , odata_version = ODATA_VERSION_2 , namespaces = None ,
4178 config : pyodata .v2 .model .Config = None , metadata : str = None ):
4279 """Create instance of the OData Client for given URL"""
@@ -53,24 +90,29 @@ def __new__(cls, url, connection, odata_version=ODATA_VERSION_2, namespaces=None
5390 else :
5491 logger .info ('Using static metadata' )
5592
56- if config is not None and namespaces is not None :
57- raise PyODataException ('You cannot pass namespaces and config at the same time ' )
93+ return Client . _build_service ( logger , url , connection , odata_version , namespaces , config , metadata )
94+ raise PyODataException (f'No implementation for selected odata version { odata_version } ' )
5895
59- if config is None :
60- config = pyodata .v2 .model .Config ()
96+ @staticmethod
97+ def _build_service (logger , url , connection , odata_version = ODATA_VERSION_2 , namespaces = None ,
98+ config : pyodata .v2 .model .Config = None , metadata : str = None ):
6199
62- if namespaces is not None :
63- warnings .warn ("Passing namespaces directly is deprecated. Use class Config instead" , DeprecationWarning )
64- config .namespaces = namespaces
100+ if config is not None and namespaces is not None :
101+ raise PyODataException ('You cannot pass namespaces and config at the same time' )
65102
66- # create model instance from received metadata
67- logger .info ('Creating OData Schema (version: %d)' , odata_version )
68- schema = pyodata .v2 .model .MetadataBuilder (metadata , config = config ).build ()
103+ if config is None :
104+ config = pyodata .v2 .model .Config ()
69105
70- # create service instance based on model we have
71- logger . info ( 'Creating OData Service (version: %d)' , odata_version )
72- service = pyodata . v2 . service . Service ( url , schema , connection , config = config )
106+ if namespaces is not None :
107+ warnings . warn ( "Passing namespaces directly is deprecated. Use class Config instead" , DeprecationWarning )
108+ config . namespaces = namespaces
73109
74- return service
110+ # create model instance from received metadata
111+ logger .info ('Creating OData Schema (version: %d)' , odata_version )
112+ schema = pyodata .v2 .model .MetadataBuilder (metadata , config = config ).build ()
75113
76- raise PyODataException (f'No implementation for selected odata version { odata_version } ' )
114+ # create service instance based on model we have
115+ logger .info ('Creating OData Service (version: %d)' , odata_version )
116+ service = pyodata .v2 .service .Service (url , schema , connection , config = config )
117+
118+ return service
0 commit comments