diff --git a/README.md b/README.md index 669ea77..df70496 100644 --- a/README.md +++ b/README.md @@ -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|✔️| diff --git a/omglol/common_test.go b/omglol/common_test.go index f45044c..2f2054d 100644 --- a/omglol/common_test.go +++ b/omglol/common_test.go @@ -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 } \ No newline at end of file diff --git a/omglol/email.go b/omglol/email.go new file mode 100644 index 0000000..611b00c --- /dev/null +++ b/omglol/email.go @@ -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 +} diff --git a/omglol/email_test.go b/omglol/email_test.go new file mode 100644 index 0000000..8cbf00c --- /dev/null +++ b/omglol/email_test.go @@ -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) + } +} diff --git a/omglol/models.go b/omglol/models.go index 24124f9..12528ec 100644 --- a/omglol/models.go +++ b/omglol/models.go @@ -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"`