@@ -41,7 +41,7 @@ def __init__(self, api_key: str, delivery_token: str, environment: str,
4141 total = 5 , backoff_factor = 0 , status_forcelist = [408 , 429 ]),
4242 live_preview = None ,
4343 branch = None ,
44- early_access = None ,
44+ early_access = None ,
4545 ):
4646 """
4747 # Class that wraps the credentials of the authenticated user. Think of
@@ -96,6 +96,15 @@ def __init__(self, api_key: str, delivery_token: str, environment: str,
9696 self .live_preview = live_preview
9797 self .early_access = early_access
9898 self ._validate_stack ()
99+ self ._setup_headers ()
100+ self ._setup_live_preview ()
101+ self .http_instance = HTTPSConnection (
102+ endpoint = self .endpoint ,
103+ headers = self .headers ,
104+ timeout = self .timeout ,
105+ retry_strategy = self .retry_strategy ,
106+ live_preview = self .live_preview
107+ )
99108
100109 def _validate_stack (self ):
101110 if self .api_key is None or self .api_key == '' :
@@ -123,6 +132,7 @@ def _validate_stack(self):
123132 self .host = f'{ self .region .value } -{ DEFAULT_HOST } '
124133 self .endpoint = f'https://{ self .host } /{ self .version } '
125134
135+ def _setup_headers (self ):
126136 self .headers = {
127137 'api_key' : self .api_key ,
128138 'access_token' : self .delivery_token ,
@@ -131,18 +141,10 @@ def _validate_stack(self):
131141 if self .early_access is not None :
132142 early_access_str = ', ' .join (self .early_access )
133143 self .headers ['x-header-ea' ] = early_access_str
134-
144+
135145 if self .branch is not None :
136146 self .headers ['branch' ] = self .branch
137-
138- self .http_instance = HTTPSConnection (
139- endpoint = self .endpoint ,
140- headers = self .headers ,
141- timeout = self .timeout ,
142- retry_strategy = self .retry_strategy ,
143- live_preview = self .live_preview
144- )
145-
147+
146148 @property
147149 def get_api_key (self ):
148150 """
@@ -323,8 +325,7 @@ def __sync_request(self):
323325 base_url = f'{ self .http_instance .endpoint } /stacks/sync'
324326 self .sync_param ['environment' ] = self .http_instance .headers ['environment' ]
325327 query = parse .urlencode (self .sync_param )
326- url = f'{ base_url } ?{ query } '
327- return self .http_instance .get (url )
328+ return self .http_instance .get (f'{ base_url } ?{ query } ' )
328329
329330 def image_transform (self , image_url , ** kwargs ):
330331 """
@@ -341,6 +342,15 @@ def image_transform(self, image_url, **kwargs):
341342 raise PermissionError (
342343 'image_url required for the image_transformation' )
343344 return ImageTransform (self .http_instance , image_url , ** kwargs )
345+
346+ def _setup_live_preview (self ):
347+ if self .live_preview and self .live_preview .get ("enable" ):
348+ region_prefix = "" if self .region .value == "us" else f"{ self .region .value } -"
349+ self .live_preview ["host" ] = f"{ region_prefix } rest-preview.contentstack.com"
350+
351+ if self .live_preview .get ("preview_token" ):
352+ self .headers ["preview_token" ] = self .live_preview ["preview_token" ]
353+
344354
345355 def live_preview_query (self , ** kwargs ):
346356 """
@@ -361,28 +371,31 @@ def live_preview_query(self, **kwargs):
361371 'authorization': 'management_token'
362372 )
363373 """
364-
365- if self .live_preview is not None and self .live_preview ['enable' ] and 'live_preview_query' in kwargs :
366- self .live_preview .update (** kwargs ['live_preview_query' ])
367- query = kwargs ['live_preview_query' ]
368- if query is not None :
369- self .live_preview ['live_preview' ] = query ['live_preview' ]
370- else :
371- self .live_preview ['live_preview' ] = 'init'
372- if 'content_type_uid' in self .live_preview and self .live_preview ['content_type_uid' ] is not None :
373- self .live_preview ['content_type_uid' ] = query ['content_type_uid' ]
374- if 'entry_uid' in self .live_preview and self .live_preview ['entry_uid' ] is not None :
375- self .live_preview ['entry_uid' ] = query ['entry_uid' ]
376- self ._cal_url ()
374+ if self .live_preview and self .live_preview .get ("enable" ) and "live_preview_query" in kwargs :
375+ query = kwargs ["live_preview_query" ]
376+ if isinstance (query , dict ):
377+ self .live_preview .update (query )
378+ self .live_preview ["live_preview" ] = query .get ("live_preview" , "init" )
379+ if "content_type_uid" in query :
380+ self .live_preview ["content_type_uid" ] = query ["content_type_uid" ]
381+ if "entry_uid" in query :
382+ self .live_preview ["entry_uid" ] = query ["entry_uid" ]
383+
384+ for key in ["release_id" , "preview_timestamp" ]:
385+ if key in query :
386+ self .http_instance .headers [key ] = query [key ]
387+ else :
388+ self .http_instance .headers .pop (key , None )
389+
390+ self ._cal_url ()
377391 return self
378392
379393 def _cal_url (self ):
380- host = self .live_preview ['host' ]
381- ct = self .live_preview ['content_type_uid' ]
382- url = f'https://{ host } /v3/content_types/{ ct } /entries'
383- if 'entry_uid' in self .live_preview :
384- uid = self .live_preview ['entry_uid' ]
385- lv = self .live_preview ['live_preview' ]
386- url = f'{ url } /{ uid } ?live_preview={ lv } '
387- self .live_preview ['url' ] = url
388- pass
394+ host = self .live_preview .get ("host" , DEFAULT_HOST )
395+ content_type = self .live_preview .get ("content_type_uid" , "default_content_type" )
396+ url = f"https://{ host } /v3/content_types/{ content_type } /entries"
397+ entry_uid = self .live_preview .get ("entry_uid" )
398+ live_preview = self .live_preview .get ("live_preview" , "init" )
399+ if entry_uid :
400+ url = f"{ url } /{ entry_uid } ?live_preview={ live_preview } "
401+ self .live_preview ["url" ] = url
0 commit comments