-
Notifications
You must be signed in to change notification settings - Fork 3
Added variants support in Python CDA SDK #141
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| import logging | ||
| from urllib import parse | ||
|
|
||
| from contentstack.deep_merge_lp import DeepMergeMixin | ||
sunil-lakshman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| from contentstack.entryqueryable import EntryQueryable | ||
|
|
||
| class Variants(EntryQueryable, ): | ||
sunil-lakshman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| """ | ||
| An entry is the actual piece of content that you want to publish. | ||
| Entries can be created for one of the available content types. | ||
|
|
||
| Entry works with | ||
| version={version_number} | ||
| environment={environment_name} | ||
| locale={locale_code} | ||
| """ | ||
|
|
||
| def __init__(self, | ||
| http_instance=None, | ||
| content_type_uid=None, | ||
| entry_uid=None, | ||
| variant_uid=None, | ||
| params=None, | ||
| logger=None): | ||
|
|
||
| super().__init__() | ||
| EntryQueryable.__init__(self) | ||
| self.entry_param = {} | ||
| self.http_instance = http_instance | ||
| self.content_type_id = content_type_uid | ||
| self.entry_uid = entry_uid | ||
| self.variant_uid = variant_uid | ||
| self.logger = logger or logging.getLogger(__name__) | ||
| self.entry_param = params or {} | ||
|
|
||
| def find(self, params=None): | ||
| """ | ||
| find the variants of the entry of a particular content type | ||
| :param self.variant_uid: {str} -- self.variant_uid | ||
| :return: Entry, so you can chain this call. | ||
| """ | ||
| headers = self.http_instance.headers.copy() # Create a local copy of headers | ||
| if isinstance(self.variant_uid, str): | ||
| headers['x-cs-variant-uid'] = self.variant_uid | ||
| elif isinstance(self.variant_uid, list): | ||
| headers['x-cs-variant-uid'] = ','.join(self.variant_uid) | ||
|
|
||
| if params is not None: | ||
| self.entry_param.update(params) | ||
| encoded_params = parse.urlencode(self.entry_param) | ||
| endpoint = self.http_instance.endpoint | ||
| url = f'{endpoint}/content_types/{self.content_type_id}/entries?{encoded_params}' | ||
| self.http_instance.headers.update(headers) | ||
| result = self.http_instance.get(url) | ||
| self.http_instance.headers.pop('x-cs-variant-uid', None) | ||
| return result | ||
|
|
||
| def fetch(self, params=None): | ||
| """ | ||
| This method is useful to fetch variant entries of a paticular content type and entries of the of the stack. | ||
sunil-lakshman marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| :return:dict -- contentType response | ||
| ------------------------------ | ||
| Example: | ||
|
|
||
| >>> import contentstack | ||
| >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') | ||
| >>> content_type = stack.content_type('content_type_uid') | ||
| >>> some_dict = {'abc':'something'} | ||
| >>> response = content_type.fetch(some_dict) | ||
| ------------------------------ | ||
| """ | ||
| """ | ||
| Fetches the variants of the entry | ||
| :param self.variant_uid: {str} -- self.variant_uid | ||
| :return: Entry, so you can chain this call. | ||
| """ | ||
| if self.entry_uid is None: | ||
| raise ValueError("entry_uid is required") | ||
| else: | ||
| headers = self.http_instance.headers.copy() # Create a local copy of headers | ||
| if isinstance(self.variant_uid, str): | ||
| headers['x-cs-variant-uid'] = self.variant_uid | ||
| elif isinstance(self.variant_uid, list): | ||
| headers['x-cs-variant-uid'] = ','.join(self.variant_uid) | ||
|
|
||
| if params is not None: | ||
| self.entry_param.update(params) | ||
| encoded_params = parse.urlencode(self.entry_param) | ||
| endpoint = self.http_instance.endpoint | ||
| url = f'{endpoint}/content_types/{self.content_type_id}/entries/{self.entry_uid}?{encoded_params}' | ||
| self.http_instance.headers.update(headers) | ||
| result = self.http_instance.get(url) | ||
| self.http_instance.headers.pop('x-cs-variant-uid', None) | ||
| return result | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.