|
| 1 | +from spontit import util |
| 2 | +from enum import Enum |
| 3 | +import json |
| 4 | + |
| 5 | + |
| 6 | +class SpontitResource: |
| 7 | + """ |
| 8 | + Use this resource to access your account and related functions. |
| 9 | + """ |
| 10 | + class FunctionStrings(Enum): |
| 11 | + """ |
| 12 | + An mapping of function to string. For internal use. |
| 13 | + """ |
| 14 | + GET_TOPIC_ID_TO_DISPLAY_NAME_MAPPING = "get_topic_id_to_display_name_mapping" |
| 15 | + PUSH = "push" |
| 16 | + |
| 17 | + def __init__(self, user_id, secret_key): |
| 18 | + """ |
| 19 | + Initializes the resource. |
| 20 | + :param user_id: Your user ID. |
| 21 | + :param secret_key: A secret key you generated on spontit.com/secret_keys |
| 22 | + """ |
| 23 | + if type(user_id) is not str: |
| 24 | + raise Exception("User ID must be a string.") |
| 25 | + if type(secret_key) is not str: |
| 26 | + raise Exception("Secret key must be a string.") |
| 27 | + self.user_id = user_id |
| 28 | + self.secret_key = secret_key |
| 29 | + |
| 30 | + def __get_payload_dict(self, method_str): |
| 31 | + """ |
| 32 | + Constructs a basic payload dictionary for the method string passed. To be sent with the put request. |
| 33 | + :param method_str: The string representing the method in consideration. |
| 34 | + :return: The payload dictionary |
| 35 | + """ |
| 36 | + if type(method_str) is SpontitResource.FunctionStrings: |
| 37 | + method_str = method_str.value |
| 38 | + print(type(method_str)) |
| 39 | + assert type(method_str) is str |
| 40 | + return { |
| 41 | + 'user_id': self.user_id, |
| 42 | + 'secret_key': self.secret_key, |
| 43 | + 'method': method_str |
| 44 | + } |
| 45 | + |
| 46 | + def get_topic_id_to_display_name_mapping(self): |
| 47 | + """ |
| 48 | + Sends a put request requesting the topic IDs associated with the user account. You can access the list of topic |
| 49 | + IDs by getting the .keys() of the dictionary returned. |
| 50 | + :return: Returns either a mapping or an error description (with the key "Error") |
| 51 | + """ |
| 52 | + return util.put_request(self.__get_payload_dict(self.FunctionStrings.GET_TOPIC_ID_TO_DISPLAY_NAME_MAPPING)) |
| 53 | + |
| 54 | + def push(self, |
| 55 | + call_to_action, |
| 56 | + link=None, |
| 57 | + to_topic_ids=None): |
| 58 | + """ |
| 59 | + Use this method to send your own push notification! |
| 60 | + :param call_to_action: The message that you would like to push. |
| 61 | + :param link: [OPTIONAL] A link for content you would like to attach to the push notification |
| 62 | + :param to_topic_ids: [OPTIONAL] A list of topic IDS you would like to push to. If to_topic_ids is not specified, |
| 63 | + then the push notification will be sent to the main channel. |
| 64 | + :return: Returns either a success response or an error description (with the key "Error") |
| 65 | + """ |
| 66 | + # Construct the payload. |
| 67 | + payload = self.__get_payload_dict(self.FunctionStrings.PUSH) |
| 68 | + |
| 69 | + # Type check call_to_action and add to payload. |
| 70 | + if type(call_to_action) is not str: |
| 71 | + raise Exception("Content must be a string.") |
| 72 | + payload["call_to_action"] = call_to_action |
| 73 | + |
| 74 | + # If link exists, type check and add to payload. |
| 75 | + if link is not None: |
| 76 | + if type(link) is not str: |
| 77 | + raise Exception("URL must be formatted as a string.") |
| 78 | + payload["link"] = link |
| 79 | + |
| 80 | + # If topic IDs exist, type check and add to payload. |
| 81 | + if to_topic_ids is not None: |
| 82 | + if type(to_topic_ids) is set: |
| 83 | + to_topic_ids = list(to_topic_ids) |
| 84 | + if type(to_topic_ids) is not list: |
| 85 | + raise Exception("The list of topic IDs passed must be a set or a list.") |
| 86 | + payload["to_topic_ids"] = json.dumps(to_topic_ids) |
| 87 | + # Send the put request and return the content. |
| 88 | + return util.put_request(payload) |
0 commit comments