-
Notifications
You must be signed in to change notification settings - Fork 146
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added notice in README about go dep bug. * Added endpoints for webhooks in mock server * Change names of some parameters on public methods to make their use clearer. * Changed signature of `GetWebhook()` now returns []string. * Changed signature of `ListWebhooks()` now returns map[string][]string. * Both `GetWebhooks()` and `ListWebhooks()` now handle new and legacy webhooks properly.
- Loading branch information
Showing
9 changed files
with
214 additions
and
66 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
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 |
---|---|---|
|
@@ -626,7 +626,7 @@ func ValidateEmail(apiKey string) (mailgun.EmailVerification, error) { | |
return mv.ValidateEmail(ctx, "[email protected]", false) | ||
} | ||
|
||
func GetWebhook(domain, apiKey string) (string, error) { | ||
func GetWebhook(domain, apiKey string) ([]string, error) { | ||
mg := mailgun.NewMailgun(domain, apiKey) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) | ||
|
@@ -635,7 +635,7 @@ func GetWebhook(domain, apiKey string) (string, error) { | |
return mg.GetWebhook(ctx, "clicked") | ||
} | ||
|
||
func ListWebhooks(domain, apiKey string) (map[string]string, error) { | ||
func ListWebhooks(domain, apiKey string) (map[string][]string, error) { | ||
mg := mailgun.NewMailgun(domain, apiKey) | ||
|
||
ctx, cancel := context.WithTimeout(context.Background(), time.Second*30) | ||
|
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,83 @@ | ||
package mailgun | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/go-chi/chi" | ||
) | ||
|
||
func (ms *MockServer) addWebhookRoutes(r chi.Router) { | ||
r.Route("/domains/{domain}/webhooks", func(r chi.Router) { | ||
r.Get("/", ms.listWebHooks) | ||
r.Post("/", ms.postWebHook) | ||
r.Get("/{webhook}", ms.getWebHook) | ||
r.Put("/{webhook}", ms.putWebHook) | ||
r.Delete("/{webhook}", ms.deleteWebHook) | ||
}) | ||
ms.webhooks = WebHooksListResponse{ | ||
Webhooks: map[string]UrlOrUrls{ | ||
"new-webhook": { | ||
Urls: []string{"http://example.com/new"}, | ||
}, | ||
"legacy-webhook": { | ||
Url: "http://example.com/legacy", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (ms *MockServer) listWebHooks(w http.ResponseWriter, _ *http.Request) { | ||
toJSON(w, ms.webhooks) | ||
} | ||
|
||
func (ms *MockServer) getWebHook(w http.ResponseWriter, r *http.Request) { | ||
resp := WebHookResponse{ | ||
Webhook: UrlOrUrls{ | ||
Urls: ms.webhooks.Webhooks[chi.URLParam(r, "webhook")].Urls, | ||
}, | ||
} | ||
toJSON(w, resp) | ||
} | ||
|
||
func (ms *MockServer) postWebHook(w http.ResponseWriter, r *http.Request) { | ||
if err := r.ParseForm(); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
toJSON(w, okResp{Message: err.Error()}) | ||
return | ||
} | ||
|
||
var urls []string | ||
for _, url := range r.Form["url"] { | ||
urls = append(urls, url) | ||
} | ||
ms.webhooks.Webhooks[r.FormValue("id")] = UrlOrUrls{Urls: urls} | ||
|
||
toJSON(w, okResp{Message: "success"}) | ||
} | ||
|
||
func (ms *MockServer) putWebHook(w http.ResponseWriter, r *http.Request) { | ||
if err := r.ParseForm(); err != nil { | ||
w.WriteHeader(http.StatusBadRequest) | ||
toJSON(w, okResp{Message: err.Error()}) | ||
return | ||
} | ||
|
||
var urls []string | ||
for _, url := range r.Form["url"] { | ||
urls = append(urls, url) | ||
} | ||
ms.webhooks.Webhooks[chi.URLParam(r, "webhook")] = UrlOrUrls{Urls: urls} | ||
|
||
toJSON(w, okResp{Message: "success"}) | ||
} | ||
|
||
func (ms *MockServer) deleteWebHook(w http.ResponseWriter, r *http.Request) { | ||
_, ok := ms.webhooks.Webhooks[chi.URLParam(r, "webhook")] | ||
if !ok { | ||
w.WriteHeader(http.StatusNotFound) | ||
toJSON(w, okResp{Message: "webhook not found"}) | ||
} | ||
|
||
delete(ms.webhooks.Webhooks, chi.URLParam(r, "webhook")) | ||
toJSON(w, okResp{Message: "success"}) | ||
} |
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
Oops, something went wrong.