From b14431895a13afa9ab79e05b64721751b72d7e83 Mon Sep 17 00:00:00 2001 From: Csaba Okrona Date: Tue, 6 Oct 2020 14:41:54 +0200 Subject: [PATCH] Some refactoring, and the full implementation of higlights. Fixes #17 , fixes #15 , fixes #16 --- instapaper/bookmarks.go | 42 +++++++++--------- instapaper/bookmarks_test.go | 2 +- instapaper/folders.go | 1 - instapaper/highlights.go | 85 +++++++++++++++++++++++++++++++++++- 4 files changed, 105 insertions(+), 25 deletions(-) diff --git a/instapaper/bookmarks.go b/instapaper/bookmarks.go index aa6251e..722616a 100644 --- a/instapaper/bookmarks.go +++ b/instapaper/bookmarks.go @@ -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. diff --git a/instapaper/bookmarks_test.go b/instapaper/bookmarks_test.go index 8e13c78..0e7d260 100644 --- a/instapaper/bookmarks_test.go +++ b/instapaper/bookmarks_test.go @@ -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, }, }, diff --git a/instapaper/folders.go b/instapaper/folders.go index 7cc9d4f..94d2556 100644 --- a/instapaper/folders.go +++ b/instapaper/folders.go @@ -56,7 +56,6 @@ func (svc *FolderService) List() ([]Folder, error) { WrappedError: err, } } - fmt.Println(string(bodyBytes)) return folderList, nil } diff --git a/instapaper/highlights.go b/instapaper/highlights.go index 14bd97c..adc596b 100644 --- a/instapaper/highlights.go +++ b/instapaper/highlights.go @@ -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 +}