v1.0.0
This commit is contained in:
commit
e7f605ec8c
292 files changed
+43992
No files matched your search
@@ -0,0 +1,238 @@
|
||||
// Code generated by internal codegen utility; DO NOT EDIT.
|
||||
|
||||
package desdk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"repository.basistech.ru/BASIS/de-golang-sdk/core"
|
||||
"repository.basistech.ru/BASIS/de-golang-sdk/tags/de_task"
|
||||
"repository.basistech.ru/BASIS/de-golang-sdk/tags/session"
|
||||
)
|
||||
|
||||
const (
|
||||
// DefaultTaskPollInterval is the default upper bound for WaitForTask polling delays.
|
||||
DefaultTaskPollInterval = 30 * time.Second
|
||||
|
||||
taskPollIntervalInitial = time.Second
|
||||
taskPollIntervalMultiplier = 2
|
||||
)
|
||||
|
||||
// Each SDK major version targets a specific DE release.
|
||||
//
|
||||
// For example, SDK 1.x.x supports DE 1.8.x.
|
||||
const (
|
||||
supportedSDKMajor = 1
|
||||
|
||||
supportedDEMajor = 1
|
||||
supportedDEMinor = 8
|
||||
)
|
||||
|
||||
func (c *Client) WaitForTask(ctx context.Context, taskID int64, maxPollInterval time.Duration) (de_task.Task, error) {
|
||||
if ctx == nil {
|
||||
return de_task.Task{}, fmt.Errorf("nil context")
|
||||
}
|
||||
if c == nil {
|
||||
return de_task.Task{}, fmt.Errorf("nil client")
|
||||
}
|
||||
if c.DeTask == nil {
|
||||
return de_task.Task{}, fmt.Errorf("de_task service is nil; call NewClient or NewClientWithConfig")
|
||||
}
|
||||
if taskID <= 0 {
|
||||
return de_task.Task{}, fmt.Errorf("task id must be positive")
|
||||
}
|
||||
if maxPollInterval <= 0 {
|
||||
maxPollInterval = DefaultTaskPollInterval
|
||||
}
|
||||
|
||||
pollInterval := taskPollIntervalInitial
|
||||
if pollInterval > maxPollInterval {
|
||||
pollInterval = maxPollInterval
|
||||
}
|
||||
|
||||
for {
|
||||
if err := ctx.Err(); err != nil {
|
||||
return de_task.Task{}, err
|
||||
}
|
||||
|
||||
task, err := c.DeTask.Get(ctx, de_task.GetRequest{ID: taskID})
|
||||
if err != nil {
|
||||
if ctxErr := ctx.Err(); ctxErr != nil {
|
||||
return de_task.Task{}, ctxErr
|
||||
}
|
||||
return de_task.Task{}, fmt.Errorf("get task: %w", err)
|
||||
}
|
||||
|
||||
if task.Finished {
|
||||
if task.DoneWithError {
|
||||
if task.ErrorMessage == "" {
|
||||
return task, fmt.Errorf("task finished with error")
|
||||
}
|
||||
return task, fmt.Errorf("task finished with error: %s", task.ErrorMessage)
|
||||
}
|
||||
return task, nil
|
||||
}
|
||||
|
||||
timer := time.NewTimer(pollInterval)
|
||||
select {
|
||||
case <-ctx.Done():
|
||||
if !timer.Stop() {
|
||||
select {
|
||||
case <-timer.C:
|
||||
default:
|
||||
}
|
||||
}
|
||||
return de_task.Task{}, ctx.Err()
|
||||
case <-timer.C:
|
||||
}
|
||||
|
||||
pollInterval = nextTaskPollInterval(pollInterval, maxPollInterval)
|
||||
}
|
||||
}
|
||||
|
||||
func nextTaskPollInterval(current, max time.Duration) time.Duration {
|
||||
if current >= max || current*taskPollIntervalMultiplier >= max {
|
||||
return max
|
||||
}
|
||||
return current * taskPollIntervalMultiplier
|
||||
}
|
||||
|
||||
func (c *Client) IssueToken(ctx context.Context, username, password string) (string, error) {
|
||||
response, err := c.Session.Create(ctx, session.CreateRequest{Username: username, Password: password})
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("create session: %w", err)
|
||||
}
|
||||
if response.Token == "" {
|
||||
return "", fmt.Errorf("create session: response contains no token")
|
||||
}
|
||||
return response.Token, nil
|
||||
}
|
||||
|
||||
// SetToken configures the bearer token without validating it or checking compatibility.
|
||||
func (c *Client) SetToken(token string) {
|
||||
c.cfg.AddDefaultHeader("Authorization", "Bearer "+token)
|
||||
}
|
||||
|
||||
// CheckCompatibility verifies access to DE with the configured token and
|
||||
// checks that this SDK version supports the connected DE version.
|
||||
//
|
||||
// An explicit check is performed even when SkipCompatibilityCheck is enabled.
|
||||
func (c *Client) CheckCompatibility(ctx context.Context) error {
|
||||
if ctx == nil {
|
||||
return fmt.Errorf("nil context")
|
||||
}
|
||||
if c == nil {
|
||||
return fmt.Errorf("nil client")
|
||||
}
|
||||
if c.cfg == nil {
|
||||
return fmt.Errorf("nil client configuration")
|
||||
}
|
||||
if c.DeDeployment == nil {
|
||||
return fmt.Errorf("de_deployment service is nil; call NewClient or NewClientWithConfig")
|
||||
}
|
||||
authorization := strings.TrimSpace(c.cfg.DefaultHeaders()["Authorization"])
|
||||
if !strings.HasPrefix(authorization, "Bearer ") || strings.TrimSpace(strings.TrimPrefix(authorization, "Bearer ")) == "" {
|
||||
return fmt.Errorf("token is not set")
|
||||
}
|
||||
|
||||
return c.checkCompatibility(ctx, core.SDKVersion())
|
||||
}
|
||||
|
||||
func (c *Client) checkCompatibility(ctx context.Context, sdkVersion string) error {
|
||||
if ctx == nil {
|
||||
return fmt.Errorf("nil context")
|
||||
}
|
||||
|
||||
deployment, err := c.DeDeployment.Get(ctx)
|
||||
if err != nil {
|
||||
return fmt.Errorf("get DE version: %w", err)
|
||||
}
|
||||
|
||||
if sdkVersion != "devel" {
|
||||
sdkMajor, _, ok := versionMajorMinor(sdkVersion)
|
||||
if !ok || sdkMajor != supportedSDKMajor {
|
||||
return fmt.Errorf("incompatible SDK version %q: expected %d.x.x", sdkVersion, supportedSDKMajor)
|
||||
}
|
||||
}
|
||||
|
||||
deMajor, deMinor, ok := versionMajorMinor(deployment.ReleaseVersion)
|
||||
if !ok || deMajor != supportedDEMajor || deMinor != supportedDEMinor {
|
||||
return fmt.Errorf("incompatible DE version %q: expected %d.%d.x", deployment.ReleaseVersion, supportedDEMajor, supportedDEMinor)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func versionMajorMinor(version string) (int, int, bool) {
|
||||
version = strings.TrimPrefix(version, "v")
|
||||
if suffix := strings.IndexAny(version, "-+"); suffix >= 0 {
|
||||
version = version[:suffix]
|
||||
}
|
||||
|
||||
parts := strings.Split(version, ".")
|
||||
if len(parts) != 3 {
|
||||
return 0, 0, false
|
||||
}
|
||||
|
||||
major, err := strconv.Atoi(parts[0])
|
||||
if err != nil || major < 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
minor, err := strconv.Atoi(parts[1])
|
||||
if err != nil || minor < 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
patch, err := strconv.Atoi(parts[2])
|
||||
if err != nil || patch < 0 {
|
||||
return 0, 0, false
|
||||
}
|
||||
|
||||
return major, minor, true
|
||||
}
|
||||
|
||||
// AuthenticateWithToken verifies the token and server compatibility before
|
||||
// configuring the token for subsequent requests.
|
||||
//
|
||||
// The verification is skipped when SkipCompatibilityCheck is enabled.
|
||||
func (c *Client) AuthenticateWithToken(ctx context.Context, token string) error {
|
||||
if ctx == nil {
|
||||
return fmt.Errorf("nil context")
|
||||
}
|
||||
if c == nil {
|
||||
return fmt.Errorf("nil client")
|
||||
}
|
||||
if c.cfg == nil {
|
||||
return fmt.Errorf("nil client configuration")
|
||||
}
|
||||
if strings.TrimSpace(token) == "" {
|
||||
return fmt.Errorf("empty token")
|
||||
}
|
||||
|
||||
if !c.cfg.SkipCompatibilityCheck {
|
||||
probeClient := NewClientWithConfig(c.cfg.Clone())
|
||||
probeClient.SetToken(token)
|
||||
if err := probeClient.CheckCompatibility(ctx); err != nil {
|
||||
return fmt.Errorf("check compatibility: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
c.SetToken(token)
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Client) Authenticate(ctx context.Context, username, password string) (string, error) {
|
||||
token, err := c.IssueToken(ctx, username, password)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
|
||||
if err := c.AuthenticateWithToken(ctx, token); err != nil {
|
||||
return "", fmt.Errorf("authenticate with issued token: %w", err)
|
||||
}
|
||||
|
||||
return token, nil
|
||||
}
|
||||
Reference in new issue
Block a user