v1.0.5
This commit is contained in:
1 parent
d340d1b2ad
commit
0a9202c7c0
100 files changed
+2064
-330
No files matched your search
@@ -0,0 +1,98 @@
|
||||
// Code generated by internal codegen utility; DO NOT EDIT.
|
||||
|
||||
package desdk
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"repository.basistech.ru/BASIS/de-golang-sdk/core"
|
||||
)
|
||||
|
||||
// Supported DE version is selected during SDK generation.
|
||||
const (
|
||||
supportedSDKMajor = 1
|
||||
|
||||
supportedDEMajor = 1
|
||||
supportedDEMinor = 8
|
||||
)
|
||||
|
||||
// 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
|
||||
}
|
||||
Reference in new issue
Block a user