Skip to content

Commit

Permalink
add port default value , go fmt project
Browse files Browse the repository at this point in the history
  • Loading branch information
runzexia committed Dec 12, 2017
1 parent 6bd4388 commit 6c67f3a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 14 deletions.
25 changes: 16 additions & 9 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,29 +65,36 @@ func New(accessKeyID, secretAccessKey string) (*Config, error) {
return config, nil
}

//NewWithEndpoint create a Config with given AccessKeyID , SecretAccessKey and endpoint
// 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, ":") {
return nil, fmt.Errorf("If you use endpoint , you must pass in the port number ")
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
}
config.AccessKeyID = accessKeyID
config.SecretAccessKey = secretAccessKey
// get host and port
hostPort := strings.Split(qcURL.Host, ":")
config.Host = hostPort[0]
port, err := strconv.Atoi(hostPort[1])

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

config.Port, _ = strconv.Atoi(port)
config.AccessKeyID = accessKeyID
config.SecretAccessKey = secretAccessKey
config.Protocol = qcURL.Scheme
config.URI = qcURL.Path
config.Connection = &http.Client{}
Expand Down
10 changes: 5 additions & 5 deletions config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ func TestNew(t *testing.T) {
}

func TestNewWithEndpoint(t *testing.T) {
config, err:= NewWithEndpoint("AccessKeyID","SecretAccessKey","https://api.qingcloud.com:443/iaas")
assert.Nil(t,err)
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)
assert.Equal(t, "api.qingcloud.com", config.Host)
assert.Equal(t, 443, config.Port)
assert.Equal(t, "/iaas", config.URI)
}

0 comments on commit 6c67f3a

Please sign in to comment.