|
| 1 | +r"""This call fetches the list of all the assets of a particular stack. |
| 2 | +It also returns the content of each asset in JSON format. |
| 3 | +You can also specify the environment of which you wish to get the assets. |
| 4 | +""" |
| 5 | + |
| 6 | +# ************* Module assetquery ************** |
| 7 | +# Your code has been rated at 10/10 by pylint |
| 8 | + |
| 9 | +import json |
| 10 | +from contentstack.utility import Utils |
| 11 | +from contentstack.basequery import BaseQuery |
| 12 | + |
| 13 | + |
| 14 | +class AssetQuery(BaseQuery): |
| 15 | + """ |
| 16 | + This call fetches the list of all the assets of a particular stack. |
| 17 | + """ |
| 18 | + |
| 19 | + def __init__(self, http_instance): |
| 20 | + super().__init__() |
| 21 | + self.http_instance = http_instance |
| 22 | + self.__query_params = {} |
| 23 | + self.base_url = "{}/assets".format(self.http_instance.endpoint) |
| 24 | + if "environment" in self.http_instance.headers: |
| 25 | + env = self.http_instance.headers["environment"] |
| 26 | + self.base_url = "{}?{}".format(self.base_url, "environment={}".format(env)) |
| 27 | + |
| 28 | + def environment(self, environment): |
| 29 | + r"""Provide the name of the environment if you wish to retrieve the assets published |
| 30 | + in a particular environment. |
| 31 | +
|
| 32 | + :param environment: environment of the stack |
| 33 | +
|
| 34 | + :return: AssetQuery - so we can chain the call |
| 35 | +
|
| 36 | + ----------------------------- |
| 37 | + [Example]: |
| 38 | +
|
| 39 | + >>> import contentstack |
| 40 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 41 | + >>> result = stack.asset_query().environment('production').find() |
| 42 | + ------------------------------ |
| 43 | + """ |
| 44 | + self.__query_params["environment"] = environment |
| 45 | + return self |
| 46 | + |
| 47 | + def version(self, version): |
| 48 | + r"""Specify the version number of the asset that you wish to retrieve. |
| 49 | + If the version is not specified, the details of the latest version will be retrieved. |
| 50 | + To retrieve a specific version, keep the environment parameter blank. |
| 51 | +
|
| 52 | + :param version: version number of the asset that you wish to retrieve |
| 53 | +
|
| 54 | + :return: AssetQuery: so we can chain the call |
| 55 | +
|
| 56 | + ----------------------------- |
| 57 | + [Example]: |
| 58 | +
|
| 59 | + >>> import contentstack |
| 60 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 61 | + >>> result = stack.asset_query().version(3).find() |
| 62 | + ------------------------------ |
| 63 | + """ |
| 64 | + self.__query_params["version"] = version |
| 65 | + return self |
| 66 | + |
| 67 | + def include_dimension(self): |
| 68 | + r"""Include the dimensions (height and width) of the image in the response. |
| 69 | + Supported image types: JPG, GIF, PNG, WebP, BMP, TIFF, SVG, and PSD |
| 70 | +
|
| 71 | + :return: AssetQuery: so we can chain the call |
| 72 | +
|
| 73 | + ----------------------------- |
| 74 | + [Example]: |
| 75 | +
|
| 76 | + >>> import contentstack |
| 77 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 78 | + >>> result = stack.asset_query().include_dimension().find() |
| 79 | + ------------------------------ |
| 80 | + """ |
| 81 | + self.__query_params["include_dimension"] = "true" |
| 82 | + return self |
| 83 | + |
| 84 | + def relative_url(self): |
| 85 | + r"""include the relative URLs of the assets in the response. |
| 86 | +
|
| 87 | + :return: AssetQuery: so we can chain the call |
| 88 | +
|
| 89 | + ----------------------------- |
| 90 | + [Example]: |
| 91 | +
|
| 92 | + >>> import contentstack |
| 93 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 94 | + >>> result = stack.asset_query().relative_url().find() |
| 95 | + ------------------------------ |
| 96 | + """ |
| 97 | + self.__query_params["relative_urls"] = "true" |
| 98 | + return self |
| 99 | + |
| 100 | + def find(self): |
| 101 | + r"""This call fetches the list of all the assets of a particular stack. |
| 102 | + It also returns the content of each asset in JSON format. |
| 103 | + Learn more about Assets |
| 104 | + [https://www.contentstack.com/docs/content-managers/work-with-assets]. |
| 105 | +
|
| 106 | + :return: json result, List of asset object |
| 107 | +
|
| 108 | + ----------------------------- |
| 109 | + [Example]: |
| 110 | +
|
| 111 | + >>> import contentstack |
| 112 | + >>> stack = contentstack.Stack('api_key', 'delivery_token', 'environment') |
| 113 | + >>> result = stack.asset_query().find() |
| 114 | +
|
| 115 | + """ |
| 116 | + if self.parameters is not None and len(self.parameters) > 0: |
| 117 | + self.__query_params["query"] = json.dumps(self.parameters) |
| 118 | + url = Utils.get_complete_url(self.base_url, self.__query_params) |
| 119 | + return self.http_instance.get(url) |
0 commit comments