Skip to content

Commit

Permalink
Merge pull request #73 from yunify/add-NewWithEndpoint
Browse files Browse the repository at this point in the history
Add new with endpoint
  • Loading branch information
runzexia authored Dec 12, 2017
2 parents c7fc8b6 + 6c67f3a commit 19b51a6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 2 deletions.
43 changes: 41 additions & 2 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,18 @@
package config

import (
"fmt"
"io/ioutil"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"

"github.com/yunify/qingcloud-sdk-go/logger"
"github.com/yunify/qingcloud-sdk-go/utils"
"net"
"time"
)

// A Config stores a configuration of this sdk.
Expand Down Expand Up @@ -62,6 +65,42 @@ func New(accessKeyID, secretAccessKey string) (*Config, error) {
return config, nil
}

// NewWithEndpoint create a Config with given AccessKeyID, SecretAccessKey and endpoint
func NewWithEndpoint(accessKeyID, secretAccessKey, endpoint string) (*Config, error) {
qcURL, err := url.Parse(endpoint)
if err != nil {
return nil, err
}

if !strings.Contains(qcURL.Host, ":") {
if qcURL.Scheme == "https" {
qcURL.Host += ":443"
} else if qcURL.Scheme == "http" {
qcURL.Host += ":80"
} else {
return nil, fmt.Errorf("can not find default port ")
}
}

config, err := NewDefault()
if err != nil {
return nil, err
}

_, port, err := net.SplitHostPort(qcURL.Host)
if err != nil {
return nil, err
}

config.Port, _ = strconv.Atoi(port)
config.AccessKeyID = accessKeyID
config.SecretAccessKey = secretAccessKey
config.Protocol = qcURL.Scheme
config.URI = qcURL.Path
config.Connection = &http.Client{}
return config, nil
}

// NewDefault create a Config with default configuration.
func NewDefault() (*Config, error) {
config := &Config{}
Expand Down
12 changes: 12 additions & 0 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,15 @@ func TestNew(t *testing.T) {
assert.Equal(t, "SecretAccessKey", config.SecretAccessKey)
assert.Equal(t, "https", config.Protocol)
}

func TestNewWithEndpoint(t *testing.T) {
config, err := NewWithEndpoint("AccessKeyID", "SecretAccessKey", "https://api.qingcloud.com/iaas")
assert.Nil(t, err)

assert.Equal(t, "AccessKeyID", config.AccessKeyID)
assert.Equal(t, "SecretAccessKey", config.SecretAccessKey)
assert.Equal(t, "https", config.Protocol)
assert.Equal(t, "api.qingcloud.com", config.Host)
assert.Equal(t, 443, config.Port)
assert.Equal(t, "/iaas", config.URI)
}
1 change: 1 addition & 0 deletions request/signer.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@ func (is *Signer) BuildStringToSign(request *http.Request) (string, error) {
}
return "", fmt.Errorf("Requset Type Not Support For Sign ")
}

// BuildStringToSignByValues build the string to sign.
func (is *Signer) BuildStringToSignByValues(requestDate string, requestMethod string, requestPath string, requestParams url.Values) (string, error) {
requestParams.Set("access_key_id", is.AccessKeyID)
Expand Down

0 comments on commit 19b51a6

Please sign in to comment.