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