Skip to content

Commit

Permalink
Some refactoring, and the full implementation of higlights. Fixes #17 ,
Browse files Browse the repository at this point in the history
fixes #15 , fixes #16
  • Loading branch information
ochronus committed Oct 6, 2020
1 parent 07436f7 commit b144318
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 25 deletions.
42 changes: 20 additions & 22 deletions instapaper/bookmarks.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,33 +83,31 @@ func (svc *BookmarkService) List(p BookmarkListRequestParams) (*BookmarkListResp
res, err := svc.Client.Call("/bookmarks/list", params)
if err != nil {
return &BookmarkListResponse{}, err
} else {
var bookmarkList BookmarkListResponse
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, &APIError{
}
var bookmarkList BookmarkListResponse
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, &APIError{
StatusCode: res.StatusCode,
Message: err.Error(),
ErrorCode: ErrHTTPError,
WrappedError: err,
}
}
bodyString := string(bodyBytes)
bookmarkList.RawResponse = bodyString
err = json.Unmarshal([]byte(bodyString), &bookmarkList)
if err != nil {
return &BookmarkListResponse{
RawResponse: bodyString,
}, &APIError{
StatusCode: res.StatusCode,
Message: err.Error(),
ErrorCode: ErrHTTPError,
ErrorCode: ErrUnmarshalError,
WrappedError: err,
}
}
bodyString := string(bodyBytes)
bookmarkList.RawResponse = bodyString
err = json.Unmarshal([]byte(bodyString), &bookmarkList)
if err != nil {
return &BookmarkListResponse{
RawResponse: bodyString,
}, &APIError{
StatusCode: res.StatusCode,
Message: err.Error(),
ErrorCode: ErrUnmarshalError,
WrappedError: err,
}
}
return &bookmarkList, nil
}

return &bookmarkList, nil
}

// GetText returns the specified bookmark's processed text-view HTML, which is always text/html encoded as UTF-8.
Expand Down
2 changes: 1 addition & 1 deletion instapaper/bookmarks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,7 @@ func TestValidResponse(t *testing.T) {
BookmarkID: 123456,
Text: "That said, I do have some feelings on the matter.",
Note: "",
Time: 1601797631,
Time: "1601797631",
Position: 0,
},
},
Expand Down
1 change: 0 additions & 1 deletion instapaper/folders.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ func (svc *FolderService) List() ([]Folder, error) {
WrappedError: err,
}
}
fmt.Println(string(bodyBytes))
return folderList, nil
}

Expand Down
85 changes: 84 additions & 1 deletion instapaper/highlights.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,94 @@
package instapaper

import (
"encoding/json"
"fmt"
"io/ioutil"
"net/url"
"strconv"
)

// Highlight represents a highlight within a bookmark
type Highlight struct {
ID int `json:"highlight_id"`
BookmarkID int `json:"bookmark_id"`
Text string
Note string
Time int
Time json.Number
Position int
}

type HighlightService struct {
Client Client
}

// List fetches all highlights for the specified bookmark
func (svc *HighlightService) List(bookmarkID int) ([]Highlight, error) {
path := fmt.Sprintf("/bookmarks/%d/highlights", bookmarkID)
res, err := svc.Client.Call(path, nil)
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, &APIError{
StatusCode: res.StatusCode,
Message: err.Error(),
ErrorCode: ErrHTTPError,
WrappedError: err,
}
}
var highlightList []Highlight
err = json.Unmarshal(bodyBytes, &highlightList)
if err != nil {
return nil, &APIError{
StatusCode: res.StatusCode,
Message: err.Error(),
ErrorCode: ErrUnmarshalError,
WrappedError: err,
}
}
return highlightList, nil
}

// Delete removes the specified highlight
func (svc *HighlightService) Delete(highlightID int) error {
path := fmt.Sprintf("/highlights/%d/delete", highlightID)
_, err := svc.Client.Call(path, nil)
if err != nil {
return err
}
return nil
}

// Add adds a highlight for the specified bookmark
func (svc *HighlightService) Add(bookmarkID int, text string, position int) (*Highlight, error) {
path := fmt.Sprintf("/bookmarks/%d/highlight", bookmarkID)
params := url.Values{}
params.Set("text", text)
params.Set("position", strconv.Itoa(position))
res, err := svc.Client.Call(path, params)
if err != nil {
return nil, err
}
bodyBytes, err := ioutil.ReadAll(res.Body)
if err != nil {
return nil, &APIError{
StatusCode: res.StatusCode,
Message: err.Error(),
ErrorCode: ErrHTTPError,
WrappedError: err,
}
}
var highlightList []Highlight
err = json.Unmarshal(bodyBytes, &highlightList)
if err != nil {
return nil, &APIError{
StatusCode: res.StatusCode,
Message: err.Error(),
ErrorCode: ErrUnmarshalError,
WrappedError: err,
}
}
return &highlightList[0], nil
}

0 comments on commit b144318

Please sign in to comment.