-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathnotes.go
148 lines (122 loc) · 2.92 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
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
package pinboard
import (
"encoding/json"
"reflect"
"strconv"
"time"
)
// Note represents a Pinboard note.
type Note struct {
// Unique ID of the note.
ID string
// Title of the note.
Title string
// 20 character long sha1 hash of the note text.
Hash []byte
// Time the note was created.
CreatedAt time.Time
// Time the note was updated.
UpdatedAt time.Time
// Character length of the note.
Length int
// Body text of the note.
//
// Note: only /notes/ID returns body text.
Text []byte
}
// note holds intermediate data for preprocessing types because JSON
// is so cool.
type note struct {
ID string `json:"id,omitempty"`
Title string `json:"title,omitempty"`
Hash string `json:"hash,omitempty"`
CreatedAt string `json:"created_at,omitempty"`
UpdatedAt string `json:"updated_at,omitempty"`
Length interface{} `json:"length,omitempty"`
Text string `json:"text,omitempty"`
}
// notesResponse holds notes responses from the Pinboard API.
type notesResponse struct {
Count int
Notes []note
}
// parseNote takes the note as JSON data returned from the Pinboard
// API and translates them into Notes with proper types.
func parseNote(n note) (*Note, error) {
var note Note
note.ID = n.ID
note.Title = n.Title
note.Hash = []byte(n.Hash)
note.Text = []byte(n.Text)
layout := "2006-01-02 15:04:05"
created, err := time.Parse(layout, n.CreatedAt)
if err != nil {
return nil, err
}
note.CreatedAt = created
updated, err := time.Parse(layout, n.UpdatedAt)
if err != nil {
return nil, err
}
note.UpdatedAt = updated
switch v := reflect.ValueOf(n.Length); v.Kind() {
case reflect.String:
length, err := strconv.Atoi(n.Length.(string))
if err != nil {
return nil, err
}
note.Length = length
case reflect.Float64:
note.Length = int(n.Length.(float64))
}
return ¬e, nil
}
// NotesList returns a list of the user's notes.
//
// https://pinboard.in/api/#notes_list
func NotesList() ([]*Note, error) {
resp, err := get("notesList", nil)
if err != nil {
return nil, err
}
var nr notesResponse
err = json.Unmarshal(resp, &nr)
if err != nil {
return nil, err
}
// Parse returned untyped notes into Notes.
var notes []*Note
for _, n := range nr.Notes {
note, err := parseNote(n)
if err != nil {
return nil, err
}
notes = append(notes, note)
}
return notes, nil
}
// notesIDOptions represents the single required argument for
// /notes/ID.
type notesIDOptions struct {
ID string
}
// NotesID returns an individual user note. The hash property is a 20
// character long sha1 hash of the note text.
//
// https://pinboard.in/api/#notes_get
func NotesID(id string) (*Note, error) {
resp, err := get("notesID", ¬esIDOptions{ID: id})
if err != nil {
return nil, err
}
var n note
err = json.Unmarshal(resp, &n)
if err != nil {
return nil, err
}
note, err := parseNote(n)
if err != nil {
return nil, err
}
return note, nil
}