2024-03-15 00:37:40 +00:00
|
|
|
import requests
|
|
|
|
|
2024-03-16 12:38:40 +00:00
|
|
|
from typing import Union, Dict, Any, List
|
2024-03-15 00:37:40 +00:00
|
|
|
|
|
|
|
|
2024-03-16 12:38:40 +00:00
|
|
|
def create_alias(api_token: str, alias_domain: str, alias_description: str, recipient_ids: List[str] | None) -> Union[Dict[str, Any], str]:
|
2024-03-15 00:37:40 +00:00
|
|
|
"""
|
|
|
|
Creates a new alias using the provided API token, domain and description.
|
|
|
|
|
|
|
|
:param api_token: The API token used for authentication
|
|
|
|
:type api_token: str
|
|
|
|
:param alias_domain: The domain for which the alias is to be created
|
|
|
|
:type alias_domain: str
|
|
|
|
:param alias_description: A description for the alias.
|
|
|
|
:type alias_description: str
|
|
|
|
:return: The API response in case of success, or error string in case of failure.
|
|
|
|
:rtype: Dict[str, Any] or str
|
|
|
|
|
|
|
|
|
|
|
|
if successful, grab the ['data']['email'] key, and you're golden.
|
|
|
|
store the ['data']['id'] key to delete the alias later
|
|
|
|
"""
|
|
|
|
headers = {
|
|
|
|
'Authorization': f'Bearer {api_token}',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
|
|
}
|
|
|
|
api_url = 'https://app.addy.io/api/v1/aliases'
|
|
|
|
payload = {
|
|
|
|
'domain': alias_domain,
|
2024-03-16 12:38:40 +00:00
|
|
|
'description': alias_description,
|
2024-03-15 00:37:40 +00:00
|
|
|
}
|
2024-03-16 12:38:40 +00:00
|
|
|
|
|
|
|
if recipient_ids:
|
|
|
|
payload['recipient_ids'] = recipient_ids
|
|
|
|
|
2024-03-15 00:37:40 +00:00
|
|
|
response = requests.post(api_url, headers=headers, json=payload)
|
|
|
|
|
|
|
|
if response.status_code == 201:
|
|
|
|
# Alias created successfully
|
|
|
|
return response.json()
|
|
|
|
else:
|
|
|
|
return f'Failed to create alias: {response.status_code} {response.text}'
|
|
|
|
|
|
|
|
|
|
|
|
def delete_alias(api_token: str, alias_id: str) -> str:
|
|
|
|
"""
|
|
|
|
Deletes an alias using the provided API token and alias ID.
|
|
|
|
|
|
|
|
:param api_token: The API token used for authentication
|
|
|
|
:type api_token: str
|
|
|
|
:param alias_id: The ID of the alias to be deleted
|
|
|
|
:type alias_id: str
|
|
|
|
:return: Success message if the alias is deleted successfully, or an error message otherwise.
|
|
|
|
:rtype: str
|
|
|
|
"""
|
|
|
|
api_url = f'https://app.addy.io/api/v1/aliases/{alias_id}'
|
|
|
|
headers = {
|
|
|
|
'Authorization': f'Bearer {api_token}',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
|
|
}
|
|
|
|
|
|
|
|
response = requests.delete(api_url, headers=headers)
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
return 'Alias deleted successfully.'
|
|
|
|
else:
|
|
|
|
return f'Failed to delete alias: {response.status_code} - {response.text}'
|
|
|
|
|
|
|
|
|
2024-03-16 12:38:40 +00:00
|
|
|
def create_recipient(api_token: str, recipient_email: str) -> Union[Dict[str, Any], str]:
|
|
|
|
"""
|
|
|
|
Creates a new recipient using the provided API token and recipient email.
|
|
|
|
|
|
|
|
grab the ['data']['id'] for setting recipients on aliases, or deleting them
|
|
|
|
|
|
|
|
:param api_token: The API token used for authentication.
|
|
|
|
:type api_token: str
|
|
|
|
:param recipient_email: The email of the recipient to be created.
|
|
|
|
:type recipient_email: str
|
|
|
|
:return: The API response in case of success, or error string in case of failure.
|
|
|
|
:rtype: Dict[str, Any] or str
|
|
|
|
"""
|
|
|
|
headers = {
|
|
|
|
'Authorization': f'Bearer {api_token}',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
|
|
}
|
|
|
|
api_url = 'https://app.addy.io/api/v1/recipients'
|
|
|
|
payload = {
|
|
|
|
'email': recipient_email
|
|
|
|
}
|
|
|
|
response = requests.post(api_url, headers=headers, json=payload)
|
|
|
|
|
|
|
|
if response.status_code == 201:
|
|
|
|
# Recipient created successfully
|
|
|
|
return response.json()
|
|
|
|
else:
|
|
|
|
return f'Failed to create recipient: {response.status_code} {response.text}'
|
|
|
|
|
|
|
|
|
|
|
|
def delete_recipient(api_token: str, recipient_id: str) -> str:
|
|
|
|
"""
|
|
|
|
Deletes a specific recipient using the provided API token and recipient ID.
|
|
|
|
|
|
|
|
:param api_token: The API token used for authentication.
|
|
|
|
:type api_token: str
|
|
|
|
:param recipient_id: The ID of the recipient to be deleted.
|
|
|
|
:type recipient_id: str
|
|
|
|
:return: Success message if the recipient is deleted successfully, or an error message otherwise.
|
|
|
|
:rtype: str
|
|
|
|
"""
|
|
|
|
api_url = f'https://app.addy.io/api/v1/recipients/{recipient_id}'
|
|
|
|
headers = {
|
|
|
|
'Authorization': f'Bearer {api_token}',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
|
|
}
|
|
|
|
|
|
|
|
response = requests.delete(api_url, headers=headers)
|
|
|
|
|
|
|
|
if response.status_code == 204:
|
|
|
|
return 'Recipient deleted successfully.'
|
|
|
|
else:
|
|
|
|
return f'Failed to delete recipient: {response.status_code} - {response.text}'
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_recipients(api_token: str, verified: bool = None) -> Union[Dict[str, Any], str]:
|
|
|
|
"""
|
|
|
|
Retrieves all recipients, optionally filtering by verification status.
|
|
|
|
|
|
|
|
:param api_token: The API token used for authentication.
|
|
|
|
:type api_token: str
|
|
|
|
:param verified: Optional; if specified, filters recipients by their verification status.
|
|
|
|
:type verified: bool
|
|
|
|
:return: The API response in case of success, or error string in case of failure.
|
|
|
|
:rtype: Dict[str, Any] or str
|
|
|
|
"""
|
|
|
|
headers = {
|
|
|
|
'Authorization': f'Bearer {api_token}',
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
'X-Requested-With': 'XMLHttpRequest'
|
|
|
|
}
|
|
|
|
api_url = 'https://app.addy.io/api/v1/recipients'
|
|
|
|
|
|
|
|
# Add query parameters if 'verified' is not None
|
|
|
|
params = {}
|
|
|
|
if verified is not None:
|
|
|
|
params['filter[verified]'] = str(verified).lower()
|
|
|
|
|
|
|
|
response = requests.get(api_url, headers=headers, params=params)
|
|
|
|
|
|
|
|
if response.status_code == 200:
|
|
|
|
# Recipients retrieved successfully
|
|
|
|
return response.json()
|
|
|
|
else:
|
|
|
|
return f'Failed to retrieve recipients: {response.status_code} {response.text}'
|
|
|
|
|
|
|
|
|
|
|
|
# Example usage
|
2024-03-15 00:37:40 +00:00
|
|
|
if __name__ == "__main__":
|
2024-03-16 12:38:40 +00:00
|
|
|
api_token = 'insert_api_key'
|
|
|
|
recipient_id = '46eebc50-f7f8-46d7-beb9-c37f04c29a84' # Example recipient ID
|
|
|
|
|
|
|
|
response = delete_recipient(api_token, recipient_id)
|
|
|
|
print(response)
|
|
|
|
|
|
|
|
api_token = 'insert_api_key'
|
|
|
|
recipient_email = 'me@example.com'
|
|
|
|
|
|
|
|
response = create_recipient(api_token, recipient_email)
|
|
|
|
print(response)
|
|
|
|
|
2024-03-15 00:37:40 +00:00
|
|
|
api_token = 'insert_api_key'
|
|
|
|
alias_domain = 'addy.io' # Generates random alias
|
|
|
|
alias_description = 'Randomly generated alias'
|
|
|
|
|
|
|
|
response = create_alias(api_token, alias_domain, alias_description)
|
|
|
|
print(response)
|