update after test and add scope check

This commit is contained in:
Pavel 2024-10-18 12:19:58 +03:00
parent ed8959d942
commit 0bda592bbd
4 changed files with 31 additions and 25 deletions

@ -25,18 +25,15 @@ func NewDataUpdaterWC(deps Deps) *DataUpdater {
}
}
// раз в час так как акес токен живет час
func (wc *DataUpdater) Start(ctx context.Context) {
nextStart := calculateTime()
ticker := time.NewTicker(time.Nanosecond * time.Duration(nextStart))
//ticker := time.NewTicker(10 * time.Second)
ticker := time.NewTicker(time.Hour)
defer ticker.Stop()
for {
select {
case <-ticker.C:
wc.processTasks(ctx)
nextStart = calculateTime()
ticker.Reset(time.Nanosecond * time.Duration(nextStart))
case <-ctx.Done():
return
}

@ -1,19 +0,0 @@
package data_updater
import (
"time"
)
func calculateTime() int64 {
now := time.Now()
targetTime := time.Date(now.Year(), now.Month(), now.Day(), 4, 0, 0, 0, now.Location())
if now.After(targetTime) {
targetTime = targetTime.AddDate(0, 0, 1)
}
toTarget := targetTime.Sub(now)
sec := toTarget.Nanoseconds()
return sec
}

@ -45,6 +45,7 @@ func NewBitrixClient(deps BitrixDeps) *Bitrix {
}
}
// todo для выполнения некоторых операций нужен определенный скоуп токена надо тоже проверить мою теорию по правам приложения
// todo растестить этот запрос пока не проходит
// https://dev.1c-bitrix.ru/rest_help/users/user_search.php
func (b *Bitrix) GetUserList(accessToken string, domain string) (*models.ResponseGetListUsers, error) {
@ -646,3 +647,30 @@ func (b *Bitrix) CreateContact(req map[string]map[string]interface{}, accessToke
time.Sleep(b.rateLimiter.Interval)
}
}
func (b *Bitrix) CheckScope(token, domain string) (any, error) {
for {
if b.rateLimiter.Check() {
uri := fmt.Sprintf("https://%s/rest/scope?auth=%s", domain, token)
agent := b.fiberClient.Get(uri)
statusCode, resBody, errs := agent.Bytes()
if len(errs) > 0 {
for _, err := range errs {
b.logger.Error("error check scope", zap.Error(err))
}
return 0, fmt.Errorf("request failed: %v", errs[0])
}
if statusCode != fiber.StatusOK {
errorMessage := fmt.Sprintf("received an incorrect response from check scope: %s", string(resBody))
b.logger.Error(errorMessage, zap.Int("status", statusCode))
return 0, fmt.Errorf(errorMessage)
}
return string(resBody), nil
}
time.Sleep(b.rateLimiter.Interval)
}
}

@ -189,7 +189,7 @@ func Test_GetListUsers(t *testing.T) {
RateLimiter: lim,
})
r, err := b.GetUserList("6a901167007232200072541200000001000007701b6ceba9a960508e993456e9b48a5a", "b24-s5jg6c.bitrix24.ru")
r, err := b.CheckScope("f62e126700723220007254120000000100000733941b7c87be3b4174636b7ffd83d2a0", "b24-s5jg6c.bitrix24.ru")
if err != nil {
fmt.Println(err)
return