dex/vendor/github.com/mailgun/mailgun-go/routes.go

132 lines
4.9 KiB
Go
Raw Normal View History

2015-08-18 05:57:27 +05:30
package mailgun
import (
"strconv"
)
// A Route structure contains information on a configured or to-be-configured route.
// The Priority field indicates how soon the route works relative to other configured routes.
// Routes of equal priority are consulted in chronological order.
// The Description field provides a human-readable description for the route.
// Mailgun ignores this field except to provide the description when viewing the Mailgun web control panel.
// The Expression field lets you specify a pattern to match incoming messages against.
// The Actions field contains strings specifying what to do
// with any message which matches the provided expression.
// The CreatedAt field provides a time-stamp for when the route came into existence.
// Finally, the ID field provides a unique identifier for this route.
//
// When creating a new route, the SDK only uses a subset of the fields of this structure.
// In particular, CreatedAt and ID are meaningless in this context, and will be ignored.
// Only Priority, Description, Expression, and Actions need be provided.
type Route struct {
Priority int `json:"priority,omitempty"`
Description string `json:"description,omitempty"`
Expression string `json:"expression,omitempty"`
Actions []string `json:"actions,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
ID string `json:"id,omitempty"`
}
// GetRoutes returns the complete set of routes configured for your domain.
// You use routes to configure how to handle returned messages, or
// messages sent to a specfic address on your domain.
// See the Mailgun documentation for more information.
func (mg *MailgunImpl) GetRoutes(limit, skip int) (int, []Route, error) {
2016-06-28 22:48:18 +05:30
r := newHTTPRequest(generatePublicApiUrl(routesEndpoint))
2015-08-18 05:57:27 +05:30
if limit != DefaultLimit {
2016-06-28 22:48:18 +05:30
r.addParameter("limit", strconv.Itoa(limit))
2015-08-18 05:57:27 +05:30
}
if skip != DefaultSkip {
2016-06-28 22:48:18 +05:30
r.addParameter("skip", strconv.Itoa(skip))
2015-08-18 05:57:27 +05:30
}
2016-06-28 22:48:18 +05:30
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
2015-08-18 05:57:27 +05:30
var envelope struct {
TotalCount int `json:"total_count"`
Items []Route `json:"items"`
}
err := getResponseFromJSON(r, &envelope)
if err != nil {
return -1, nil, err
}
return envelope.TotalCount, envelope.Items, nil
}
// CreateRoute installs a new route for your domain.
// The route structure you provide serves as a template, and
// only a subset of the fields influence the operation.
// See the Route structure definition for more details.
func (mg *MailgunImpl) CreateRoute(prototype Route) (Route, error) {
2016-06-28 22:48:18 +05:30
r := newHTTPRequest(generatePublicApiUrl(routesEndpoint))
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
p := newUrlEncodedPayload()
p.addValue("priority", strconv.Itoa(prototype.Priority))
p.addValue("description", prototype.Description)
p.addValue("expression", prototype.Expression)
2015-08-18 05:57:27 +05:30
for _, action := range prototype.Actions {
2016-06-28 22:48:18 +05:30
p.addValue("action", action)
2015-08-18 05:57:27 +05:30
}
var envelope struct {
Message string `json:"message"`
*Route `json:"route"`
}
err := postResponseFromJSON(r, p, &envelope)
return *envelope.Route, err
}
// DeleteRoute removes the specified route from your domain's configuration.
// To avoid ambiguity, Mailgun identifies the route by unique ID.
// See the Route structure definition and the Mailgun API documentation for more details.
func (mg *MailgunImpl) DeleteRoute(id string) error {
2016-06-28 22:48:18 +05:30
r := newHTTPRequest(generatePublicApiUrl(routesEndpoint) + "/" + id)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
2015-08-18 05:57:27 +05:30
_, err := makeDeleteRequest(r)
return err
}
// GetRouteByID retrieves the complete route definition associated with the unique route ID.
func (mg *MailgunImpl) GetRouteByID(id string) (Route, error) {
2016-06-28 22:48:18 +05:30
r := newHTTPRequest(generatePublicApiUrl(routesEndpoint) + "/" + id)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
2015-08-18 05:57:27 +05:30
var envelope struct {
Message string `json:"message"`
*Route `json:"route"`
}
err := getResponseFromJSON(r, &envelope)
return *envelope.Route, err
}
// UpdateRoute provides an "in-place" update of the specified route.
// Only those route fields which are non-zero or non-empty are updated.
// All other fields remain as-is.
func (mg *MailgunImpl) UpdateRoute(id string, route Route) (Route, error) {
2016-06-28 22:48:18 +05:30
r := newHTTPRequest(generatePublicApiUrl(routesEndpoint) + "/" + id)
r.setClient(mg.Client())
r.setBasicAuth(basicAuthUser, mg.ApiKey())
p := newUrlEncodedPayload()
2015-08-18 05:57:27 +05:30
if route.Priority != 0 {
2016-06-28 22:48:18 +05:30
p.addValue("priority", strconv.Itoa(route.Priority))
2015-08-18 05:57:27 +05:30
}
if route.Description != "" {
2016-06-28 22:48:18 +05:30
p.addValue("description", route.Description)
2015-08-18 05:57:27 +05:30
}
if route.Expression != "" {
2016-06-28 22:48:18 +05:30
p.addValue("expression", route.Expression)
2015-08-18 05:57:27 +05:30
}
if route.Actions != nil {
for _, action := range route.Actions {
2016-06-28 22:48:18 +05:30
p.addValue("action", action)
2015-08-18 05:57:27 +05:30
}
}
// For some reason, this API function just returns a bare Route on success.
// Unsure why this is the case; it seems like it ought to be a bug.
var envelope Route
err := putResponseFromJSON(r, p, &envelope)
return envelope, err
}