add client for create or and update tokens need test
This commit is contained in:
parent
f45326b4f8
commit
2dbc93d407
@ -3,7 +3,6 @@ package amoClients
|
|||||||
import (
|
import (
|
||||||
"amocrm/models/amo"
|
"amocrm/models/amo"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
"go.uber.org/zap"
|
"go.uber.org/zap"
|
||||||
@ -52,33 +51,35 @@ func NewAmoClient(deps AmoDeps) *Amo {
|
|||||||
func (a *Amo) GetUserList(req amo.RequestGetListUsers) (*amo.ResponseGetListUsers, error) {
|
func (a *Amo) GetUserList(req amo.RequestGetListUsers) (*amo.ResponseGetListUsers, error) {
|
||||||
url := fmt.Sprintf("%s?page=%d&limit=%d&with=%s", a.urlUserList, req.Page, req.Limit, req.With)
|
url := fmt.Sprintf("%s?page=%d&limit=%d&with=%s", a.urlUserList, req.Page, req.Limit, req.With)
|
||||||
|
|
||||||
resp := a.fiberClient.Get(url)
|
agent := a.fiberClient.Get(url)
|
||||||
|
|
||||||
statusCode, response, errs := resp.Bytes()
|
statusCode, resBody, errs := agent.Bytes()
|
||||||
if errs != nil {
|
if len(errs) > 0 {
|
||||||
a.logger.Error("error GetUserList:", zap.Error(errs[0]))
|
for _, err := range errs {
|
||||||
return nil, errs[0]
|
a.logger.Error("error sending request in GetUserList", zap.Error(err))
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("request GetUserList failed: %v", errs[0])
|
||||||
}
|
}
|
||||||
|
|
||||||
if statusCode != fiber.StatusOK {
|
if statusCode != fiber.StatusOK {
|
||||||
switch statusCode {
|
switch statusCode {
|
||||||
case fiber.StatusForbidden:
|
case fiber.StatusForbidden:
|
||||||
err := errors.New("error GetUserList StatusForbidden - 403")
|
errorMessage := fmt.Sprintf("error GetUserList StatusForbidden - %d", statusCode)
|
||||||
a.logger.Error("error GetUserList:", zap.Error(err))
|
a.logger.Error(errorMessage, zap.Int("status", statusCode))
|
||||||
return nil, err
|
return nil, fmt.Errorf(errorMessage)
|
||||||
case fiber.StatusUnauthorized:
|
case fiber.StatusUnauthorized:
|
||||||
err := errors.New("error GetUserList StatusUnauthorized - 401")
|
errorMessage := fmt.Sprintf("error GetUserList StatusUnauthorized - %d", statusCode)
|
||||||
a.logger.Error("error GetUserList:", zap.Error(err))
|
a.logger.Error(errorMessage, zap.Int("status", statusCode))
|
||||||
return nil, err
|
return nil, fmt.Errorf(errorMessage)
|
||||||
default:
|
default:
|
||||||
err := errors.New(fmt.Sprintf("error GetUserList statusCode- %d", statusCode))
|
errorMessage := fmt.Sprintf("error GetUserList statusCode - %d", statusCode)
|
||||||
a.logger.Error("error GetUserList:", zap.Error(err))
|
a.logger.Error(errorMessage, zap.Int("status", statusCode))
|
||||||
return nil, err
|
return nil, fmt.Errorf(errorMessage)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var userListResponse amo.ResponseGetListUsers
|
var userListResponse amo.ResponseGetListUsers
|
||||||
err := json.Unmarshal(response, &userListResponse)
|
err := json.Unmarshal(resBody, &userListResponse)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
a.logger.Error("error marshal ResponseGetListUsers:", zap.Error(err))
|
a.logger.Error("error marshal ResponseGetListUsers:", zap.Error(err))
|
||||||
return nil, err
|
return nil, err
|
||||||
@ -88,8 +89,40 @@ func (a *Amo) GetUserList(req amo.RequestGetListUsers) (*amo.ResponseGetListUser
|
|||||||
}
|
}
|
||||||
|
|
||||||
// https://www.amocrm.ru/developers/content/oauth/step-by-step
|
// https://www.amocrm.ru/developers/content/oauth/step-by-step
|
||||||
func (a *Amo) CreateWebHook() {
|
// POST /oauth2/access_token
|
||||||
|
// тут и создание по коду и обновление по рефрешу в этом клиенте
|
||||||
|
func (a *Amo) CreateWebHook(req amo.WebHookRequest) (*amo.CreateWebHookResp, error) {
|
||||||
|
bodyBytes, err := json.Marshal(req)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("error marshal req in CreateWebHook:", zap.Error(err))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
agent := a.fiberClient.Post(a.urlCreateWebHook)
|
||||||
|
agent.Set("Content-Type", "application/json").Body(bodyBytes)
|
||||||
|
|
||||||
|
statusCode, resBody, errs := agent.Bytes()
|
||||||
|
if len(errs) > 0 {
|
||||||
|
for _, err = range errs {
|
||||||
|
a.logger.Error("error sending request in CreateWebHook for create or update tokens", zap.Error(err))
|
||||||
|
}
|
||||||
|
return nil, fmt.Errorf("request failed: %v", errs[0])
|
||||||
|
}
|
||||||
|
|
||||||
|
if statusCode != fiber.StatusOK {
|
||||||
|
errorMessage := fmt.Sprintf("received an incorrect response from CreateWebHook: %d", statusCode)
|
||||||
|
a.logger.Error(errorMessage, zap.Int("status", statusCode))
|
||||||
|
return nil, fmt.Errorf(errorMessage)
|
||||||
|
}
|
||||||
|
|
||||||
|
var tokens amo.CreateWebHookResp
|
||||||
|
err = json.Unmarshal(resBody, &tokens)
|
||||||
|
if err != nil {
|
||||||
|
a.logger.Error("error marshal CreateWebHookResp:", zap.Error(err))
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
return &tokens, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// https://www.amocrm.ru/developers/content/oauth/step-by-step#%D0%A5%D1%83%D0%BA-%D0%BE%D0%B1-%D0%BE%D1%82%D0%BA%D0%BB%D1%8E%D1%87%D0%B5%D0%BD%D0%B8%D0%B8-%D0%B8%D0%BD%D1%82%D0%B5%D0%B3%D1%80%D0%B0%D1%86%D0%B8%D0%B8
|
// https://www.amocrm.ru/developers/content/oauth/step-by-step#%D0%A5%D1%83%D0%BA-%D0%BE%D0%B1-%D0%BE%D1%82%D0%BA%D0%BB%D1%8E%D1%87%D0%B5%D0%BD%D0%B8%D0%B8-%D0%B8%D0%BD%D1%82%D0%B5%D0%B3%D1%80%D0%B0%D1%86%D0%B8%D0%B8
|
||||||
|
72
models/amo/createWebHook.go
Normal file
72
models/amo/createWebHook.go
Normal file
@ -0,0 +1,72 @@
|
|||||||
|
package amo
|
||||||
|
|
||||||
|
type CreateWebHookReq struct {
|
||||||
|
ClientID string `json:"client_id"` // id интеграции
|
||||||
|
ClientSecret string `json:"client_secret"` // Секрет интеграции
|
||||||
|
GrantType string `json:"grant_type"` // Тип авторизационных данных (для кода авторизации – authorization_code)
|
||||||
|
Code string `json:"code"` // Полученный код авторизации
|
||||||
|
RedirectUrl string `json:"redirect_uri"` // Redirect URI указанный в настройках интеграции. Должен четко совпадать с тем, что указан в настройках
|
||||||
|
}
|
||||||
|
|
||||||
|
type CreateWebHookResp struct {
|
||||||
|
TokenType string `json:"token_type"` // Тип токена
|
||||||
|
ExpiresIn int `json:"expires_in"` // ttl в секундах
|
||||||
|
AccessToken string `json:"access_token"` // Access Token в формате JWT
|
||||||
|
RefreshToken string `json:"refresh_token"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type UpdateWebHookReq struct {
|
||||||
|
ClientID string `json:"client_id"` // id интеграции
|
||||||
|
ClientSecret string `json:"client_secret"` // Секрет интеграции
|
||||||
|
GrantType string `json:"grant_type"` // Тип авторизационных данных (для кода авторизации – authorization_code)
|
||||||
|
RefreshToken string `json:"refresh_token"` // Refresh токен
|
||||||
|
RedirectUrl string `json:"redirect_uri"` // Redirect URI указанный в настройках интеграции. Должен четко совпадать с тем, что указан в настройках
|
||||||
|
}
|
||||||
|
|
||||||
|
type WebHookRequest interface {
|
||||||
|
GetClientID() string
|
||||||
|
GetClientSecret() string
|
||||||
|
GetGrantType() string
|
||||||
|
GetRedirectURL() string
|
||||||
|
GetToken() string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req CreateWebHookReq) GetClientID() string {
|
||||||
|
return req.ClientID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req CreateWebHookReq) GetClientSecret() string {
|
||||||
|
return req.ClientSecret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req CreateWebHookReq) GetGrantType() string {
|
||||||
|
return req.GrantType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req CreateWebHookReq) GetRedirectURL() string {
|
||||||
|
return req.RedirectUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req CreateWebHookReq) GetToken() string {
|
||||||
|
return req.Code
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req UpdateWebHookReq) GetClientID() string {
|
||||||
|
return req.ClientID
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req UpdateWebHookReq) GetClientSecret() string {
|
||||||
|
return req.ClientSecret
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req UpdateWebHookReq) GetGrantType() string {
|
||||||
|
return req.GrantType
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req UpdateWebHookReq) GetRedirectURL() string {
|
||||||
|
return req.RedirectUrl
|
||||||
|
}
|
||||||
|
|
||||||
|
func (req UpdateWebHookReq) GetToken() string {
|
||||||
|
return req.RefreshToken
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user