-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
201 lines (181 loc) · 4.52 KB
/
client.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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package hatena
import (
"bytes"
"encoding/xml"
"fmt"
"html"
"io"
"net/http"
"os"
"text/template"
"time"
"github.com/pkg/errors"
"github.com/podhmo/hatena/article"
)
// Client :
type Client interface {
List() ([]Entry, error)
Create(article article.Article) (string, error)
Edit(article article.Article, ID string) (string, error)
}
// ClientConfig :
type ClientConfig struct {
HatenaID string
BlogID string
}
// NewClient :
func NewClient(hatenaID, blogID string, dryRun bool, client *http.Client, auth func(*http.Request) error) Client {
config := ClientConfig{HatenaID: hatenaID, BlogID: blogID}
if dryRun {
return &dummyClient{Config: config}
}
return &actualClient{Config: config, Client: client, Auth: auth}
}
// dummyClient :
type dummyClient struct {
Config ClientConfig
}
// List :
func (c *dummyClient) List() ([]Entry, error) {
return nil, nil
}
// Create :
func (c *dummyClient) Create(article article.Article) (string, error) {
fmt.Println("Create: ")
err := tmpl.Execute(os.Stdout, article)
return "xxx", err
}
// Edit :
func (c *dummyClient) Edit(article article.Article, ID string) (string, error) {
fmt.Println("Edit: ")
err := tmpl.Execute(os.Stdout, article)
return ID, err
}
// actualClient :
type actualClient struct {
Config ClientConfig
Client *http.Client
Auth func(*http.Request) error
}
// Link :
type Link struct {
Rel string `xml:"rel,attr,omitempty"`
Href string `xml:"href,attr"`
}
// Entry :
type Entry struct {
Title string `xml:"title"`
Id string `xml:"id"`
Link []Link `xml:"link"`
Updated time.Time `xml:"updated"`
}
type escapedArticle struct {
*article.Article
Body string
}
func escaped(article *article.Article) *escapedArticle {
return &escapedArticle{Article: article, Body: html.EscapeString(article.Body)}
}
// List :
func (c *actualClient) List() ([]Entry, error) {
url := fmt.Sprintf("https://blog.hatena.ne.jp/%s/%s/atom/entry", c.Config.HatenaID, c.Config.BlogID)
req, err := http.NewRequest("GET", url, nil)
if err != nil {
return nil, err
}
if err := c.Auth(req); err != nil {
return nil, err
}
resp, err := c.Client.Do(req)
defer resp.Body.Close()
decoder := xml.NewDecoder(resp.Body)
var feed struct {
Entries []Entry `xml:"entry"`
}
err = decoder.Decode(&feed)
if err != nil {
io.Copy(os.Stdout, resp.Body)
return nil, errors.Wrap(err, "parse error")
}
return feed.Entries, nil
}
// Create :
func (c *actualClient) Create(article article.Article) (string, error) {
url := fmt.Sprintf("https://blog.hatena.ne.jp/%s/%s/atom/entry", c.Config.HatenaID, c.Config.BlogID)
var buf bytes.Buffer
err := tmpl.Execute(&buf, escaped(&article))
if err != nil {
return "", err
}
req, err := http.NewRequest("POST", url, &buf)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/xml; charset=utf-8")
if err := c.Auth(req); err != nil {
return "", err
}
resp, err := c.Client.Do(req)
defer resp.Body.Close()
if resp.StatusCode != http.StatusCreated {
io.Copy(os.Stdout, resp.Body)
return "", errors.Errorf("something wrong: %s", resp.Status)
}
fmt.Println(resp.Status)
decoder := xml.NewDecoder(resp.Body)
var entry Entry
err = decoder.Decode(&entry)
if err != nil {
io.Copy(os.Stdout, resp.Body)
return "", errors.Wrap(err, "parse error")
}
for _, link := range entry.Link {
if link.Rel == "edit" {
return link.Href, err
}
}
return "", errors.Errorf("hmm.")
}
// Edit :
func (c *actualClient) Edit(article article.Article, url string) (string, error) {
var buf bytes.Buffer
err := tmpl.Execute(&buf, escaped(&article))
if err != nil {
return "", err
}
req, err := http.NewRequest("PUT", url, &buf)
if err != nil {
return "", err
}
req.Header.Add("Content-Type", "application/xml; charset=utf-8")
if err := c.Auth(req); err != nil {
return "", err
}
resp, err := c.Client.Do(req)
defer resp.Body.Close()
fmt.Println(resp.Status)
if resp.StatusCode != http.StatusOK {
io.Copy(os.Stdout, resp.Body)
return "", errors.Errorf("something wrong: %s", resp.Status)
}
return url, err
}
var tmpl *template.Template
func init() {
body := `<?xml version="1.0" encoding="utf-8"?>
<entry xmlns="http://www.w3.org/2005/Atom"
xmlns:app="http://www.w3.org/2007/app">
<title>{{.Title.Title}}</title>
<author><name>name</name></author>
<content type="text/plain">
{{.Body}}
</content>
{{range .Title.Categories}}
<category term="{{.}}" />
{{end}}
<app:control>
<app:draft>no</app:draft>
</app:control>
</entry>`
tmpl = template.Must(template.New("req.tmpl").Parse(body))
}