Skip to content

Commit

Permalink
feat: optimize code
Browse files Browse the repository at this point in the history
  • Loading branch information
devhaozi committed Nov 29, 2024
1 parent a4aeb41 commit 404dd32
Show file tree
Hide file tree
Showing 195 changed files with 169 additions and 8,668 deletions.
3 changes: 2 additions & 1 deletion provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,9 @@ import (
"slices"
"time"

"github.com/libdns/huaweicloud/sdk/services/dns/v2/model"
"github.com/libdns/libdns"

"github.com/libdns/huaweicloud/sdk/services/dns/v2/model"
)

// Provider facilitates DNS record manipulation with Huawei Cloud
Expand Down
18 changes: 5 additions & 13 deletions sdk/core/auth/basic/basic_icredential.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ package basic

import (
"fmt"
"io/ioutil"
"os"
"strings"
"time"

"github.com/libdns/huaweicloud/sdk/core/auth"
"github.com/libdns/huaweicloud/sdk/core/auth/cache"
"github.com/libdns/huaweicloud/sdk/core/auth/internal"
"github.com/libdns/huaweicloud/sdk/core/auth/signer"
"github.com/libdns/huaweicloud/sdk/core/impl"
"github.com/libdns/huaweicloud/sdk/core/request"
"github.com/libdns/huaweicloud/sdk/core/sdkerr"
"io/ioutil"
"os"
"strings"
"time"
)

const (
Expand Down Expand Up @@ -320,15 +321,6 @@ func (builder *CredentialsBuilder) WithIdTokenFile(idTokenFile string) *Credenti
return builder
}

// Deprecated: This function may panic under certain circumstances. Use SafeBuild instead.
func (builder *CredentialsBuilder) Build() *Credentials {
credentials, err := builder.SafeBuild()
if err != nil {
panic(err)
}
return credentials
}

func (builder *CredentialsBuilder) SafeBuild() (*Credentials, error) {
if builder.errMap != nil && len(builder.errMap) != 0 {
errMsg := "build credentials failed: "
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/auth/derived_icredential.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
package auth

import (
"github.com/libdns/huaweicloud/sdk/core/request"
"regexp"
"strings"

"github.com/libdns/huaweicloud/sdk/core/request"
)

const DefaultEndpointReg = "^[a-z][a-z0-9-]+(\\.[a-z]{2,}-[a-z]+-\\d{1,2})?\\.(my)?(huaweicloud|myhwclouds).(com|cn)"
Expand Down
16 changes: 4 additions & 12 deletions sdk/core/auth/global/global_icredential.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ package global

import (
"fmt"
"io/ioutil"
"os"
"time"

"github.com/libdns/huaweicloud/sdk/core/auth"
"github.com/libdns/huaweicloud/sdk/core/auth/cache"
"github.com/libdns/huaweicloud/sdk/core/auth/internal"
"github.com/libdns/huaweicloud/sdk/core/auth/signer"
"github.com/libdns/huaweicloud/sdk/core/impl"
"github.com/libdns/huaweicloud/sdk/core/request"
"github.com/libdns/huaweicloud/sdk/core/sdkerr"
"io/ioutil"
"os"
"time"
)

const (
Expand Down Expand Up @@ -311,15 +312,6 @@ func (builder *CredentialsBuilder) WithIdTokenFile(idTokenFile string) *Credenti
return builder
}

// Deprecated: This function may panic under certain circumstances. Use SafeBuild instead.
func (builder *CredentialsBuilder) Build() *Credentials {
credentials, err := builder.SafeBuild()
if err != nil {
panic(err)
}
return credentials
}

func (builder *CredentialsBuilder) SafeBuild() (*Credentials, error) {
if builder.errMap != nil && len(builder.errMap) != 0 {
errMsg := "build credentials failed: "
Expand Down
7 changes: 4 additions & 3 deletions sdk/core/auth/internal/iam_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,16 @@ package internal

import (
"errors"
"os"
"reflect"
"strings"

"github.com/libdns/huaweicloud/sdk/core/config"
"github.com/libdns/huaweicloud/sdk/core/impl"
"github.com/libdns/huaweicloud/sdk/core/request"
"github.com/libdns/huaweicloud/sdk/core/response"
"github.com/libdns/huaweicloud/sdk/core/sdkerr"
"github.com/libdns/huaweicloud/sdk/core/utils"
"os"
"reflect"
"strings"
)

const (
Expand Down
5 changes: 3 additions & 2 deletions sdk/core/auth/internal/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
package internal

import (
"github.com/libdns/huaweicloud/sdk/core/sdkerr"
"github.com/libdns/huaweicloud/sdk/core/utils"
"io/ioutil"
"net/http"
"time"

"github.com/libdns/huaweicloud/sdk/core/sdkerr"
"github.com/libdns/huaweicloud/sdk/core/utils"
)

type GetTemporaryCredentialFromMetadataResponse struct {
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/auth/provider/credential_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ package provider

import (
"fmt"
"reflect"

"github.com/libdns/huaweicloud/sdk/core/auth"
"github.com/libdns/huaweicloud/sdk/core/sdkerr"
"reflect"
)

const (
Expand Down
9 changes: 5 additions & 4 deletions sdk/core/auth/provider/env_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
package provider

import (
"os"
"strings"

"github.com/libdns/huaweicloud/sdk/core/auth"
"github.com/libdns/huaweicloud/sdk/core/auth/basic"
"github.com/libdns/huaweicloud/sdk/core/auth/global"
"github.com/libdns/huaweicloud/sdk/core/sdkerr"
"os"
"strings"
)

const (
Expand Down Expand Up @@ -70,14 +71,14 @@ func (p *EnvCredentialProvider) GetCredentials() (auth.ICredential, error) {
if err != nil {
return nil, err
}
return builder.Build(), nil
return builder.SafeBuild()
} else if strings.HasPrefix(p.credentialType, globalCredentialType) {
builder := global.NewCredentialsBuilder().WithDomainId(os.Getenv(domainIdEnvName))
err := fillCommonAttrs(builder, getCommonAttrsFromEnv())
if err != nil {
return nil, err
}
return builder.Build(), nil
return builder.SafeBuild()
}

return nil, sdkerr.NewCredentialsTypeError("unsupported credential type: " + p.credentialType)
Expand Down
15 changes: 10 additions & 5 deletions sdk/core/auth/provider/metadata_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
package provider

import (
"strings"

"github.com/libdns/huaweicloud/sdk/core/auth"
"github.com/libdns/huaweicloud/sdk/core/auth/basic"
"github.com/libdns/huaweicloud/sdk/core/auth/global"
"github.com/libdns/huaweicloud/sdk/core/sdkerr"
"strings"
)

type MetadataCredentialProvider struct {
Expand Down Expand Up @@ -54,18 +55,22 @@ func (p *MetadataCredentialProvider) GetCredentials() (auth.ICredential, error)
}

if strings.HasPrefix(p.credentialType, basicCredentialType) {
credentials := basic.NewCredentialsBuilder().Build()
err := credentials.UpdateSecurityTokenFromMetadata()
credentials, err := basic.NewCredentialsBuilder().SafeBuild()
if err != nil {
return nil, err
}
if err = credentials.UpdateSecurityTokenFromMetadata(); err != nil {
return nil, err
}
return credentials, nil
} else if strings.HasPrefix(p.credentialType, globalCredentialType) {
credentials := global.NewCredentialsBuilder().Build()
err := credentials.UpdateSecurityTokenFromMetadata()
credentials, err := global.NewCredentialsBuilder().SafeBuild()
if err != nil {
return nil, err
}
if err = credentials.UpdateSecurityTokenFromMetadata(); err != nil {
return nil, err
}
return credentials, nil
}

Expand Down
3 changes: 2 additions & 1 deletion sdk/core/auth/provider/profile_provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ package provider

import (
"errors"
"github.com/libdns/huaweicloud/sdk/core/auth"
"strings"

"github.com/libdns/huaweicloud/sdk/core/auth"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/auth/provider/provider_chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
package provider

import (
"strings"

"github.com/libdns/huaweicloud/sdk/core/auth"
"github.com/libdns/huaweicloud/sdk/core/sdkerr"
"strings"
)

type CredentialProviderChain struct {
Expand Down
6 changes: 4 additions & 2 deletions sdk/core/auth/signer/derived_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,13 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/libdns/huaweicloud/sdk/core/request"
"golang.org/x/crypto/hkdf"
"io"
"strings"
"time"

"golang.org/x/crypto/hkdf"

"github.com/libdns/huaweicloud/sdk/core/request"
)

const (
Expand Down
1 change: 1 addition & 0 deletions sdk/core/auth/signer/iaksksigner.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ package signer

import (
"errors"

"github.com/libdns/huaweicloud/sdk/core/auth/signer/algorithm"
"github.com/libdns/huaweicloud/sdk/core/request"
)
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/auth/signer/p256_sha256_signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ import (
"crypto/elliptic"
"encoding/hex"
"errors"
"github.com/libdns/huaweicloud/sdk/core/request"
"math/big"

"github.com/libdns/huaweicloud/sdk/core/request"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/auth/signer/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@ import (
"encoding/hex"
"errors"
"fmt"
"github.com/libdns/huaweicloud/sdk/core/request"
"net/url"
"reflect"
"sort"
"strings"
"time"

"github.com/libdns/huaweicloud/sdk/core/request"
)

const (
Expand Down
5 changes: 3 additions & 2 deletions sdk/core/config/http_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,12 @@ package config
import (
"context"
"fmt"
"github.com/libdns/huaweicloud/sdk/core/auth/signer/algorithm"
"github.com/libdns/huaweicloud/sdk/core/httphandler"
"net"
"net/http"
"time"

"github.com/libdns/huaweicloud/sdk/core/auth/signer/algorithm"
"github.com/libdns/huaweicloud/sdk/core/httphandler"
)

const (
Expand Down
3 changes: 2 additions & 1 deletion sdk/core/def/multipart.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,14 @@ package def
import (
"errors"
"fmt"
"github.com/libdns/huaweicloud/sdk/core/converter"
"io"
"mime/multipart"
"net/textproto"
"os"
"path/filepath"
"strings"

"github.com/libdns/huaweicloud/sdk/core/converter"
)

var quoteEscape = strings.NewReplacer("\\", "\\\\", `"`, "\\\"")
Expand Down
16 changes: 6 additions & 10 deletions sdk/core/hc_http_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ import (
"encoding/xml"
"errors"
"fmt"
"net"
"net/url"
"reflect"
"strings"
"sync/atomic"

"github.com/libdns/huaweicloud/sdk/core/auth"
"github.com/libdns/huaweicloud/sdk/core/converter"
"github.com/libdns/huaweicloud/sdk/core/def"
Expand All @@ -34,11 +40,6 @@ import (
"github.com/libdns/huaweicloud/sdk/core/response"
"github.com/libdns/huaweicloud/sdk/core/sdkerr"
"github.com/libdns/huaweicloud/sdk/core/utils"
"net"
"net/url"
"reflect"
"strings"
"sync/atomic"
)

const (
Expand Down Expand Up @@ -85,11 +86,6 @@ func (hc *HcHttpClient) GetCredential() auth.ICredential {
return hc.credential
}

// Deprecated: This function will be removed in the future version. Use WithExtraHeaders instead.
func (hc *HcHttpClient) PreInvoke(headers map[string]string) *HcHttpClient {
return hc.WithExtraHeaders(headers)
}

func (hc *HcHttpClient) Sync(req interface{}, reqDef *def.HttpRequestDef) (interface{}, error) {
exg := &exchange.SdkExchange{
ApiReference: &exchange.ApiReference{},
Expand Down
Loading

0 comments on commit 404dd32

Please sign in to comment.