package agentapi import ( "context" "errors" "net/http" ) // Visitors of a status page subscribe to its incidents by email. The status // page server passes their requests on; the API checks its limits, mails // the confirmation and later the incidents. // SubscribeRequest asks for an email subscription to the page at Host, or // Slug if Host is empty. type SubscribeRequest struct { Host string `json:"host,omitempty"` Slug string `json:"slug,omitempty"` Email string `json:"email"` // IP is the visitor's address, for rate limits. IP string `json:"ip"` // Locale is the language of the mails, as the visitor's browser asks. Locale string `json:"locale,omitempty"` // PageURL is where the visitor saw the page, ending in "/". The API // uses it for the links in mails only where it does not know the // page's address itself. PageURL string `json:"page_url,omitempty"` } // SubscriptionInfo describes a subscription that was confirmed or ended. type SubscriptionInfo struct { PageTitle string `json:"page_title"` Email string `json:"email"` } // ErrInvalidInput means the API refused the request's content, e.g. an // email address that is none. var ErrInvalidInput = errors.New("agentapi: invalid input") type tokenRequest struct { Token string `json:"token"` } // Subscribe asks for a subscription and has the confirmation mailed. It // succeeds whether or not the address was subscribed already, so it tells // nobody who is. ErrRateLimited means a limit was reached, ErrNotFound // that the page takes no subscriptions. func (c *Client) Subscribe(ctx context.Context, r SubscribeRequest) error { return c.do(ctx, http.MethodPost, "/subscriptions", r, nil) } // ConfirmSubscription confirms the subscription token was mailed for. func (c *Client) ConfirmSubscription(ctx context.Context, token string) (SubscriptionInfo, error) { var out SubscriptionInfo err := c.do(ctx, http.MethodPost, "/subscriptions/confirm", tokenRequest{token}, &out) return out, err } // Unsubscribe ends the subscription of an unsubscribe token. func (c *Client) Unsubscribe(ctx context.Context, token string) (SubscriptionInfo, error) { var out SubscriptionInfo err := c.do(ctx, http.MethodPost, "/subscriptions/unsubscribe", tokenRequest{token}, &out) return out, err }