@@ -50,7 +50,7 @@ def __init__(self, api_key: str, delivery_token: str, environment: str,
5050 branch = None ,
5151 ):
5252 """
53- Class that wraps the credentials of the authenticated user. Think of
53+ # Class that wraps the credentials of the authenticated user. Think of
5454 this as a container that holds authentication related data.
5555
5656 param api_key: api_key of the stack
@@ -65,13 +65,13 @@ def __init__(self, api_key: str, delivery_token: str, environment: str,
6565 takes input as dictionary object. containing one/multiple key value pair like below.
6666
6767 ```python
68- live_preview = {
69- 'enable': True,
68+ live_preview = {
69+ 'enable': True,
7070 'authorization': 'your_management_token',
7171 'host': 'api.contentstack.com',
7272 'include_edit_tags': True,
7373 'edit_tags_type': object | str,
74- }
74+ }
7575 ```
7676 :param retry_strategy: (optional) custom retry_strategy can be set.
7777 Method to create retry_strategy: create object of Retry() and provide the
@@ -101,20 +101,23 @@ def __init__(self, api_key: str, delivery_token: str, environment: str,
101101 self .timeout = timeout
102102 self .branch = branch
103103 self .retry_strategy = retry_strategy
104- self .live_preview_dict = live_preview
104+ self .live_preview = live_preview
105105
106106 self ._validate_stack ()
107107
108108 def _validate_stack (self ):
109109 if self .api_key is None or self .api_key == '' :
110110 raise PermissionError (
111- 'You are not permitted to the stack without valid APIKey' )
111+ 'You are not permitted to the stack without valid APIKey'
112+ )
112113 if self .delivery_token is None or self .delivery_token == "" :
113114 raise PermissionError (
114- 'You are not permitted to the stack without valid Delivery Token' )
115+ 'You are not permitted to the stack without valid Delivery Token'
116+ )
115117 if self .environment is None or self .environment == "" :
116118 raise PermissionError (
117- 'You are not permitted to the stack without valid Environment' )
119+ 'You are not permitted to the stack without valid Environment'
120+ )
118121
119122 if self .region .value == 'eu' and self .host == DEFAULT_HOST :
120123 self .host = 'eu-cdn.contentstack.com'
@@ -133,24 +136,14 @@ def _validate_stack(self):
133136 if self .branch is not None :
134137 self .headers ['branch' ] = self .branch
135138
136- if self .live_preview_dict is not None :
137- self ._validate_live_preview ()
138139 self .http_instance = HTTPSConnection (
139140 endpoint = self .endpoint ,
140- headers = self .headers , timeout = self .timeout ,
141+ headers = self .headers ,
142+ timeout = self .timeout ,
141143 retry_strategy = self .retry_strategy ,
142- live_preview = self .live_preview_dict
144+ live_preview = self .live_preview
143145 )
144146
145- def _validate_live_preview (self ):
146- if isinstance (self .live_preview_dict , dict ):
147- if 'enable' in self .live_preview_dict and self .live_preview_dict ['enable' ]:
148- self .headers ['authorization' ] = self .live_preview_dict ['authorization' ]
149- self .host = self .live_preview_dict ['host' ]
150- self .endpoint = f'https://{ self .host } /{ self .version } '
151- self .headers .pop ('access_token' )
152- self .headers .pop ('environment' )
153-
154147 @property
155148 def get_api_key (self ):
156149 """
@@ -191,7 +184,7 @@ def get_live_preview(self):
191184 """
192185 :return: live preview dictionary
193186 """
194- return self .live_preview_dict
187+ return self .live_preview
195188
196189 def content_type (self , content_type_uid = None ):
197190 """
@@ -360,14 +353,28 @@ def live_preview_query(self, **kwargs):
360353 hash='hashcode'
361354 )
362355 """
363- self .live_preview_dict .update (kwargs )
356+ self .live_preview .update (kwargs )
364357 self ._execute_management_api ()
365358 return self
366359
367360 def _execute_management_api (self ):
368- _ct_uid = self .live_preview_dict .get ("content_type_uid" )
369- _entry_uid = self .live_preview_dict .get ("entry_uid" )
370- _url = f'{ self .http_instance .endpoint } /content_types/{ _ct_uid } /entries/{ _entry_uid } '
371- _resp = self .http_instance .get (_url )
372- self .live_preview_dict ['resp' ] = _resp ['entry' ]
361+ _headers , _endpoint = self ._enable_live_preview ()
362+ _ct_uid = self .live_preview .get ("content_type_uid" )
363+ _entry_uid = self .live_preview .get ("entry_uid" )
364+ _url = f'{ _endpoint } /content_types/{ _ct_uid } /entries/{ _entry_uid } '
365+ import requests
366+ r = requests .get (url = _url , verify = True , headers = _headers )
367+ # _resp = self.http_instance.get(_url, headers=_headers)
368+ self .live_preview ['resp' ] = r .json ()['entry' ]
373369 return self
370+
371+ def _enable_live_preview (self ):
372+ if isinstance (self .live_preview , dict ) and not None :
373+ _endpoint = None
374+ _local_headers = {}
375+ if 'enable' in self .live_preview and self .live_preview ['enable' ]:
376+ _local_headers ['authorization' ] = self .live_preview ['authorization' ]
377+ _host = self .live_preview ['host' ]
378+ _endpoint = f'https://{ _host } /{ self .version } '
379+ _local_headers ['api_key' ] = self .headers ['api_key' ]
380+ return _local_headers , _endpoint
0 commit comments