package client import ( "gitea.24example.ru/spavelit/bpiek/api" "github.com/go-resty/resty/v2" ) const ( AuthApiUrl = "https://bp.iek.ru/oauth/login" BaseApiUrl = "https://bp.iek.ru/api/catalog/v1/" ) type ( Client struct { HTTPClient *resty.Client } AuthSuccess struct { TokenType string `json:"token_type"` AccessToken string `json:"access_token"` IdToken string `json:"id_token"` RefreshToken string `json:"refresh_token"` ExpiresIn int `json:"expires_in"` Scope string `json:"scope"` } ) func NewClient(credentials api.Credentials) *Client { authSuccess := &AuthSuccess{} client := resty.New() client.SetBaseURL(BaseApiUrl) response, err := client.R(). SetFormData(map[string]string{ "username": credentials.Username, "password": credentials.Password, }). SetResult(&authSuccess). Post(AuthApiUrl) if err != nil { panic(err) } if response.StatusCode() != 200 { panic("Auth failed. Invalid credentials") } client.SetHeader("Content-Type", "application/json") client.SetHeader("Authorization", authSuccess.TokenType+" "+authSuccess.AccessToken) return &Client{ HTTPClient: client, } }