♻️(dimail) rename some methods of DimailAPIClient
Use more intuitive and shorter names for actions available in DimailAPIClient.
This commit is contained in:
@@ -16,7 +16,7 @@ def sync_mailboxes_from_dimail(modeladmin, request, queryset): # pylint: disabl
|
||||
|
||||
for domain in queryset:
|
||||
try:
|
||||
imported_mailboxes = client.synchronize_mailboxes_from_dimail(domain)
|
||||
imported_mailboxes = client.import_mailboxes(domain)
|
||||
except exceptions.HTTPError as err:
|
||||
messages.error(
|
||||
request,
|
||||
|
||||
@@ -36,7 +36,7 @@ class MailboxSerializer(serializers.ModelSerializer):
|
||||
if validated_data["domain"].status == enums.MailDomainStatusChoices.ENABLED:
|
||||
client = DimailAPIClient()
|
||||
# send new mailbox request to dimail
|
||||
response = client.send_mailbox_request(
|
||||
response = client.create_mailbox(
|
||||
validated_data, self.context["request"].user.sub
|
||||
)
|
||||
|
||||
@@ -49,7 +49,7 @@ class MailboxSerializer(serializers.ModelSerializer):
|
||||
mailbox_status = enums.MailDomainStatusChoices.ENABLED
|
||||
|
||||
# send confirmation email
|
||||
client.send_new_mailbox_notification(
|
||||
client.notify_mailbox_creation(
|
||||
recipient=validated_data["secondary_email"], mailbox_data=mailbox_data
|
||||
)
|
||||
|
||||
@@ -96,9 +96,7 @@ class MailDomainSerializer(serializers.ModelSerializer):
|
||||
"""
|
||||
# send new domain request to dimail
|
||||
client = DimailAPIClient()
|
||||
client.send_domain_creation_request(
|
||||
validated_data["name"], self.context["request"].user.sub
|
||||
)
|
||||
client.create_domain(validated_data["name"], self.context["request"].user.sub)
|
||||
|
||||
# no exception raised ? Then actually save domain on our database
|
||||
return models.MailDomain.objects.create(**validated_data)
|
||||
|
||||
@@ -56,7 +56,7 @@ def test_dimail_synchronization__already_sync():
|
||||
status=status.HTTP_200_OK,
|
||||
content_type="application/json",
|
||||
)
|
||||
imported_mailboxes = dimail_client.synchronize_mailboxes_from_dimail(domain)
|
||||
imported_mailboxes = dimail_client.import_mailboxes(domain)
|
||||
|
||||
post_sync_mailboxes = models.Mailbox.objects.filter(domain=domain)
|
||||
assert post_sync_mailboxes.count() == 3
|
||||
@@ -130,7 +130,7 @@ def test_dimail_synchronization__synchronize_mailboxes(mock_warning):
|
||||
content_type="application/json",
|
||||
)
|
||||
|
||||
imported_mailboxes = dimail_client.synchronize_mailboxes_from_dimail(domain)
|
||||
imported_mailboxes = dimail_client.import_mailboxes(domain)
|
||||
|
||||
# 3 imports failed: wrong domain, HeaderParseError, NonASCIILocalPartDefect
|
||||
assert mock_warning.call_count == 3
|
||||
|
||||
@@ -75,9 +75,9 @@ class DimailAPIClient:
|
||||
"Token denied. Please check your MAIL_PROVISIONING_API_CREDENTIALS."
|
||||
)
|
||||
|
||||
return self.pass_dimail_unexpected_response(response)
|
||||
return self.raise_exception_for_unexpected_response(response)
|
||||
|
||||
def send_domain_creation_request(self, domain_name, request_user):
|
||||
def create_domain(self, domain_name, request_user):
|
||||
"""Send a domain creation request to dimail API."""
|
||||
|
||||
payload = {
|
||||
@@ -110,9 +110,9 @@ class DimailAPIClient:
|
||||
)
|
||||
return response
|
||||
|
||||
return self.pass_dimail_unexpected_response(response)
|
||||
return self.raise_exception_for_unexpected_response(response)
|
||||
|
||||
def send_mailbox_request(self, mailbox, user_sub=None):
|
||||
def create_mailbox(self, mailbox, user_sub=None):
|
||||
"""Send a CREATE mailbox request to mail provisioning API."""
|
||||
|
||||
payload = {
|
||||
@@ -155,9 +155,9 @@ class DimailAPIClient:
|
||||
"Permission denied. Please check your MAIL_PROVISIONING_API_CREDENTIALS."
|
||||
)
|
||||
|
||||
return self.pass_dimail_unexpected_response(response)
|
||||
return self.raise_exception_for_unexpected_response(response)
|
||||
|
||||
def pass_dimail_unexpected_response(self, response):
|
||||
def raise_exception_for_unexpected_response(self, response):
|
||||
"""Raise error when encountering an unexpected error in dimail."""
|
||||
try:
|
||||
error_content = json.loads(
|
||||
@@ -173,7 +173,7 @@ class DimailAPIClient:
|
||||
f"Unexpected response from dimail: {response.status_code} {error_content}"
|
||||
)
|
||||
|
||||
def send_new_mailbox_notification(self, recipient, mailbox_data):
|
||||
def notify_mailbox_creation(self, recipient, mailbox_data):
|
||||
"""
|
||||
Send email to confirm mailbox creation
|
||||
and send new mailbox information.
|
||||
@@ -210,7 +210,7 @@ class DimailAPIClient:
|
||||
exception,
|
||||
)
|
||||
|
||||
def synchronize_mailboxes_from_dimail(self, domain):
|
||||
def import_mailboxes(self, domain):
|
||||
"""Synchronize mailboxes from dimail - open xchange to our database.
|
||||
This is useful in case of acquisition of a pre-existing mail domain.
|
||||
Mailboxes created here are not new mailboxes and will not trigger mail notification."""
|
||||
@@ -231,7 +231,7 @@ class DimailAPIClient:
|
||||
raise error
|
||||
|
||||
if response.status_code != status.HTTP_200_OK:
|
||||
return self.pass_dimail_unexpected_response(response)
|
||||
return self.raise_exception_for_unexpected_response(response)
|
||||
|
||||
dimail_mailboxes = ast.literal_eval(
|
||||
response.content.decode("utf-8")
|
||||
|
||||
Reference in New Issue
Block a user