Skip to content

Commit 5f0c48a

Browse files
committed
changed the default time for client to 5 & refactored a code
1 parent fb9db3d commit 5f0c48a

File tree

4 files changed

+38
-31
lines changed

4 files changed

+38
-31
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.idea
2+
.DS_Store

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.0-blue)](https://github.com/Lapp-coder/go-pocket-sdk/releases)
3+
[![Release](https://img.shields.io/badge/release-v1.0.1-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.0-blue)](https://github.com/Lapp-coder/go-pocket-sdk/releases)
3+
[![Release](https://img.shields.io/badge/release-v1.0.1-blue)](https://github.com/Lapp-coder/go-pocket-sdk/releases)
44

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

pocket.go

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ const (
2727
xErrorHeader = "X-Error"
2828
xErrorCodeHeader = "X-Error-Code"
2929

30-
defaultTimeout = time.Second * 10
30+
defaultTimeout = time.Second * 5
3131
)
3232

3333
// Client is a getpocket API client
@@ -74,45 +74,21 @@ func (c *Client) Modify(ctx context.Context, input ModifyInput) error {
7474

7575
// Retrieving retrieves user data (items) Pocket, such as the item id, which is needed to modify items in the Modify function
7676
func (c *Client) Retrieving(ctx context.Context, input RetrievingInput) ([]Item, error) {
77-
var items []Item
78-
7977
req, err := input.generateRequest(c.consumerKey)
8078
if err != nil {
81-
return items, err
79+
return nil, err
8280
}
8381

8482
values, err := c.doHTTP(ctx, endpointRetrieving, req)
8583
if err != nil {
86-
return items, err
84+
return nil, err
8785
}
8886

8987
if values.GetObject("list") == nil {
90-
return items, nil
91-
}
92-
93-
var itemId string
94-
var index int
95-
newJsonStr := values.GetObject("list").String()
96-
for index != -1 {
97-
index = strings.Index(newJsonStr, "{\"item_id\":\"")
98-
if index != -1 {
99-
for i := index + 12; string(newJsonStr[i]) != "\""; i++ {
100-
itemId += string(newJsonStr[i])
101-
}
102-
103-
items = append(items, createItem(itemId, values))
104-
105-
itemId = ""
106-
oldJsonStr := newJsonStr
107-
newJsonStr = ``
108-
109-
for i := index + 12; i != len(oldJsonStr); i++ {
110-
newJsonStr += string(oldJsonStr[i])
111-
}
112-
}
88+
return nil, nil
11389
}
11490

115-
return items, nil
91+
return c.parseItems(values), nil
11692
}
11793

11894
// Authorize returns the Authorization structure with the access token, user name and state obtained from the authorization request
@@ -185,6 +161,36 @@ func (c *Client) GetRequestToken(ctx context.Context, redirectURL string, state
185161
return string(token), nil
186162
}
187163

164+
func (c *Client) parseItems(values *fastjson.Value) []Item {
165+
var (
166+
items []Item
167+
itemId string
168+
index int
169+
)
170+
171+
newJsonStr := values.GetObject("list").String()
172+
for index != -1 {
173+
index = strings.Index(newJsonStr, "{\"item_id\":\"")
174+
if index != -1 {
175+
for i := index + 12; string(newJsonStr[i]) != "\""; i++ {
176+
itemId += string(newJsonStr[i])
177+
}
178+
179+
items = append(items, createItem(itemId, values))
180+
181+
itemId = ""
182+
oldJsonStr := newJsonStr
183+
newJsonStr = ``
184+
185+
for i := index + 12; i != len(oldJsonStr); i++ {
186+
newJsonStr += string(oldJsonStr[i])
187+
}
188+
}
189+
}
190+
191+
return items
192+
}
193+
188194
func (c *Client) doHTTP(ctx context.Context, endpoint string, body interface{}) (*fastjson.Value, error) {
189195
b, err := json.Marshal(body)
190196
if err != nil {

0 commit comments

Comments
 (0)