|
| 1 | +from typing import Any, Dict, List, Optional, cast |
| 2 | + |
| 3 | +from typing_extensions import NotRequired, TypedDict |
| 4 | + |
| 5 | +from resend import request |
| 6 | +from resend.pagination_helper import PaginationHelper |
| 7 | + |
| 8 | +from ._topic import Topic |
| 9 | + |
| 10 | + |
| 11 | +class Topics: |
| 12 | + |
| 13 | + class CreateTopicResponse(TypedDict): |
| 14 | + """ |
| 15 | + CreateTopicResponse is the type that wraps the response of the topic that was created |
| 16 | +
|
| 17 | + Attributes: |
| 18 | + id (str): The ID of the created topic |
| 19 | + """ |
| 20 | + |
| 21 | + id: str |
| 22 | + """ |
| 23 | + The ID of the created topic |
| 24 | + """ |
| 25 | + |
| 26 | + class CreateParams(TypedDict): |
| 27 | + name: str |
| 28 | + """ |
| 29 | + The topic name. Max length is 50 characters. |
| 30 | + """ |
| 31 | + default_subscription: str |
| 32 | + """ |
| 33 | + The default subscription preference for new contacts. Possible values: opt_in or opt_out. |
| 34 | + This value cannot be changed later. |
| 35 | + """ |
| 36 | + description: NotRequired[str] |
| 37 | + """ |
| 38 | + The topic description. Max length is 200 characters. |
| 39 | + """ |
| 40 | + |
| 41 | + class UpdateTopicResponse(TypedDict): |
| 42 | + """ |
| 43 | + UpdateTopicResponse is the type that wraps the response of the topic that was updated |
| 44 | +
|
| 45 | + Attributes: |
| 46 | + id (str): The ID of the updated topic |
| 47 | + """ |
| 48 | + |
| 49 | + id: str |
| 50 | + """ |
| 51 | + The ID of the updated topic |
| 52 | + """ |
| 53 | + |
| 54 | + class UpdateParams(TypedDict, total=False): |
| 55 | + name: str |
| 56 | + """ |
| 57 | + The topic name. Max length is 50 characters. |
| 58 | + """ |
| 59 | + description: str |
| 60 | + """ |
| 61 | + The topic description. Max length is 200 characters. |
| 62 | + """ |
| 63 | + |
| 64 | + class RemoveTopicResponse(TypedDict): |
| 65 | + """ |
| 66 | + RemoveTopicResponse is the type that wraps the response of the topic that was removed |
| 67 | +
|
| 68 | + Attributes: |
| 69 | + object (str): The object type, "topic" |
| 70 | + id (str): The ID of the removed topic |
| 71 | + deleted (bool): Whether the topic was deleted |
| 72 | + """ |
| 73 | + |
| 74 | + object: str |
| 75 | + """ |
| 76 | + The object type, "topic" |
| 77 | + """ |
| 78 | + id: str |
| 79 | + """ |
| 80 | + The ID of the removed topic |
| 81 | + """ |
| 82 | + deleted: bool |
| 83 | + """ |
| 84 | + Whether the topic was deleted |
| 85 | + """ |
| 86 | + |
| 87 | + class ListParams(TypedDict): |
| 88 | + limit: NotRequired[int] |
| 89 | + """ |
| 90 | + Number of topics to retrieve. Maximum is 100, and minimum is 1. |
| 91 | + """ |
| 92 | + after: NotRequired[str] |
| 93 | + """ |
| 94 | + The ID after which we'll retrieve more topics (for pagination). |
| 95 | + This ID will not be included in the returned list. |
| 96 | + Cannot be used with the before parameter. |
| 97 | + """ |
| 98 | + before: NotRequired[str] |
| 99 | + """ |
| 100 | + The ID before which we'll retrieve more topics (for pagination). |
| 101 | + This ID will not be included in the returned list. |
| 102 | + Cannot be used with the after parameter. |
| 103 | + """ |
| 104 | + |
| 105 | + class ListResponse(TypedDict): |
| 106 | + """ |
| 107 | + ListResponse type that wraps a list of topic objects with pagination metadata |
| 108 | +
|
| 109 | + Attributes: |
| 110 | + object (str): The object type, always "list" |
| 111 | + data (List[Topic]): A list of topic objects |
| 112 | + has_more (bool): Whether there are more results available |
| 113 | + """ |
| 114 | + |
| 115 | + object: str |
| 116 | + """ |
| 117 | + The object type, always "list" |
| 118 | + """ |
| 119 | + data: List[Topic] |
| 120 | + """ |
| 121 | + A list of topic objects |
| 122 | + """ |
| 123 | + has_more: bool |
| 124 | + """ |
| 125 | + Whether there are more results available for pagination |
| 126 | + """ |
| 127 | + |
| 128 | + @classmethod |
| 129 | + def create(cls, params: CreateParams) -> CreateTopicResponse: |
| 130 | + """ |
| 131 | + Create a topic. |
| 132 | + see more: https://resend.com/docs/api-reference/topics/create-topic |
| 133 | +
|
| 134 | + Args: |
| 135 | + params (CreateParams): The topic creation parameters |
| 136 | + - name: The topic name (max 50 characters) |
| 137 | + - default_subscription: The default subscription preference ("opt_in" or "opt_out") |
| 138 | + - description: Optional topic description (max 200 characters) |
| 139 | +
|
| 140 | + Returns: |
| 141 | + CreateTopicResponse: The created topic response with the topic ID |
| 142 | + """ |
| 143 | + |
| 144 | + path = "/topics" |
| 145 | + resp = request.Request[Topics.CreateTopicResponse]( |
| 146 | + path=path, params=cast(Dict[Any, Any], params), verb="post" |
| 147 | + ).perform_with_content() |
| 148 | + return resp |
| 149 | + |
| 150 | + @classmethod |
| 151 | + def get(cls, id: str) -> Topic: |
| 152 | + """ |
| 153 | + Retrieve a single topic by its ID. |
| 154 | + see more: https://resend.com/docs/api-reference/topics/get-topic |
| 155 | +
|
| 156 | + Args: |
| 157 | + id (str): The topic ID |
| 158 | +
|
| 159 | + Returns: |
| 160 | + Topic: The topic object |
| 161 | + """ |
| 162 | + path = f"/topics/{id}" |
| 163 | + resp = request.Request[Topic]( |
| 164 | + path=path, params={}, verb="get" |
| 165 | + ).perform_with_content() |
| 166 | + return resp |
| 167 | + |
| 168 | + @classmethod |
| 169 | + def update(cls, id: str, params: UpdateParams) -> UpdateTopicResponse: |
| 170 | + """ |
| 171 | + Update an existing topic. |
| 172 | + see more: https://resend.com/docs/api-reference/topics/update-topic |
| 173 | +
|
| 174 | + Args: |
| 175 | + id (str): The topic ID |
| 176 | + params (UpdateParams): The topic update parameters |
| 177 | + - name: Optional topic name (max 50 characters) |
| 178 | + - description: Optional topic description (max 200 characters) |
| 179 | +
|
| 180 | + Returns: |
| 181 | + UpdateTopicResponse: The updated topic response with the topic ID |
| 182 | + """ |
| 183 | + path = f"/topics/{id}" |
| 184 | + resp = request.Request[Topics.UpdateTopicResponse]( |
| 185 | + path=path, params=cast(Dict[Any, Any], params), verb="patch" |
| 186 | + ).perform_with_content() |
| 187 | + return resp |
| 188 | + |
| 189 | + @classmethod |
| 190 | + def remove(cls, id: str) -> RemoveTopicResponse: |
| 191 | + """ |
| 192 | + Delete a single topic. |
| 193 | + see more: https://resend.com/docs/api-reference/topics/delete-topic |
| 194 | +
|
| 195 | + Args: |
| 196 | + id (str): The topic ID |
| 197 | +
|
| 198 | + Returns: |
| 199 | + RemoveTopicResponse: The removed topic response |
| 200 | + """ |
| 201 | + path = f"/topics/{id}" |
| 202 | + resp = request.Request[Topics.RemoveTopicResponse]( |
| 203 | + path=path, params={}, verb="delete" |
| 204 | + ).perform_with_content() |
| 205 | + return resp |
| 206 | + |
| 207 | + @classmethod |
| 208 | + def list(cls, params: Optional[ListParams] = None) -> ListResponse: |
| 209 | + """ |
| 210 | + Retrieve a list of topics. |
| 211 | + see more: https://resend.com/docs/api-reference/topics/list-topics |
| 212 | +
|
| 213 | + Args: |
| 214 | + params (Optional[ListParams]): Optional pagination parameters |
| 215 | + - limit: Number of topics to retrieve (max 100, min 1). |
| 216 | + If not provided, all topics will be returned without pagination. |
| 217 | + - after: ID after which to retrieve more topics |
| 218 | + - before: ID before which to retrieve more topics |
| 219 | +
|
| 220 | + Returns: |
| 221 | + ListResponse: A list of topic objects |
| 222 | + """ |
| 223 | + base_path = "/topics" |
| 224 | + query_params = cast(Dict[Any, Any], params) if params else None |
| 225 | + path = PaginationHelper.build_paginated_path(base_path, query_params) |
| 226 | + resp = request.Request[Topics.ListResponse]( |
| 227 | + path=path, params={}, verb="get" |
| 228 | + ).perform_with_content() |
| 229 | + return resp |
0 commit comments