@@ -38,7 +38,7 @@ type Client struct {
38
38
// NewClient creates a new client with your application key (to generate a key, create your application here: https://getpocket.com/developer/apps)
39
39
func NewClient (consumerKey string ) (* Client , error ) {
40
40
if consumerKey == "" {
41
- return nil , fmt . Errorf ( "empty consumer key" )
41
+ return nil , ErrEmptyConsumerKey
42
42
}
43
43
44
44
return & Client {
@@ -83,13 +83,13 @@ func (c *Client) Retrieving(ctx context.Context, input RetrievingInput) ([]Item,
83
83
return nil , err
84
84
}
85
85
86
- return c .getItems (result ), nil
86
+ return c .parseItems (result ), nil
87
87
}
88
88
89
- func (c * Client ) getItems (result gjson.Result ) []Item {
89
+ func (c * Client ) parseItems (result gjson.Result ) []Item {
90
90
var items []Item
91
91
for itemID := range result .Get ("list" ).Map () {
92
- item := newItem ( itemID )
92
+ item := Item { ID : itemID }
93
93
item .fillAllFields (result )
94
94
items = append (items , item )
95
95
}
@@ -100,7 +100,7 @@ func (c *Client) getItems(result gjson.Result) []Item {
100
100
// Authorize returns the Authorization structure with the access token, username and state obtained from the authorization request
101
101
func (c * Client ) Authorize (ctx context.Context , requestToken string ) (Authorization , error ) {
102
102
if requestToken == "" {
103
- return Authorization {}, fmt . Errorf ( "empty request token" )
103
+ return Authorization {}, ErrEmptyRequestToken
104
104
}
105
105
106
106
body := requestAuthorization {
@@ -118,7 +118,7 @@ func (c *Client) Authorize(ctx context.Context, requestToken string) (Authorizat
118
118
state := result .Get ("state" ).String ()
119
119
120
120
if accessToken == "" {
121
- return Authorization {}, fmt . Errorf ( "empty access token in API response" )
121
+ return Authorization {}, ErrEmptyAccessToken
122
122
}
123
123
124
124
return Authorization {
@@ -131,11 +131,11 @@ func (c *Client) Authorize(ctx context.Context, requestToken string) (Authorizat
131
131
// GetAuthorizationURL returns the url string that is used to grant the user access rights to his Pocket account in your application
132
132
func (c Client ) GetAuthorizationURL (requestToken string ) (string , error ) {
133
133
if requestToken == "" {
134
- return "" , fmt . Errorf ( "empty request token" )
134
+ return "" , ErrEmptyRequestToken
135
135
}
136
136
137
137
if c .redirectURL == "" {
138
- return "" , fmt . Errorf ( "empty redirection URL" )
138
+ return "" , ErrEmptyRedirectURL
139
139
}
140
140
141
141
return fmt .Sprintf (authorizeUrl , requestToken , c .redirectURL ), nil
@@ -146,7 +146,7 @@ func (c Client) GetAuthorizationURL(requestToken string) (string, error) {
146
146
// State - metadata string that will be returned at each subsequent authentication response (if you don't need it, specify an empty string).
147
147
func (c * Client ) GetRequestToken (ctx context.Context , redirectURL string , state string ) (string , error ) {
148
148
if redirectURL == "" {
149
- return "" , fmt . Errorf ( "empty redirect URL" )
149
+ return "" , ErrEmptyRedirectURL
150
150
}
151
151
152
152
c .redirectURL = redirectURL
@@ -164,7 +164,7 @@ func (c *Client) GetRequestToken(ctx context.Context, redirectURL string, state
164
164
165
165
token := result .Get ("code" ).String ()
166
166
if token == "" {
167
- return "" , fmt . Errorf ( "empty request token in API response" )
167
+ return "" , ErrEmptyRequestTokenInResponse
168
168
}
169
169
170
170
return token , nil
@@ -173,20 +173,20 @@ func (c *Client) GetRequestToken(ctx context.Context, redirectURL string, state
173
173
func (c * Client ) doHTTP (ctx context.Context , endpoint string , body interface {}) (gjson.Result , error ) {
174
174
b , err := json .Marshal (body )
175
175
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 ())
177
177
}
178
178
179
179
req , err := http .NewRequestWithContext (ctx , "POST" , host + endpoint , bytes .NewBufferString (string (b )))
180
180
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 ())
182
182
}
183
183
184
184
req .Header .Set ("Content-Type" , "application/json; charset=UTF-8" )
185
185
req .Header .Set ("X-Accept" , "application/json" )
186
186
187
187
resp , err := c .client .Do (req )
188
188
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 ())
190
190
}
191
191
defer resp .Body .Close ()
192
192
@@ -196,12 +196,12 @@ func (c *Client) doHTTP(ctx context.Context, endpoint string, body interface{})
196
196
197
197
respBody , err := io .ReadAll (resp .Body )
198
198
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 ())
200
200
}
201
201
202
202
result := gjson .Parse (string (respBody ))
203
203
if result .String () == "" {
204
- return gjson.Result {}, fmt . Errorf ( "failed to parse response body" )
204
+ return gjson.Result {}, ErrFailedToParseInputBody
205
205
}
206
206
207
207
return result , nil
0 commit comments