Skip to content

Commit 4180028

Browse files
author
Lapp
committed
project: refactoring
1 parent 6102638 commit 4180028

File tree

8 files changed

+99
-88
lines changed

8 files changed

+99
-88
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# GetPocket API Golang SDK
22

3-
[![Release](https://img.shields.io/badge/release-v1.0.3-blue)](https://github.com/Lapp-coder/go-pocket-sdk/releases)
3+
[![Release](https://img.shields.io/badge/release-v1.0.4-blue)](https://github.com/Lapp-coder/go-pocket-sdk/releases)
44

55
### The basis of the package was made on code from [this](https://github.com/zhashkevych/go-pocket-sdk) repository.
66

README_RU.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# GetPocket API Golang SDK
22

3-
[![Release](https://img.shields.io/badge/release-v1.0.3-blue)](https://github.com/Lapp-coder/go-pocket-sdk/releases)
3+
[![Release](https://img.shields.io/badge/release-v1.0.4-blue)](https://github.com/Lapp-coder/go-pocket-sdk/releases)
44

55
### Основа пакета была сделана на коде из [этого](https://github.com/zhashkevych/go-pocket-sdk) репозитория.
66

errors.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package go_pocket_sdk
2+
3+
import (
4+
"fmt"
5+
)
6+
7+
var (
8+
ErrEmptyConsumerKey = fmt.Errorf("empty consumer key")
9+
ErrEmptyRequestToken = fmt.Errorf("empty request token")
10+
ErrEmptyAccessToken = fmt.Errorf("empty access token")
11+
ErrEmptyRedirectURL = fmt.Errorf("empty redirect URL")
12+
ErrEmptyRequestTokenInResponse = fmt.Errorf("empty request token in API response")
13+
ErrEmptyItemURL = fmt.Errorf("empty URL for add item")
14+
ErrNoActions = fmt.Errorf("no actions to modify items")
15+
ErrFailedToParseInputBody = fmt.Errorf("failed to parse input body")
16+
)

input.go

Lines changed: 33 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package go_pocket_sdk
22

33
import (
4-
"errors"
54
"strings"
65
)
76

@@ -11,7 +10,7 @@ type AddInput struct {
1110
URL string
1211
Title string
1312
Tags []string
14-
TweetId string
13+
TweetID string
1514
}
1615

1716
// ModifyInput contains the data needed to modify items in the Pocket list
@@ -36,59 +35,59 @@ type RetrievingInput struct {
3635
Offset int
3736
}
3837

39-
func (ai AddInput) generateRequest(consumerKey string) (requestAdd, error) {
40-
if ai.AccessToken == "" {
41-
return requestAdd{}, errors.New("empty access token")
38+
func (i AddInput) generateRequest(consumerKey string) (requestAdd, error) {
39+
if i.AccessToken == "" {
40+
return requestAdd{}, ErrEmptyAccessToken
4241
}
4342

44-
if ai.URL == "" {
45-
return requestAdd{}, errors.New("empty URL")
43+
if i.URL == "" {
44+
return requestAdd{}, ErrEmptyItemURL
4645
}
4746

4847
return requestAdd{
4948
ConsumerKey: consumerKey,
50-
AccessToken: ai.AccessToken,
51-
URL: ai.URL,
52-
Title: ai.Title,
53-
Tags: strings.Join(ai.Tags, ", "),
54-
TweetId: ai.TweetId,
49+
AccessToken: i.AccessToken,
50+
URL: i.URL,
51+
Title: i.Title,
52+
Tags: strings.Join(i.Tags, ", "),
53+
TweetID: i.TweetID,
5554
}, nil
5655
}
5756

58-
func (mi ModifyInput) generateRequest(consumerKey string) (requestModify, error) {
59-
if mi.AccessToken == "" {
60-
return requestModify{}, errors.New("empty access token")
57+
func (i ModifyInput) generateRequest(consumerKey string) (requestModify, error) {
58+
if i.AccessToken == "" {
59+
return requestModify{}, ErrEmptyAccessToken
6160
}
6261

63-
if len(mi.Actions) == 0 {
64-
return requestModify{}, errors.New("no actions to modify")
62+
if len(i.Actions) == 0 {
63+
return requestModify{}, ErrNoActions
6564
}
6665

6766
return requestModify{
6867
ConsumerKey: consumerKey,
69-
AccessToken: mi.AccessToken,
70-
Actions: mi.Actions,
68+
AccessToken: i.AccessToken,
69+
Actions: i.Actions,
7170
}, nil
7271
}
7372

74-
func (ri RetrievingInput) generateRequest(consumerKey string) (requestRetrieving, error) {
75-
if ri.AccessToken == "" {
76-
return requestRetrieving{}, errors.New("empty access token")
73+
func (i RetrievingInput) generateRequest(consumerKey string) (requestRetrieving, error) {
74+
if i.AccessToken == "" {
75+
return requestRetrieving{}, ErrEmptyAccessToken
7776
}
7877

7978
return requestRetrieving{
8079
ConsumerKey: consumerKey,
81-
AccessToken: ri.AccessToken,
82-
State: ri.State,
83-
Favorite: ri.Favorite,
84-
Tag: ri.Tag,
85-
ContentType: ri.ContentType,
86-
Sort: ri.Sort,
87-
DetailType: ri.DetailType,
88-
Search: ri.Search,
89-
Domain: ri.Domain,
90-
Since: ri.Since,
91-
Count: ri.Count,
92-
Offset: ri.Offset,
80+
AccessToken: i.AccessToken,
81+
State: i.State,
82+
Favorite: i.Favorite,
83+
Tag: i.Tag,
84+
ContentType: i.ContentType,
85+
Sort: i.Sort,
86+
DetailType: i.DetailType,
87+
Search: i.Search,
88+
Domain: i.Domain,
89+
Since: i.Since,
90+
Count: i.Count,
91+
Offset: i.Offset,
9392
}, nil
9493
}

pocket.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ type Client struct {
3838
// NewClient creates a new client with your application key (to generate a key, create your application here: https://getpocket.com/developer/apps)
3939
func NewClient(consumerKey string) (*Client, error) {
4040
if consumerKey == "" {
41-
return nil, fmt.Errorf("empty consumer key")
41+
return nil, ErrEmptyConsumerKey
4242
}
4343

4444
return &Client{
@@ -83,13 +83,13 @@ func (c *Client) Retrieving(ctx context.Context, input RetrievingInput) ([]Item,
8383
return nil, err
8484
}
8585

86-
return c.getItems(result), nil
86+
return c.parseItems(result), nil
8787
}
8888

89-
func (c *Client) getItems(result gjson.Result) []Item {
89+
func (c *Client) parseItems(result gjson.Result) []Item {
9090
var items []Item
9191
for itemID := range result.Get("list").Map() {
92-
item := newItem(itemID)
92+
item := Item{ID: itemID}
9393
item.fillAllFields(result)
9494
items = append(items, item)
9595
}
@@ -100,7 +100,7 @@ func (c *Client) getItems(result gjson.Result) []Item {
100100
// Authorize returns the Authorization structure with the access token, username and state obtained from the authorization request
101101
func (c *Client) Authorize(ctx context.Context, requestToken string) (Authorization, error) {
102102
if requestToken == "" {
103-
return Authorization{}, fmt.Errorf("empty request token")
103+
return Authorization{}, ErrEmptyRequestToken
104104
}
105105

106106
body := requestAuthorization{
@@ -118,7 +118,7 @@ func (c *Client) Authorize(ctx context.Context, requestToken string) (Authorizat
118118
state := result.Get("state").String()
119119

120120
if accessToken == "" {
121-
return Authorization{}, fmt.Errorf("empty access token in API response")
121+
return Authorization{}, ErrEmptyAccessToken
122122
}
123123

124124
return Authorization{
@@ -131,11 +131,11 @@ func (c *Client) Authorize(ctx context.Context, requestToken string) (Authorizat
131131
// GetAuthorizationURL returns the url string that is used to grant the user access rights to his Pocket account in your application
132132
func (c Client) GetAuthorizationURL(requestToken string) (string, error) {
133133
if requestToken == "" {
134-
return "", fmt.Errorf("empty request token")
134+
return "", ErrEmptyRequestToken
135135
}
136136

137137
if c.redirectURL == "" {
138-
return "", fmt.Errorf("empty redirection URL")
138+
return "", ErrEmptyRedirectURL
139139
}
140140

141141
return fmt.Sprintf(authorizeUrl, requestToken, c.redirectURL), nil
@@ -146,7 +146,7 @@ func (c Client) GetAuthorizationURL(requestToken string) (string, error) {
146146
// State - metadata string that will be returned at each subsequent authentication response (if you don't need it, specify an empty string).
147147
func (c *Client) GetRequestToken(ctx context.Context, redirectURL string, state string) (string, error) {
148148
if redirectURL == "" {
149-
return "", fmt.Errorf("empty redirect URL")
149+
return "", ErrEmptyRedirectURL
150150
}
151151

152152
c.redirectURL = redirectURL
@@ -164,7 +164,7 @@ func (c *Client) GetRequestToken(ctx context.Context, redirectURL string, state
164164

165165
token := result.Get("code").String()
166166
if token == "" {
167-
return "", fmt.Errorf("empty request token in API response")
167+
return "", ErrEmptyRequestTokenInResponse
168168
}
169169

170170
return token, nil
@@ -173,20 +173,20 @@ func (c *Client) GetRequestToken(ctx context.Context, redirectURL string, state
173173
func (c *Client) doHTTP(ctx context.Context, endpoint string, body interface{}) (gjson.Result, error) {
174174
b, err := json.Marshal(body)
175175
if err != nil {
176-
return gjson.Result{}, fmt.Errorf("an error occurred when marshal the input body: %s", err.Error())
176+
return gjson.Result{}, fmt.Errorf("error occurred when marshal the input body: %s", err.Error())
177177
}
178178

179179
req, err := http.NewRequestWithContext(ctx, "POST", host+endpoint, bytes.NewBufferString(string(b)))
180180
if err != nil {
181-
return gjson.Result{}, fmt.Errorf("an error occurred when creating the query: %s", err.Error())
181+
return gjson.Result{}, fmt.Errorf("error occurred when creating the query: %s", err.Error())
182182
}
183183

184184
req.Header.Set("Content-Type", "application/json; charset=UTF-8")
185185
req.Header.Set("X-Accept", "application/json")
186186

187187
resp, err := c.client.Do(req)
188188
if err != nil {
189-
return gjson.Result{}, fmt.Errorf("an error occurred when sending a request to the Pocket server: %s", err.Error())
189+
return gjson.Result{}, fmt.Errorf("error occurred when sending a request to the Pocket server: %s", err.Error())
190190
}
191191
defer resp.Body.Close()
192192

@@ -196,12 +196,12 @@ func (c *Client) doHTTP(ctx context.Context, endpoint string, body interface{})
196196

197197
respBody, err := io.ReadAll(resp.Body)
198198
if err != nil {
199-
return gjson.Result{}, fmt.Errorf("an error occurred when reading the request body: %s", err.Error())
199+
return gjson.Result{}, fmt.Errorf("error occurred when reading the request body: %s", err.Error())
200200
}
201201

202202
result := gjson.Parse(string(respBody))
203203
if result.String() == "" {
204-
return gjson.Result{}, fmt.Errorf("failed to parse response body")
204+
return gjson.Result{}, ErrFailedToParseInputBody
205205
}
206206

207207
return result, nil

0 commit comments

Comments
 (0)