75 lines
2.4 KiB
Python
75 lines
2.4 KiB
Python
import requests
|
|
|
|
from typing import Union, Dict, Any
|
|
|
|
|
|
def create_alias(api_token: str, alias_domain: str, alias_description: str) -> Union[Dict[str, Any], str]:
|
|
"""
|
|
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,
|
|
'description': alias_description
|
|
}
|
|
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}'
|
|
|
|
|
|
if __name__ == "__main__":
|
|
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)
|