58 lines
1.1 KiB
Go
58 lines
1.1 KiB
Go
|
package client
|
||
|
|
||
|
import "github.com/go-resty/resty/v2"
|
||
|
|
||
|
// import (
|
||
|
// "net/http"
|
||
|
// "net/url"
|
||
|
// )
|
||
|
|
||
|
const (
|
||
|
AUTH_API_URL = "https://bp.iek.ru/oauth/login"
|
||
|
BASE_API_URL = "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 Credentials) *Client {
|
||
|
authSuccess := &AuthSuccess{}
|
||
|
client := resty.New()
|
||
|
client.SetBaseURL(BASE_API_URL)
|
||
|
|
||
|
response, err := client.R().
|
||
|
SetFormData(map[string]string{
|
||
|
"username": credentials.GetUsername(),
|
||
|
"password": credentials.GetPassword(),
|
||
|
}).
|
||
|
SetResult(&authSuccess).
|
||
|
Post(AUTH_API_URL)
|
||
|
|
||
|
if err != nil {
|
||
|
panic(err)
|
||
|
}
|
||
|
|
||
|
if response.StatusCode() != 200 {
|
||
|
panic("Auth failed. Invalid credentials")
|
||
|
}
|
||
|
|
||
|
client.SetHeader("Content-Type", "application/json")
|
||
|
client.SetHeader("Authorization", "Bearer "+authSuccess.AccessToken)
|
||
|
|
||
|
return &Client{
|
||
|
HTTPClient: client,
|
||
|
}
|
||
|
}
|