This repository has been archived by the owner on Aug 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publisher.go
112 lines (100 loc) · 2.7 KB
/
publisher.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"net/http"
)
// Publisher publishes article in dev.to.
type Publisher struct {
key string
client *Client
}
// NewPublisher constructor.
func NewPublisher(key string) *Publisher {
client, err := NewClient("https://dev.to/api")
if err != nil {
log.Fatal("init DevTo client: ", err)
}
return &Publisher{key: key, client: client}
}
// MyArticles lists all articles for the current user.
func (p *Publisher) MyArticles() ([]Article, error) {
resp, err := p.client.GetUserAllArticles(
context.Background(),
&GetUserAllArticlesParams{},
func(ctx context.Context, req *http.Request) error {
req.Header.Add("api-key", p.key)
return nil
})
if err != nil {
return nil, fmt.Errorf("obtaining articles: %v", err)
}
if resp.StatusCode != http.StatusOK {
return nil, fmt.Errorf("unexpected status code: %v", resp.StatusCode)
}
var data []ArticleMe
if err := json.NewDecoder(resp.Body).Decode(&data); err != nil {
return nil, fmt.Errorf("decoding server response: %v", err)
}
var out []Article
for _, am := range data {
out = append(out, articleMeToArticle(am))
}
return out, nil
}
func articleMeToArticle(am ArticleMe) Article {
return Article{
Title: am.Title,
Tags: am.TagList,
Published: am.Published,
Text: am.BodyMarkdown,
}
}
// TODO: parse response.
type PublishedInfo struct {
ID int
URL string
ReadingTimeMinutes int
}
// Publish an article.
func (p *Publisher) Publish(a Article) error {
body := CreateArticleJSONRequestBody{
Article: &struct {
BodyMarkdown *string `json:"body_markdown,omitempty"`
CanonicalUrl *string `json:"canonical_url,omitempty"`
Description *string `json:"description,omitempty"`
MainImage *string `json:"main_image,omitempty"`
OrganizationId *int32 `json:"organization_id,omitempty"`
Published *bool `json:"published,omitempty"`
Series *string `json:"series,omitempty"`
Tags *[]string `json:"tags,omitempty"`
Title *string `json:"title,omitempty"`
}{
BodyMarkdown: &a.Text,
CanonicalUrl: nil,
Description: nil,
MainImage: nil,
OrganizationId: nil,
Published: &a.Published,
Series: nil,
Tags: nil,
Title: &a.Title,
},
}
resp, err := p.client.CreateArticle(
context.Background(),
body,
func(ctx context.Context, req *http.Request) error {
req.Header.Add("api-key", p.key)
return nil
})
if err != nil {
return fmt.Errorf("posting article: %v", err)
}
if resp.StatusCode != http.StatusCreated {
return fmt.Errorf("unexpected status code: %v", resp.StatusCode)
}
return nil
}