bpiek/client/client.go
Pavel Sinitsin e14d4b188b
Add new API and client modules for category and product management
This commit introduces new API and client modules that provide functionality for managing categories and products. It includes the creation of `.gitignore`, necessary data models, API endpoints, and client authentication using go-resty. This forms the foundation for further enhancements to the category and product management features.
2024-09-07 19:14:29 +03:00

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,
}
}