You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
decort-golang-sdk/client.go

76 lines
1.6 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package decortsdk
import (
"context"
"errors"
"io"
"net/http"
"strings"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudapi"
"repository.basistech.ru/BASIS/decort-golang-sdk/pkg/cloudbroker"
"github.com/google/go-querystring/query"
"repository.basistech.ru/BASIS/decort-golang-sdk/config"
"repository.basistech.ru/BASIS/decort-golang-sdk/internal/client"
)
// HTTP-client for platform
type DecortClient struct {
decortURL string
client *http.Client
}
// Сlient builder
func New(cfg config.Config) *DecortClient {
if cfg.Retries == 0 {
cfg.Retries = 5
}
return &DecortClient{
decortURL: cfg.DecortURL,
client: client.NewHttpClient(cfg),
}
}
// CloudAPI builder
func (dc *DecortClient) CloudAPI() *cloudapi.CloudAPI {
return cloudapi.New(dc)
}
// CloudBroker builder
func (dc *DecortClient) CloudBroker() *cloudbroker.CloudBroker {
return cloudbroker.New(dc)
}
// DecortApiCall method for sending requests to the platform
func (dc *DecortClient) DecortApiCall(ctx context.Context, method, url string, params interface{}) ([]byte, error) {
values, err := query.Values(params)
if err != nil {
return nil, err
}
body := strings.NewReader(values.Encode())
req, err := http.NewRequestWithContext(ctx, method, dc.decortURL+"/restmachine"+url, body)
if err != nil {
return nil, err
}
resp, err := dc.client.Do(req)
if err != nil {
return nil, err
}
defer resp.Body.Close()
respBytes, err := io.ReadAll(resp.Body)
if err != nil {
return nil, err
}
if resp.StatusCode != 200 {
return nil, errors.New(string(respBytes))
}
return respBytes, nil
}