This commit is contained in:
2023-12-21 22:56:24 +03:00
parent 85a42dc405
commit 0bebf8ba8a
7 changed files with 111 additions and 11 deletions

View File

@ -76,7 +76,7 @@ func (c *Client) GetCategories(ctx context.Context) (*CategoriesResponse, error)
}
req.Header.Add("Accept", "application/json")
resp, err := c.httpCli.Do(req)
resp, err := c.httpCli().Do(req)
if err != nil {
return nil, fmt.Errorf("http do: %w", err)
}
@ -108,7 +108,7 @@ func (c *Client) GetSubCategories(ctx context.Context, categoryID int) (*SubCate
}
req.Header.Add("Accept", "application/json")
resp, err := c.httpCli.Do(req)
resp, err := c.httpCli().Do(req)
if err != nil {
return nil, fmt.Errorf("http do: %w", err)
}

View File

@ -1,17 +1,25 @@
package plati
import "net/http"
import (
"math/rand"
"net/http"
)
type Client struct {
httpCli *http.Client
httpClis []*http.Client
token string
sellerID int
}
func New() *Client {
func New(clients []*http.Client) *Client {
return &Client{
httpCli: &http.Client{},
httpClis: clients,
token: "7C731D89FED84B479B89F24F81BB8AF2",
sellerID: 1209592,
}
}
func (c *Client) httpCli() *http.Client {
i := rand.Intn(len(c.httpClis))
return c.httpClis[i]
}

View File

@ -24,7 +24,7 @@ func (c *Client) GetBlockGoodsCategory(ctx context.Context, idC int, idR int, so
dump, err := httputil.DumpRequestOut(req, false)
log.Printf("%s\n", string(dump))
resp, err := c.httpCli.Do(req)
resp, err := c.doWithRetry(req, 200)
if err != nil {
return nil, fmt.Errorf("http do: %w", err)
}
@ -46,6 +46,32 @@ func (c *Client) GetBlockGoodsCategory(ctx context.Context, idC int, idR int, so
return goods, nil
}
func (c *Client) doWithRetry(req *http.Request, expectedCode int) (*http.Response, error) {
attempt := 0
maxAttempts := 5
for {
resp, err := c.httpCli().Do(req)
if err != nil {
if attempt < maxAttempts-1 {
continue
} else {
return resp, err
}
}
if resp.StatusCode != expectedCode {
if attempt < maxAttempts-1 {
resp.Body.Close()
continue
} else {
return resp, err
}
}
return resp, nil
}
}
type Good struct {
Name string
GoodLink string