-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Pinboard notes are basically bookmarks with text bodies. The API only allows listing all or retrieving a singular notes, and the schema between the two are incompatible. Using a single type anyways, the API docs cover this well enough. The date format is unique and requires another unmarshaler (NotesDate).
- Loading branch information
Showing
2 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,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) Notes() ([]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, &Notes{}) | ||
if err != nil { | ||
return []Note{}, fmt.Errorf("Failed to parse Notes response: %v", err) | ||
} | ||
notes := tmp.(*Notes) | ||
return notes.Notes, err | ||
} | ||
|
||
func (p *Pinboard) Note(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 | ||
} |