@@ -20,6 +20,7 @@ def __init__(self,
2020 content_type_uid = None ,
2121 entry_uid = None ,
2222 variant_uid = None ,
23+ branch = None ,
2324 params = None ,
2425 logger = None ):
2526
@@ -30,29 +31,47 @@ def __init__(self,
3031 self .content_type_id = content_type_uid
3132 self .entry_uid = entry_uid
3233 self .variant_uid = variant_uid
34+ self .branch = branch
3335 self .logger = logger or logging .getLogger (__name__ )
3436 self .entry_param = params or {}
3537
38+ def _prepare_variant_headers (self ):
39+ headers = self .http_instance .headers .copy ()
40+ if isinstance (self .variant_uid , str ):
41+ headers ['x-cs-variant-uid' ] = self .variant_uid
42+ elif isinstance (self .variant_uid , list ):
43+ headers ['x-cs-variant-uid' ] = ',' .join (self .variant_uid )
44+ if self .branch is not None :
45+ headers ['branch' ] = self .branch
46+ return headers
47+
48+ def _apply_variant_headers (self , headers ):
49+ self ._original_branch = self .http_instance .headers .get ('branch' )
50+ self .http_instance .headers .update (headers )
51+
52+ def _cleanup_variant_headers (self ):
53+ self .http_instance .headers .pop ('x-cs-variant-uid' , None )
54+ if self .branch is not None :
55+ if self ._original_branch is not None :
56+ self .http_instance .headers ['branch' ] = self ._original_branch
57+ else :
58+ self .http_instance .headers .pop ('branch' , None )
59+
3660 def find (self , params = None ):
3761 """
3862 find the variants of the entry of a particular content type
3963 :param self.variant_uid: {str} -- self.variant_uid
4064 :return: Entry, so you can chain this call.
4165 """
42- headers = self .http_instance .headers .copy () # Create a local copy of headers
43- if isinstance (self .variant_uid , str ):
44- headers ['x-cs-variant-uid' ] = self .variant_uid
45- elif isinstance (self .variant_uid , list ):
46- headers ['x-cs-variant-uid' ] = ',' .join (self .variant_uid )
47-
66+ headers = self ._prepare_variant_headers ()
4867 if params is not None :
4968 self .entry_param .update (params )
5069 encoded_params = parse .urlencode (self .entry_param )
5170 endpoint = self .http_instance .endpoint
5271 url = f'{ endpoint } /content_types/{ self .content_type_id } /entries?{ encoded_params } '
53- self .http_instance . headers . update (headers )
72+ self ._apply_variant_headers (headers )
5473 result = self .http_instance .get (url )
55- self .http_instance . headers . pop ( 'x-cs-variant-uid' , None )
74+ self ._cleanup_variant_headers ( )
5675 return result
5776
5877 def fetch (self , params = None ):
@@ -77,18 +96,13 @@ def fetch(self, params=None):
7796 if self .entry_uid is None :
7897 raise ValueError (ErrorMessages .ENTRY_UID_REQUIRED )
7998 else :
80- headers = self .http_instance .headers .copy () # Create a local copy of headers
81- if isinstance (self .variant_uid , str ):
82- headers ['x-cs-variant-uid' ] = self .variant_uid
83- elif isinstance (self .variant_uid , list ):
84- headers ['x-cs-variant-uid' ] = ',' .join (self .variant_uid )
85-
99+ headers = self ._prepare_variant_headers ()
86100 if params is not None :
87101 self .entry_param .update (params )
88102 encoded_params = parse .urlencode (self .entry_param )
89103 endpoint = self .http_instance .endpoint
90104 url = f'{ endpoint } /content_types/{ self .content_type_id } /entries/{ self .entry_uid } ?{ encoded_params } '
91- self .http_instance . headers . update (headers )
105+ self ._apply_variant_headers (headers )
92106 result = self .http_instance .get (url )
93- self .http_instance . headers . pop ( 'x-cs-variant-uid' , None )
107+ self ._cleanup_variant_headers ( )
94108 return result
0 commit comments