Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 24 additions & 42 deletions forwardemail/aliases.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
Expand All @@ -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.
Expand Down
38 changes: 16 additions & 22 deletions forwardemail/domains.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
}
}
}
Loading