From 0bda592bbda0451c0d8470cbb711883002fc7407 Mon Sep 17 00:00:00 2001 From: Pavel Date: Fri, 18 Oct 2024 12:19:58 +0300 Subject: [PATCH] update after test and add scope check --- internal/workers/data_updater/data_updater.go | 7 ++--- internal/workers/data_updater/timer.go | 19 ------------- pkg/bitrixClient/bitrix.go | 28 +++++++++++++++++++ pkg/bitrixClient/bitrix_test.go | 2 +- 4 files changed, 31 insertions(+), 25 deletions(-) delete mode 100644 internal/workers/data_updater/timer.go diff --git a/internal/workers/data_updater/data_updater.go b/internal/workers/data_updater/data_updater.go index eee7daa..c34e367 100644 --- a/internal/workers/data_updater/data_updater.go +++ b/internal/workers/data_updater/data_updater.go @@ -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 } diff --git a/internal/workers/data_updater/timer.go b/internal/workers/data_updater/timer.go deleted file mode 100644 index ae4a113..0000000 --- a/internal/workers/data_updater/timer.go +++ /dev/null @@ -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 -} diff --git a/pkg/bitrixClient/bitrix.go b/pkg/bitrixClient/bitrix.go index 56e60c3..6c4a96e 100644 --- a/pkg/bitrixClient/bitrix.go +++ b/pkg/bitrixClient/bitrix.go @@ -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) + } +} diff --git a/pkg/bitrixClient/bitrix_test.go b/pkg/bitrixClient/bitrix_test.go index e82534b..000feb4 100644 --- a/pkg/bitrixClient/bitrix_test.go +++ b/pkg/bitrixClient/bitrix_test.go @@ -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