-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from mcornick/email
Add get and set email forwarding, thanks again @mcornick!
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
package omglol | ||
|
||
import ( | ||
"bytes" | ||
"encoding/json" | ||
"fmt" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
// Get email forwarding address(es) for a domain. See https://api.omg.lol/#token-get-email-retrieve-forwarding-addresses | ||
func (c *Client) GetEmails(domain string) ([]string, error) { | ||
req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/address/%s/email", c.HostURL, domain), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
var r emailResponse | ||
if err := json.Unmarshal(body, &r); err != nil { | ||
fmt.Printf("error unmarshalling response: %v\n", err) | ||
return nil, err | ||
} | ||
|
||
return r.Response.DestinationArray, nil | ||
} | ||
|
||
// Set email forwarding address(es) for a domain. See https://api.omg.lol/#token-post-email-set-forwarding-addresses | ||
func (c *Client) SetEmails(domain string, destination []string) error { | ||
jsonData := fmt.Sprintf(`{"destination": "%s"}`, strings.Join(destination, ", ")) | ||
req, err := http.NewRequest(http.MethodPost, fmt.Sprintf("%s/address/%s/email", c.HostURL, domain), bytes.NewBuffer([]byte(jsonData))) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
body, err := c.doRequest(req) | ||
if err != nil { | ||
return fmt.Errorf("sent: %s, error: %w", jsonData, err) | ||
} | ||
|
||
var r emailResponse | ||
if err := json.Unmarshal(body, &r); err != nil { | ||
fmt.Printf("error unmarshalling response: %v\n", err) | ||
return err | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package omglol | ||
|
||
import ( | ||
"testing" | ||
) | ||
|
||
func TestGetClearAndSetEmails(t *testing.T) { | ||
sleep() | ||
c, err := NewClient(testEmail, testKey, testHostURL) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
originalDestinations, err := c.GetEmails(testOwnedDomain) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
if originalDestinations != nil { | ||
t.Logf("Original Destinations: %+v\n", originalDestinations) | ||
|
||
} else { | ||
t.Error("Account destination returned 'nil'.") | ||
} | ||
|
||
sleep() | ||
|
||
clear := []string{} | ||
if err := c.SetEmails(testOwnedDomain, clear); err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
sleep() | ||
|
||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
destination, err := c.GetEmails(testOwnedDomain) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
if destination != nil { | ||
t.Logf("Destinations once cleared: %+v\n", destination) | ||
|
||
if len(destination) != 0 { | ||
t.Errorf("Expected %d, got %d", 0, len(destination)) | ||
} | ||
} else { | ||
t.Error("Account destination returned 'nil'.") | ||
} | ||
|
||
if err := c.SetEmails(testOwnedDomain, originalDestinations); err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
finalDestinations, err := c.GetEmails(testOwnedDomain) | ||
if err != nil { | ||
t.Errorf(err.Error()) | ||
} | ||
|
||
if finalDestinations != nil { | ||
t.Logf("Final Destinations: %+v\n", finalDestinations) | ||
|
||
} else { | ||
t.Error("Account destination returned 'nil'.") | ||
} | ||
|
||
if !listsHaveSameElements(t, originalDestinations, finalDestinations) { | ||
t.Errorf("Original: %s does not match final: %s.", originalDestinations, finalDestinations) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters