@@ -41,7 +41,7 @@ def __init__(self, api_key: str, delivery_token: str, environment: str,
41
41
total = 5 , backoff_factor = 0 , status_forcelist = [408 , 429 ]),
42
42
live_preview = None ,
43
43
branch = None ,
44
- early_access = None ,
44
+ early_access = None ,
45
45
):
46
46
"""
47
47
# 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,
96
96
self .live_preview = live_preview
97
97
self .early_access = early_access
98
98
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
+ )
99
108
100
109
def _validate_stack (self ):
101
110
if self .api_key is None or self .api_key == '' :
@@ -123,6 +132,7 @@ def _validate_stack(self):
123
132
self .host = f'{ self .region .value } -{ DEFAULT_HOST } '
124
133
self .endpoint = f'https://{ self .host } /{ self .version } '
125
134
135
+ def _setup_headers (self ):
126
136
self .headers = {
127
137
'api_key' : self .api_key ,
128
138
'access_token' : self .delivery_token ,
@@ -131,18 +141,10 @@ def _validate_stack(self):
131
141
if self .early_access is not None :
132
142
early_access_str = ', ' .join (self .early_access )
133
143
self .headers ['x-header-ea' ] = early_access_str
134
-
144
+
135
145
if self .branch is not None :
136
146
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
+
146
148
@property
147
149
def get_api_key (self ):
148
150
"""
@@ -323,8 +325,7 @@ def __sync_request(self):
323
325
base_url = f'{ self .http_instance .endpoint } /stacks/sync'
324
326
self .sync_param ['environment' ] = self .http_instance .headers ['environment' ]
325
327
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 } ' )
328
329
329
330
def image_transform (self , image_url , ** kwargs ):
330
331
"""
@@ -341,6 +342,15 @@ def image_transform(self, image_url, **kwargs):
341
342
raise PermissionError (
342
343
'image_url required for the image_transformation' )
343
344
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
+
344
354
345
355
def live_preview_query (self , ** kwargs ):
346
356
"""
@@ -361,28 +371,31 @@ def live_preview_query(self, **kwargs):
361
371
'authorization': 'management_token'
362
372
)
363
373
"""
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 ()
377
391
return self
378
392
379
393
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