Skip to content

Commit 42fee76

Browse files
committed
Add Client & CreateSubscription.
0 parents  commit 42fee76

File tree

5 files changed

+159
-0
lines changed

5 files changed

+159
-0
lines changed

Diff for: client.go

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package rssapi
2+
3+
import (
4+
"context"
5+
"encoding/json"
6+
"io"
7+
"log"
8+
"net/http"
9+
"net/url"
10+
"path"
11+
12+
"github.com/pkg/errors"
13+
)
14+
15+
type Client struct {
16+
url *url.URL
17+
apiKey string
18+
httpClient *http.Client
19+
20+
Logger *log.Logger
21+
}
22+
23+
// NewClient returns new RSS API Client.
24+
func NewClient(urlStr, apiKey string, logger *log.Logger) (*Client, error) {
25+
parsedURL, err := url.Parse(urlStr)
26+
if err != nil {
27+
return nil, errors.Wrap(err, "failed to parse url")
28+
}
29+
30+
// TODO: Configure HTTP Client.
31+
httpClient := &http.Client{}
32+
33+
var discardLogger = log.New(io.Discard, "", log.LstdFlags)
34+
if logger == nil {
35+
logger = discardLogger
36+
}
37+
38+
return &Client{
39+
url: parsedURL,
40+
apiKey: apiKey,
41+
httpClient: httpClient,
42+
Logger: logger,
43+
}, nil
44+
}
45+
46+
func (c *Client) newGETRequest(ctx context.Context, spath string, query map[string]string) (*http.Request, error) {
47+
u := *c.url
48+
u.Path = path.Join(c.url.Path, spath)
49+
50+
q := u.Query()
51+
for k, v := range query {
52+
q.Add(k, v)
53+
}
54+
u.RawQuery = q.Encode()
55+
56+
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil)
57+
if err != nil {
58+
return nil, errors.Wrap(err, "failed to create new request")
59+
}
60+
61+
req.Header.Add("X-API-KEY", c.apiKey)
62+
63+
return req, nil
64+
}
65+
66+
func decodeBody(resp *http.Response, out interface{}) error {
67+
defer resp.Body.Close()
68+
decoder := json.NewDecoder(resp.Body)
69+
return decoder.Decode(out)
70+
}

Diff for: create_subscription.go

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

Diff for: example/create_subscription/main.go

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package main
2+
3+
import (
4+
"context"
5+
"github.com/kaito2/rssapi-go"
6+
"log"
7+
"os"
8+
)
9+
10+
func main() {
11+
apiKey := os.Getenv("RSS_API_API_KEY")
12+
if apiKey == "" {
13+
log.Fatal("Please set the environment variable named RSS_API_API_KEY.")
14+
}
15+
16+
client, err := rssapi.NewClient(
17+
"https://api.rssapi.net",
18+
apiKey,
19+
nil,
20+
)
21+
if err != nil {
22+
log.Fatal(err)
23+
}
24+
25+
ctx := context.Background()
26+
res, err := client.CreateSubscription(
27+
ctx, "https://www.nasa.gov/rss/dyn/breaking_news.rss", "test-info")
28+
if err != nil {
29+
log.Fatal(err)
30+
}
31+
32+
log.Println(*res)
33+
}

Diff for: go.mod

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module github.com/kaito2/rssapi-go
2+
3+
go 1.18
4+
5+
require github.com/pkg/errors v0.9.1

Diff for: go.sum

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4=
2+
github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0=

0 commit comments

Comments
 (0)