-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_subscription.go
49 lines (41 loc) · 1.42 KB
/
create_subscription.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package rssapi
import (
"context"
"fmt"
"github.com/pkg/errors"
)
type CreateSubscriptionResponse struct {
OK bool `json:"ok"`
Result struct {
Status string `json:"status"` // e.g. "subscribed"
SubscriptionID string `json:"subscription_id"` // e.g. "22946
FeedType string `json:"feed_type"` // e.g. "rss"
WebhookURL string `json:"webhook_url"` // e.g. "https://example.org/webhook"
URL string `json:"url"` // e.g. "https://www.nasa.gov/rss/dyn/breaking_news.rss"
Info string `json:"info"`
} `json:"result"`
}
// CreateSubscription Create a Subscription.
// TODO: Make `info` to optional.
func (c *Client) CreateSubscription(ctx context.Context, url, info string) (*CreateSubscriptionResponse, error) {
q := map[string]string{
"url": url,
"info": info,
}
spath := fmt.Sprintf("/v1/header/subscribe")
req, err := c.newGETRequest(ctx, spath, q)
if err != nil {
// TODO: Define custom error type.
return nil, errors.Wrap(err, "failed to create GET request")
}
res, err := c.httpClient.Do(req)
if err != nil {
return nil, errors.Wrap(err, "failed to Do http request")
}
// TODO: Check status here...
var createSubscriptionResponse CreateSubscriptionResponse
if err := decodeBody(res, &createSubscriptionResponse); err != nil {
return nil, errors.Wrap(err, "failed to decode response body")
}
return &createSubscriptionResponse, nil
}