-
Notifications
You must be signed in to change notification settings - Fork 2
/
notes.go
66 lines (56 loc) · 1.47 KB
/
notes.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
package pinboard
import (
"encoding/xml"
"fmt"
"net/url"
"regexp"
)
type notes struct {
XMLName xml.Name `xml:"notes"`
Notes []Note `xml:"note"`
}
type Note struct {
XMLName xml.Name `xml:"note"`
ID string `xml:"id,attr"`
Title string `xml:"title"`
Hash string `xml:"hash"`
Created notesDate `xml:"created_at"`
Updated notesDate `xml:"updated_at"`
Length int `xml:"length"`
Text string `xml:"text"`
}
func (p *Pinboard) NotesList() ([]Note, error) {
u, err := url.Parse(apiBase + "notes/list")
if err != nil {
return []Note{}, fmt.Errorf("Failed to parse Notes list API URL: %v", err)
}
resp, err := p.get(u)
if err != nil {
return []Note{}, err
}
tmp, err := parseResponse(resp, ¬es{})
if err != nil {
return []Note{}, fmt.Errorf("Failed to parse Notes response: %v", err)
}
no := tmp.(*notes)
return no.Notes, err
}
func (p *Pinboard) NotesGet(noteID string) (Note, error) {
if m, _ := regexp.Match("[a-z0-9]{20}", []byte(noteID)); !m {
return Note{}, fmt.Errorf("Note ID must be a 20 character sha1 hash")
}
u, err := url.Parse(apiBase + "notes/" + noteID)
if err != nil {
return Note{}, fmt.Errorf("Failed to parse note URL: %v", err)
}
resp, err := p.get(u)
if err != nil {
return Note{}, fmt.Errorf("Error getting note: %v", err)
}
tmp, err := parseResponse(resp, &Note{})
if err != nil {
return Note{}, fmt.Errorf("Failed to parse Note response: %v", err)
}
note := tmp.(*Note)
return *note, err
}