-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathImprovMX.py
197 lines (155 loc) · 7.05 KB
/
ImprovMX.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import requests
from typing import Union
from requests.auth import HTTPBasicAuth
class ImprovMX:
def __init__(self, api_user: str = "api", api_key: str = "api_key") -> None:
"""
Initializes an instance of the class with the provided API user and API key.
Args:
api_user (str, optional): The username or identifier for accessing the API.
Defaults to "api" <- api is the default user name for ImprovMX's api might change so i added a argument for it.
api_key (str, optional): The authentication key or token for accessing the API.
Raises:
ValueError: If either `api_user` or `api_key` is not a string.
"""
# Chekcs
if not isinstance(api_user, str) or not api_user.strip():
raise ValueError("API user must be a non-empty string.")
if not isinstance(api_key, str) or not api_key.strip():
raise ValueError("API key must be a non-empty string.")
# User Defines
self.api_user = api_user
self.api_key = api_key
# Built-In Defines
self.base_url = "https://api.improvmx.com/v3"
self.session = requests.Session()
self.__author__ = "PinguLovesYou (https://github.com/ImInTheICU)"
self.__license__ = "MIT"
self.__version__ = "0.0.1"
def list_aliases(self, domain: str = "example.com") -> dict:
"""
Retrieves a list of aliases for the specified domain.
Args:
domain (str, optional): The domain for which to retrieve aliases.
Raises:
ValueError: If `domain` is not a string or if it does not match any of the domains linked over at ImprovMX.
Returns:
dict: A dictionary containing the list of aliases.
"""
# Checks
if not isinstance(domain, str):
raise ValueError("Domain must be a string.")
# Request
response = self.session.request(
method="GET",
url=f"{self.base_url}/domains/{domain}/aliases",
auth=HTTPBasicAuth(self.api_user, self.api_key),
cookies=self.session.cookies,
headers=self.session.headers
)
response.raise_for_status()
return response.json()
def create_alias(self, domain: str = "example.com", alias: Union[str, list] = "test", forward: str = "[email protected]", bulk: bool = False, bulkBehavior: str = None) -> dict:
"""
Creates an alias for the specified domain.
Args:
domain (str, optional): The domain for which to create the alias.
alias (Union[str, list], optional): The alias or list of aliases to be created.
If `bulk` is False, it should be a string. If `bulk` is True, it should be a list of strings.
Defaults to "test".
forward (str): The email address the alias forwards to.
bulk (bool, optional): Indicates whether to create aliases in bulk. Defaults to False.
bulkBehavior (str, optional): The behavior when creating aliases in bulk.
Can be "add" or "update". Defaults to None.
Raises:
ValueError: If input validation fails for any of the parameters.
Returns:
dict: A dictionary containing the response from the API.
"""
# Checks
if not isinstance(domain, str):
raise ValueError("Domain must be a string.")
if not isinstance(forward, str):
raise ValueError("Forward must be a string.")
if bulkBehavior not in (None, "add", "update"):
raise ValueError("bulkBehavior must be None, 'add', or 'update'.")
if bulk and not isinstance(alias, list):
raise ValueError("Alias must be a list if bulk is True.")
if not bulk and not isinstance(alias, str):
raise ValueError("Alias must be a string if bulk is False.")
# Request
payload_key = "aliases" if bulk else "alias"
payload_value = alias if bulk else [alias]
url = f"{self.base_url}/domains/{domain}/aliases" if not bulk else f"{self.base_url}/domains/{domain}/aliases/bulk"
json_payload = {
payload_key: payload_value,
"forward": forward
}
if bulkBehavior:
json_payload["behavior"] = bulkBehavior
response = self.session.request(
method="POST",
url=url,
cookies=self.session.cookies,
headers=self.session.headers,
auth=HTTPBasicAuth(self.api_user, self.api_key),
json=json_payload
)
response.raise_for_status()
return response.json()
def edit_alias(self, domain: str = "example.com", alias: str = "test", forward: str = "[email protected]") -> dict:
"""
Edits an existing alias for the specified domain.
Args:
domain (str, optional): The domain for which the alias belongs.
alias (str, optional): The existing alias to be edited.
forward (str): The new email address the edited alias forwards to.
Raises:
ValueError: If input validation fails for any of the parameters.
Returns:
dict: A dictionary containing the response from the API.
"""
# Checks
if not isinstance(domain, str):
raise ValueError("Domain must be a string.")
if not isinstance(alias, str):
raise ValueError("Alias must be a string.")
if not isinstance(forward, str):
raise ValueError("Forward must be a string.")
# Request
response = self.session.request(
method="PUT",
url=f"{self.base_url}/domains/{domain}/aliases/{alias}",
cookies=self.session.cookies,
headers=self.session.headers,
auth=HTTPBasicAuth(self.api_user, self.api_key),
json={"forward": forward}
)
response.raise_for_status()
return response.json()
def delete_alias(self, domain: str = "example.com", alias: str = "test") -> dict:
"""
Deletes an existing alias for the specified domain.
Args:
domain (str, optional): The domain from which to delete the alias.
alias (str, optional): The existing alias to be deleted.
Raises:
ValueError: If either `domain` or `alias` is not a string.
Returns:
dict: A dictionary containing the response from the API.
"""
# Checks
if not isinstance(domain, str):
raise ValueError("Domain must be a string.")
if not isinstance(alias, str):
raise ValueError("Alias must be a string.")
# Request
response = self.session.request(
method="DELETE",
url=f"{self.base_url}/domains/{domain}/aliases/{alias}",
cookies=self.session.cookies,
headers=self.session.headers,
auth=HTTPBasicAuth(self.api_user, self.api_key),
)
response.raise_for_status()
return response.json()