|
3 | 3 | from datetime import datetime |
4 | 4 | import re |
5 | 5 |
|
| 6 | +from .static_lists import GzipCompressReadStream |
| 7 | + |
6 | 8 | logger = logging.getLogger("urbanairship") |
7 | 9 |
|
8 | 10 |
|
@@ -146,6 +148,96 @@ def send(self): |
146 | 148 | return AttributeResponse(response=response) |
147 | 149 |
|
148 | 150 |
|
| 151 | +class AttributeList(object): |
| 152 | + """ |
| 153 | + Define and manage attribute lists; upload corresponding attribute data in CSV format. |
| 154 | +
|
| 155 | + :param airship: Required. An unbanairship.Airship instance. |
| 156 | + :param list_name: Required. The name of your list. Must be prefixed |
| 157 | + with "ua_attributes_" |
| 158 | + :param description: Required. A description of your list. |
| 159 | + :param extra: Optional. An optional dict of up to 100 key-value (string-to-string) |
| 160 | + pairs associated with the list. |
| 161 | + """ |
| 162 | + |
| 163 | + def __init__(self, airship, list_name, description, extra=None): |
| 164 | + self.airship = airship |
| 165 | + self.list_name = list_name |
| 166 | + self.description = description |
| 167 | + self.extra = extra |
| 168 | + |
| 169 | + @property |
| 170 | + def _create_payload(self): |
| 171 | + payload = {"name": self.list_name, "description": self.description} |
| 172 | + |
| 173 | + if self.extra: |
| 174 | + payload["extra"] = self.extra |
| 175 | + |
| 176 | + return payload |
| 177 | + |
| 178 | + def create(self): |
| 179 | + response = self.airship.request( |
| 180 | + method="POST", |
| 181 | + url=self.airship.urls.get("attributes_list_url"), |
| 182 | + body=json.dumps(self._create_payload), |
| 183 | + content_type="application/json", |
| 184 | + version=3, |
| 185 | + ) |
| 186 | + |
| 187 | + return response |
| 188 | + |
| 189 | + def upload(self, file_path): |
| 190 | + """ |
| 191 | + Upload a CSV that will set attribute values on the specified channels or |
| 192 | + named users. Please see the documentation at |
| 193 | + https://docs.airship.com/api/ua/#operation-api-attribute-lists-list_name-csv-put |
| 194 | + for details about list formatting, size limits, and error responses. |
| 195 | +
|
| 196 | + :param file_path: Required. Local path to the csv file to be uploaded. |
| 197 | + """ |
| 198 | + with open(file_path, "rb") as open_file: |
| 199 | + response = self.airship._request( |
| 200 | + method="PUT", |
| 201 | + body=GzipCompressReadStream(open_file), |
| 202 | + url=self.airship.urls.get("attributes_list_url") |
| 203 | + + self.list_name |
| 204 | + + "/csv/", |
| 205 | + content_type="text/csv", |
| 206 | + version=3, |
| 207 | + encoding="gzip", |
| 208 | + ) |
| 209 | + |
| 210 | + return response |
| 211 | + |
| 212 | + def get_errors(self): |
| 213 | + """ |
| 214 | + Returns csv of attribute list processing errors. During processing, after a |
| 215 | + list is uploaded, errors can occur. Depending on the type of list |
| 216 | + processing, an error file may be created, showing a user exactly what |
| 217 | + went wrong. |
| 218 | + """ |
| 219 | + response = self.airship.request( |
| 220 | + method="GET", |
| 221 | + body={}, |
| 222 | + url=self.airship.urls.get("attributes_list_url") |
| 223 | + + self.list_name |
| 224 | + + "/errors/", |
| 225 | + ) |
| 226 | + |
| 227 | + return response |
| 228 | + |
| 229 | + @classmethod |
| 230 | + def list(cls, airship): |
| 231 | + response = airship._request( |
| 232 | + method="GET", |
| 233 | + url=airship.urls.get("attributes_list_url"), |
| 234 | + body={}, |
| 235 | + version=3, |
| 236 | + ) |
| 237 | + |
| 238 | + return response |
| 239 | + |
| 240 | + |
149 | 241 | class AttributeResponse(object): |
150 | 242 | def __init__(self, response): |
151 | 243 | self.response = response |
|
0 commit comments