|
| 1 | +import json |
| 2 | +from typing import Dict, Optional, Any, List |
| 3 | +from io import TextIOWrapper |
| 4 | +from unicodedata import name |
| 5 | + |
| 6 | +from requests import Response |
| 7 | + |
| 8 | +from urbanairship import Airship |
| 9 | +from urbanairship.devices.static_lists import GzipCompressReadStream |
| 10 | + |
| 11 | + |
| 12 | +class TagList: |
| 13 | + """Create, Upload, Delete, and get information for a tag list. |
| 14 | + Please see the Airship API documentation for more information |
| 15 | + about CSV formatting, limits, and use of this feature. |
| 16 | +
|
| 17 | + ;param airship: Required. An urbanairship.Airship instance. |
| 18 | + :param list_name: Required. A name for the list this instance represents. |
| 19 | + :param description: Optional. A description of the list. |
| 20 | + :param extra: Optional. A a dictionary of string mappings associated with the list. |
| 21 | + :param add_tags: Optional. A dictionary consisting of a tag group string and list of tag |
| 22 | + string to add to uploaded channels. |
| 23 | + :param remove_tags: Optional. A dictionary consisting of a tag group string and list of |
| 24 | + tag strings to remove from uploaded channels. |
| 25 | + :param set_tags: Optional. A dictionary consisting of a tag group string and list of tag |
| 26 | + strings to set on uploaded channels. Warning: This action is destructive and will |
| 27 | + remove all existing tags associated with channels. |
| 28 | + """ |
| 29 | + |
| 30 | + def __init__( |
| 31 | + self, |
| 32 | + airship: Airship, |
| 33 | + list_name: str, |
| 34 | + description: Optional[str] = None, |
| 35 | + extra: Optional[Dict[str, str]] = None, |
| 36 | + add_tags: Optional[Dict[str, List[str]]] = None, |
| 37 | + remove_tags: Optional[Dict[str, List[str]]] = None, |
| 38 | + set_tags: Optional[Dict[str, List[str]]] = None, |
| 39 | + ) -> None: |
| 40 | + self.airship = airship |
| 41 | + self.list_name = list_name |
| 42 | + self.description = description |
| 43 | + self.extra = extra |
| 44 | + self.add_tags = add_tags |
| 45 | + self.remove_tags = remove_tags |
| 46 | + self.set_tags = set_tags |
| 47 | + |
| 48 | + @property |
| 49 | + def _create_payload(self) -> Dict: |
| 50 | + payload: Dict[str, Any] = {"name": self.list_name} |
| 51 | + |
| 52 | + if self.description: |
| 53 | + payload["description"] = self.description |
| 54 | + if self.extra: |
| 55 | + payload["extra"] = self.extra |
| 56 | + if self.add_tags: |
| 57 | + payload["add"] = self.add_tags |
| 58 | + if self.remove_tags: |
| 59 | + payload["remove"] = self.remove_tags |
| 60 | + if self.set_tags: |
| 61 | + payload["set"] = self.set_tags |
| 62 | + |
| 63 | + return payload |
| 64 | + |
| 65 | + def create(self) -> Response: |
| 66 | + """Create a new tag list. Channels must be uploaded after creation using |
| 67 | + the `upload` method. |
| 68 | +
|
| 69 | + :return: Response object |
| 70 | + """ |
| 71 | + response = self.airship.request( |
| 72 | + method="POST", |
| 73 | + url=self.airship.urls.get("tag_lists_url"), |
| 74 | + body=json.dumps(self._create_payload), |
| 75 | + content_type="application/json", |
| 76 | + version=3, |
| 77 | + ) |
| 78 | + |
| 79 | + return response |
| 80 | + |
| 81 | + def upload(self, file_path: str) -> Response: |
| 82 | + """Upload a CSV file of channels. See the Airship API documentation |
| 83 | + for information about CSV file formatting requirements and limits. |
| 84 | +
|
| 85 | + :param file_path: Path to the CSV file to upload. |
| 86 | +
|
| 87 | + :return: Response object |
| 88 | + """ |
| 89 | + with open(file_path, "rb") as open_file: |
| 90 | + response = self.airship.request( |
| 91 | + method="PUT", |
| 92 | + body=GzipCompressReadStream(open_file), |
| 93 | + url=f"{self.airship.urls.get('tag_lists_url')}/{self.list_name}/csv", |
| 94 | + content_type="text/csv", |
| 95 | + version=3, |
| 96 | + encoding="gzip", |
| 97 | + ) |
| 98 | + return response |
| 99 | + |
| 100 | + def get_errors(self) -> Response: |
| 101 | + """Returns a csv of tag list processing errors. |
| 102 | +
|
| 103 | + :return: Response object |
| 104 | + """ |
| 105 | + response = self.airship.request( |
| 106 | + method="GET", |
| 107 | + body={}, |
| 108 | + url=f"{self.airship.urls.get('tag_lists_url')}/{self.list_name}/errors", |
| 109 | + version=3, |
| 110 | + ) |
| 111 | + return response |
| 112 | + |
| 113 | + @classmethod |
| 114 | + def list(cls, airship: Airship) -> Response: |
| 115 | + """Returns a json string with details on all tag lists associated with |
| 116 | + an Airship instance / project. |
| 117 | +
|
| 118 | + :return: Response object |
| 119 | + """ |
| 120 | + response = airship.request( |
| 121 | + method="GET", body={}, url=airship.urls.get("tag_lists_url"), version=3 |
| 122 | + ) |
| 123 | + return response |
0 commit comments