From 552bbff10d64c5cdf9b0123372bf30fa25b1d8d8 Mon Sep 17 00:00:00 2001 From: RoseSecurity Date: Mon, 2 Mar 2026 09:51:42 -0500 Subject: [PATCH] refactor: extract duplicate parameter encoding logic Extract encodeAliasParams and encodeDomainParams helper functions to reduce code duplication in CreateAlias/UpdateAlias and CreateDomain/UpdateDomain methods. --- forwardemail/aliases.go | 66 +++++++++++++++-------------------------- forwardemail/domains.go | 38 ++++++++++-------------- 2 files changed, 40 insertions(+), 64 deletions(-) diff --git a/forwardemail/aliases.go b/forwardemail/aliases.go index 2de1e26..7665ca2 100644 --- a/forwardemail/aliases.go +++ b/forwardemail/aliases.go @@ -125,29 +125,7 @@ func (c *Client) CreateAlias(ctx context.Context, domain, alias string, paramete params := url.Values{} params.Add("name", alias) - if parameters.Description != "" { - params.Add("description", parameters.Description) - } - - for k, v := range map[string]*bool{ - "has_recipient_verification": parameters.HasRecipientVerification, - "is_enabled": parameters.IsEnabled, - } { - if v != nil { - params.Add(k, strconv.FormatBool(*v)) - } - } - - for k, v := range map[string]*[]string{ - "recipients[]": parameters.Recipients, - "labels[]": parameters.Labels, - } { - if v != nil { - for _, vv := range *v { - params.Add(k, vv) - } - } - } + encodeAliasParams(params, parameters) req, err := c.newRequest(ctx, "POST", fmt.Sprintf("/v1/domains/%s/aliases", encodedDomain), strings.NewReader(params.Encode())) if err != nil { @@ -189,6 +167,29 @@ func (c *Client) UpdateAlias(ctx context.Context, domain, alias string, paramete params := url.Values{} params.Add("name", alias) + encodeAliasParams(params, parameters) + + req, err := c.newRequest(ctx, "PUT", fmt.Sprintf("/v1/domains/%s/aliases/%s", encodedDomain, encodedAlias), strings.NewReader(params.Encode())) + if err != nil { + return nil, fmt.Errorf("failed to create request for UpdateAlias: %w", err) + } + + req.Header.Set("Content-Type", "application/x-www-form-urlencoded") + + res, err := c.doRequest(req) + if err != nil { + return nil, fmt.Errorf("failed to update alias: %w", err) + } + + var item Alias + if err := json.Unmarshal(res, &item); err != nil { + return nil, fmt.Errorf("failed to parse update alias response: %w", err) + } + + return &item, nil +} + +func encodeAliasParams(params url.Values, parameters AliasParameters) { if parameters.Description != "" { params.Add("description", parameters.Description) } @@ -212,25 +213,6 @@ func (c *Client) UpdateAlias(ctx context.Context, domain, alias string, paramete } } } - - req, err := c.newRequest(ctx, "PUT", fmt.Sprintf("/v1/domains/%s/aliases/%s", encodedDomain, encodedAlias), strings.NewReader(params.Encode())) - if err != nil { - return nil, fmt.Errorf("failed to create request for UpdateAlias: %w", err) - } - - req.Header.Set("Content-Type", "application/x-www-form-urlencoded") - - res, err := c.doRequest(req) - if err != nil { - return nil, fmt.Errorf("failed to update alias: %w", err) - } - - var item Alias - if err := json.Unmarshal(res, &item); err != nil { - return nil, fmt.Errorf("failed to parse update alias response: %w", err) - } - - return &item, nil } // DeleteAlias removes an email alias from the specified domain. diff --git a/forwardemail/domains.go b/forwardemail/domains.go index 2df5ab6..cdd7e7f 100644 --- a/forwardemail/domains.go +++ b/forwardemail/domains.go @@ -119,17 +119,7 @@ func (c *Client) CreateDomain(ctx context.Context, name string, parameters Domai params := url.Values{} params.Add("domain", name) - for k, v := range map[string]*bool{ - "has_adult_content_protection": parameters.HasAdultContentProtection, - "has_phishing_protection": parameters.HasPhishingProtection, - "has_executable_protection": parameters.HasExecutableProtection, - "has_virus_protection": parameters.HasVirusProtection, - "has_recipient_verification": parameters.HasRecipientVerification, - } { - if v != nil { - params.Add(k, strconv.FormatBool(*v)) - } - } + encodeDomainParams(params, parameters) req, err := c.newRequest(ctx, "POST", "/v1/domains", strings.NewReader(params.Encode())) if err != nil { @@ -168,17 +158,7 @@ func (c *Client) UpdateDomain(ctx context.Context, name string, parameters Domai params := url.Values{} params.Add("domain", name) - for k, v := range map[string]*bool{ - "has_adult_content_protection": parameters.HasAdultContentProtection, - "has_phishing_protection": parameters.HasPhishingProtection, - "has_executable_protection": parameters.HasExecutableProtection, - "has_virus_protection": parameters.HasVirusProtection, - "has_recipient_verification": parameters.HasRecipientVerification, - } { - if v != nil { - params.Add(k, strconv.FormatBool(*v)) - } - } + encodeDomainParams(params, parameters) req, err := c.newRequest(ctx, "PUT", fmt.Sprintf("/v1/domains/%s", encodedName), strings.NewReader(params.Encode())) if err != nil { @@ -226,3 +206,17 @@ func (c *Client) DeleteDomain(ctx context.Context, name string) error { return nil } + +func encodeDomainParams(params url.Values, parameters DomainParameters) { + for k, v := range map[string]*bool{ + "has_adult_content_protection": parameters.HasAdultContentProtection, + "has_phishing_protection": parameters.HasPhishingProtection, + "has_executable_protection": parameters.HasExecutableProtection, + "has_virus_protection": parameters.HasVirusProtection, + "has_recipient_verification": parameters.HasRecipientVerification, + } { + if v != nil { + params.Add(k, strconv.FormatBool(*v)) + } + } +}