|
| 1 | +""" |
| 2 | +Missing docstring |
| 3 | +Common Query for Entry and Assets |
| 4 | +""" |
| 5 | +import enum |
| 6 | + |
| 7 | + |
| 8 | +# ************* Module assetquery ************** |
| 9 | +# Your code has been rated at 10.00/10 |
| 10 | + |
| 11 | +class QueryOperation(enum.Enum): |
| 12 | + """ |
| 13 | + QueryOperation is enum that Provides Options to perform operation to query the result. |
| 14 | +
|
| 15 | + Available Options for QueryOperation are below. |
| 16 | + EQUALS, NOT_EQUALS, INCLUDES, EXCLUDES, IS_LESS_THAN, IS_LESS_THAN_OR_EQUAL |
| 17 | + IS_GREATER_THAN, IS_GREATER_THAN_OR_EQUAL, EXISTS, MATCHES |
| 18 | +
|
| 19 | + Arguments: |
| 20 | + enum {QueryOperation} -- Type of operation to perform |
| 21 | + """ |
| 22 | + EQUALS = "" |
| 23 | + NOT_EQUALS = '$ne' |
| 24 | + INCLUDES = '$in' |
| 25 | + EXCLUDES = '$nin' |
| 26 | + IS_LESS_THAN = '$lt' |
| 27 | + IS_LESS_THAN_OR_EQUAL = '$lte' |
| 28 | + IS_GREATER_THAN = '$gt' |
| 29 | + IS_GREATER_THAN_OR_EQUAL = '$gte' |
| 30 | + EXISTS = '$exists' |
| 31 | + MATCHES = '$regex' |
| 32 | + |
| 33 | + |
| 34 | +def _get_operation_value(fields): |
| 35 | + value = fields[0] if isinstance(fields, list) and len(fields) == 1 else fields |
| 36 | + return value |
| 37 | + |
| 38 | + |
| 39 | +class BaseQuery: |
| 40 | + """ |
| 41 | + Common Query class works for Entry As well as Asset |
| 42 | + """ |
| 43 | + |
| 44 | + def __init__(self): |
| 45 | + self.parameters = {} |
| 46 | + self.__query_params = {} |
| 47 | + |
| 48 | + def where(self, field_uid: str, query_operation: QueryOperation, fields=None): |
| 49 | + """ |
| 50 | + Get entries containing the field values matching the condition in the query. |
| 51 | + Arguments: |
| 52 | + field_uid {str} -- [accept field uid for the operation] |
| 53 | + query_operation {QueryOperation} -- [Type of operation to perform] |
| 54 | + fields {list} - list of string |
| 55 | + """ |
| 56 | + if None not in (field_uid, query_operation): |
| 57 | + result = _get_operation_value(fields) if query_operation.name == "EQUALS" \ |
| 58 | + else {query_operation.value: fields} |
| 59 | + self.parameters[field_uid] = result |
| 60 | + return self |
| 61 | + |
| 62 | + def skip(self, skip_count: int): |
| 63 | + """ |
| 64 | + The number of objects to skip before returning any. |
| 65 | + skip_count No of objects to skip from returned objects |
| 66 | + :param skip_count: |
| 67 | + :return: self |
| 68 | + """ |
| 69 | + self.__query_params["skip"] = str(skip_count) |
| 70 | + return self |
| 71 | + |
| 72 | + def limit(self, limit_count: int): |
| 73 | + """ |
| 74 | + A limit on the number of objects to return. |
| 75 | + :param limit_count: |
| 76 | + :return: self |
| 77 | + """ |
| 78 | + self.__query_params["skip"] = str(limit_count) |
| 79 | + return self |
| 80 | + |
| 81 | + def order_by_ascending(self, key: str): |
| 82 | + """ |
| 83 | + you can sort them in the ascending order with respect to the |
| 84 | + value of a specific field in the response body. |
| 85 | +
|
| 86 | + :param key: key on which ascending order to be implemented |
| 87 | + :return: self |
| 88 | + """ |
| 89 | + self.__query_params["asc"] = str(key) |
| 90 | + return self |
| 91 | + |
| 92 | + def order_by_descending(self, key: str): |
| 93 | + """ |
| 94 | + you can sort them in the descending order with respect to the value |
| 95 | + of a specific field in the response body. |
| 96 | + :param key: key on which descending order to be implemented |
| 97 | + :return: self - Class instance, So that method chaining can be performed |
| 98 | + """ |
| 99 | + self.__query_params["desc"] = str(key) |
| 100 | + return self |
| 101 | + |
| 102 | + def param(self, key: str, value): |
| 103 | + """ |
| 104 | + Adds Parameters to the to the request |
| 105 | +
|
| 106 | + Arguments: |
| 107 | + key {str} -- Key of the parameter |
| 108 | + value {[any]} -- Value of the parameter |
| 109 | +
|
| 110 | + Raises: |
| 111 | + KeyError: When None found in key or value |
| 112 | +
|
| 113 | + Returns: |
| 114 | + [self] -- instance of the class |
| 115 | +
|
| 116 | + ----------------------------------- |
| 117 | +
|
| 118 | + [Example] |
| 119 | + >>> import contentstack |
| 120 | + >>> stack = contentstack.Stack('api_key', 'access_token', 'environment') |
| 121 | + >>> content_type = stack.content_type('content_type_uid') |
| 122 | + >>> query = content_type.query() |
| 123 | + >>> query = query.param("key", "value") |
| 124 | + >>> result = query.find() |
| 125 | +
|
| 126 | + ----------------------------------- |
| 127 | + """ |
| 128 | + if None in (key, value): |
| 129 | + raise KeyError('Invalid key or value') |
| 130 | + self.__query_params[key] = str(value) |
| 131 | + return self |
| 132 | + |
| 133 | + def add_params(self, param: dict): |
| 134 | + """ |
| 135 | + Adds Parameters to the to the request |
| 136 | +
|
| 137 | + Arguments: |
| 138 | + param {dict} -- paramters |
| 139 | +
|
| 140 | + Returns: |
| 141 | + [self] -- Class instance, So that method chaining can be performed |
| 142 | + """ |
| 143 | + self.__query_params.update(param) |
| 144 | + return self |
| 145 | + |
| 146 | + def query(self, key: str, value): |
| 147 | + """ |
| 148 | + Adds key value pairs to the to the query parameters |
| 149 | +
|
| 150 | + Arguments: |
| 151 | + key {[str]} -- key of the query param |
| 152 | + value {[type]} -- value of query param |
| 153 | +
|
| 154 | + Raises: |
| 155 | + KeyError: when key or value found None |
| 156 | +
|
| 157 | + Returns: |
| 158 | + [self] -- Class instance, So that method chaining can be performed] |
| 159 | + """ |
| 160 | + if None in (key, value): |
| 161 | + raise KeyError('Invalid key or value') |
| 162 | + self.parameters[key] = str(value) |
| 163 | + return self |
0 commit comments