Skip to content

Commit

Permalink
Merge pull request #6 from mcornick/email
Browse files Browse the repository at this point in the history
Add get and set email forwarding, thanks again @mcornick!
  • Loading branch information
ejstreet authored Mar 2, 2023
2 parents d920cbd + 3f470ae commit ebd0cf6
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ This project is a work-in-progress, see the following table for supported featur
||Create|POST|✔️|
||Update|PATCH|✔️|
||Delete|DELETE|✔️|
|Email|Retrieve|GET|✔️|
||Set|POST|✔️|
|PURL|List|GET|✔️|
||Retrieve|GET|✔️|
||Create|POST|✔️|
Expand Down
20 changes: 20 additions & 0 deletions omglol/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,24 @@ func randStringBytes(n int) string {
b[i] = letterBytes[rand.Intn(len(letterBytes))]
}
return string(b)
}

func listsHaveSameElements(t *testing.T, list1 []string, list2 []string) bool {
if len(list1) == 0 && len(list2) == 0 {
t.Log("Both lists are length 0")
return true
}

map2 := make(map[string]bool)
for _, str := range list2 {
map2[str] = true
}

for _, str := range list1 {
if _, ok := map2[str]; !ok {
return false
}
}

return true
}
52 changes: 52 additions & 0 deletions omglol/email.go
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
}
73 changes: 73 additions & 0 deletions omglol/email_test.go
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)
}
}
11 changes: 11 additions & 0 deletions omglol/models.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,17 @@ type dnsChangeResponse struct {
} `json:"response"`
}

type emailResponse struct {
Request request `json:"request"`
Response struct {
Message string `json:"message"`
DestinationString string `json:"destination_string"`
DestinationArray []string `json:"destination_array"`
Address string `json:"address"`
EmailAddress string `json:"email_address"`
} `json:"response"`
}

type Paste struct {
Title string `json:"title"`
Content string `json:"content"`
Expand Down

0 comments on commit ebd0cf6

Please sign in to comment.