add sema and retries
This commit is contained in:
@ -35,33 +35,37 @@ type client struct {
|
||||
http *resty.Client
|
||||
log zerolog.Logger
|
||||
|
||||
ownerID string
|
||||
htmlParseSema chan struct{}
|
||||
releaseSemaDelay time.Duration
|
||||
ownerID string
|
||||
}
|
||||
|
||||
type Config config.Eway
|
||||
|
||||
func New(ctx context.Context, cfg Config, log zerolog.Logger) (client, error) {
|
||||
func New(ctx context.Context, cfg Config, log zerolog.Logger) (*client, error) {
|
||||
httpclient := resty.New().
|
||||
SetDebug(cfg.Debug).
|
||||
SetBaseURL("https://eway.elevel.ru/api")
|
||||
|
||||
c := client{
|
||||
http: httpclient,
|
||||
log: log.With().Str("client", "eway").Logger(),
|
||||
http: httpclient,
|
||||
log: log.With().Str("client", "eway").Logger(),
|
||||
htmlParseSema: make(chan struct{}, 2),
|
||||
releaseSemaDelay: time.Second / 2,
|
||||
}
|
||||
|
||||
if cfg.SessionID == "" || cfg.SessionUser == "" {
|
||||
if cfg.Login == "" || cfg.Password == "" {
|
||||
return client{}, entity.SimpleError("no auth method provided")
|
||||
return nil, entity.SimpleError("no auth method provided")
|
||||
}
|
||||
|
||||
decryptedPassword, err := crypto.Decrypt(cfg.Password)
|
||||
if err != nil {
|
||||
return client{}, fmt.Errorf("decrypting password: %w", err)
|
||||
return nil, fmt.Errorf("decrypting password: %w", err)
|
||||
}
|
||||
err = c.login(ctx, cfg.Login, decryptedPassword)
|
||||
if err != nil {
|
||||
return client{}, err
|
||||
return nil, err
|
||||
}
|
||||
|
||||
log.Info().Msg("login successful")
|
||||
@ -83,10 +87,10 @@ func New(ctx context.Context, cfg Config, log zerolog.Logger) (client, error) {
|
||||
|
||||
c.http.SetCookies(cookies)
|
||||
} else {
|
||||
return client{}, entity.SimpleError("bad configuration: either session_id and session_user should be set or login and password")
|
||||
return nil, entity.SimpleError("bad configuration: either session_id and session_user should be set or login and password")
|
||||
}
|
||||
|
||||
return c, nil
|
||||
return &c, nil
|
||||
}
|
||||
|
||||
type GetGoodsNewParams struct {
|
||||
@ -162,7 +166,7 @@ func mapResponseByOrder(response getGoodsNewResponse) (items []entity.GoodsItemR
|
||||
return items
|
||||
}
|
||||
|
||||
func (c client) GetGoodsRemnants(
|
||||
func (c *client) GetGoodsRemnants(
|
||||
ctx context.Context,
|
||||
productIDs []int,
|
||||
) (out entity.MappedGoodsRemnants, err error) {
|
||||
@ -209,7 +213,7 @@ func (c client) GetGoodsRemnants(
|
||||
return out, nil
|
||||
}
|
||||
|
||||
func (c client) GetGoodsNew(
|
||||
func (c *client) GetGoodsNew(
|
||||
ctx context.Context,
|
||||
params GetGoodsNewParams,
|
||||
) (items []entity.GoodsItemRaw, total int, err error) {
|
||||
@ -252,7 +256,7 @@ func (c client) GetGoodsNew(
|
||||
return mapResponseByOrder(response), response.RecordsTotal, nil
|
||||
}
|
||||
|
||||
func (c client) login(ctx context.Context, user, pass string) error {
|
||||
func (c *client) login(ctx context.Context, user, pass string) error {
|
||||
resp, err := c.http.R().
|
||||
SetDoNotParseResponse(true).
|
||||
SetFormData(map[string]string{
|
||||
@ -282,7 +286,19 @@ type parameterSelector struct {
|
||||
Value string `selector:"div.text-right"`
|
||||
}
|
||||
|
||||
func (c client) GetProductInfo(ctx context.Context, cart int64) (pi entity.GoodsItemInfo, err error) {
|
||||
func (c *client) GetProductInfo(ctx context.Context, cart int64) (pi entity.GoodsItemInfo, err error) {
|
||||
select {
|
||||
case c.htmlParseSema <- struct{}{}:
|
||||
defer func() {
|
||||
go func() {
|
||||
time.Sleep(c.releaseSemaDelay)
|
||||
<-c.htmlParseSema
|
||||
}()
|
||||
}()
|
||||
case <-ctx.Done():
|
||||
return pi, ctx.Err()
|
||||
}
|
||||
|
||||
collector := colly.NewCollector(
|
||||
colly.AllowedDomains("eway.elevel.ru"),
|
||||
colly.AllowURLRevisit(),
|
||||
@ -336,6 +352,8 @@ func (c client) GetProductInfo(ctx context.Context, cart int64) (pi entity.Goods
|
||||
return pi, ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
break
|
||||
}
|
||||
if err != nil {
|
||||
return pi, fmt.Errorf("visiting site: %w", err)
|
||||
|
||||
Reference in New Issue
Block a user