1
|
|
|
package devto |
2
|
|
|
|
3
|
|
|
import ( |
4
|
|
|
"context" |
5
|
|
|
"encoding/json" |
6
|
|
|
"fmt" |
7
|
|
|
"net/http" |
8
|
|
|
"strings" |
9
|
|
|
|
10
|
|
|
"github.com/google/go-querystring/query" |
11
|
|
|
) |
12
|
|
|
|
13
|
|
|
// ArticlesResource implements the APIResource interface |
14
|
|
|
// for devto articles. |
15
|
|
|
type ArticlesResource struct { |
16
|
|
|
API *Client |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
// List will return the articles uploaded to devto, the result |
20
|
|
|
// can be narrowed down, filtered or enhanced using query |
21
|
|
|
// parameters as specified on the documentation. |
22
|
|
|
// See: https://docs.dev.to/api/#tag/articles/paths/~1articles/get |
23
|
|
|
func (ar *ArticlesResource) List(ctx context.Context, opt ArticleListOptions) ([]Article, error) { |
24
|
1 |
|
var l []Article |
25
|
1 |
|
q, err := query.Values(opt) |
26
|
1 |
|
if err != nil { |
27
|
|
|
return nil, err |
28
|
|
|
} |
29
|
1 |
|
req, _ := ar.API.NewRequest(http.MethodGet, fmt.Sprintf("api/articles?%s", q.Encode()), nil) |
30
|
1 |
|
res, _ := ar.API.HTTPClient.Do(req) |
31
|
1 |
|
cont := decodeResponse(res) |
32
|
1 |
|
json.Unmarshal(cont, &l) |
33
|
1 |
|
return l, nil |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
// Find will retrieve an Article matching the ID passed. |
37
|
|
|
func (ar *ArticlesResource) Find(ctx context.Context, id uint32) (Article, error) { |
38
|
1 |
|
var art Article |
39
|
1 |
|
req, _ := ar.API.NewRequest(http.MethodGet, fmt.Sprintf("api/articles/%d", id), nil) |
40
|
1 |
|
res, err := ar.API.HTTPClient.Do(req) |
41
|
1 |
|
if err != nil { |
42
|
|
|
return art, err |
43
|
|
|
} |
44
|
1 |
|
cont := decodeResponse(res) |
45
|
1 |
|
json.Unmarshal(cont, &art) |
46
|
1 |
|
return art, nil |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
// New will create a new article on dev.to |
50
|
|
|
func (ar *ArticlesResource) New(ctx context.Context, a Article) (Article, error) { |
51
|
1 |
|
if ar.API.Config.InsecureOnly { |
52
|
1 |
|
return a, ErrProtectedEndpoint |
53
|
|
|
} |
54
|
1 |
|
cont, err := json.Marshal(a) |
55
|
1 |
|
if err != nil { |
56
|
|
|
return a, err |
57
|
|
|
} |
58
|
1 |
|
req, err := ar.API.NewRequest(http.MethodPost, "api/articles", strings.NewReader(string(cont))) |
59
|
1 |
|
if err != nil { |
60
|
|
|
return a, err |
61
|
|
|
} |
62
|
1 |
|
req.Header.Add(APIKeyHeader, ar.API.Config.APIKey) |
63
|
1 |
|
res, err := ar.API.HTTPClient.Do(req) |
64
|
1 |
|
if err != nil { |
65
|
|
|
return a, err |
66
|
|
|
} |
67
|
1 |
|
content := decodeResponse(res) |
68
|
1 |
|
json.Unmarshal(content, &a) |
69
|
1 |
|
return a, nil |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
// Update will mutate the resource by id, and all the changes |
73
|
|
|
// performed to the Article will be applied, thus validation |
74
|
|
|
// on the API side. |
75
|
|
|
func (ar *ArticlesResource) Update(ctx context.Context, a Article) (Article, error) { |
76
|
1 |
|
if ar.API.Config.InsecureOnly { |
77
|
1 |
|
return a, ErrProtectedEndpoint |
78
|
|
|
} |
79
|
1 |
|
cont, err := json.Marshal(a) |
80
|
1 |
|
if err != nil { |
81
|
|
|
return a, err |
82
|
|
|
} |
83
|
1 |
|
req, err := ar.API.NewRequest(http.MethodPut, fmt.Sprintf("api/articles/%d", a.ID), strings.NewReader(string(cont))) |
84
|
1 |
|
if err != nil { |
85
|
|
|
return a, err |
86
|
|
|
} |
87
|
1 |
|
req.Header.Add(APIKeyHeader, ar.API.Config.APIKey) |
88
|
1 |
|
res, err := ar.API.HTTPClient.Do(req) |
89
|
1 |
|
if err != nil { |
90
|
|
|
return a, err |
91
|
|
|
} |
92
|
1 |
|
content := decodeResponse(res) |
93
|
1 |
|
json.Unmarshal(content, &a) |
94
|
1 |
|
return a, nil |
95
|
|
|
} |
96
|
|
|
|