Skip to content

Commit

Permalink
Merge pull request #27 from Archirk/master
Browse files Browse the repository at this point in the history
Add method to utilize token as openstack token
  • Loading branch information
Archirk authored Sep 14, 2023
2 parents 641cfc2 + 55c3d7c commit 5c46772
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
20 changes: 18 additions & 2 deletions pkg/v1/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const (
appName = "domains-go"

// appVersion is a version of the application.
appVersion = "0.4.0"
appVersion = "0.5.0"

// userAgent contains a basic user agent that will be used in queries.
userAgent = appName + "/" + appVersion
Expand Down Expand Up @@ -63,6 +63,9 @@ type ServiceClient struct {

// UserAgent contains user agent that will be used in all requests.
UserAgent string

// isOpenstackToken defines if passed token should be treated as OpenStack token.
isOpenstackToken bool
}

// NewDomainsClientV1 initializes a new client for the Domains API V1.
Expand Down Expand Up @@ -134,7 +137,12 @@ func (client *ServiceClient) DoRequest(ctx context.Context, method, path string,
}

request.Header.Set("User-Agent", client.UserAgent)
request.Header.Set("X-Token", client.Token)
if !client.isOpenstackToken {
request.Header.Set("X-Token", client.Token)
} else {
request.Header.Set("X-Auth-Token", client.Token)
}

if body != nil {
request.Header.Set("Content-Type", "application/json")
}
Expand All @@ -161,6 +169,14 @@ func (client *ServiceClient) DoRequest(ctx context.Context, method, path string,
return responseResult, nil
}

// WithOSToken return copy of original client where .Token is written to .OpenstackToken and
// .Token set to empty string.
func (client *ServiceClient) WithOSToken() *ServiceClient {
clientCopy := *client
clientCopy.isOpenstackToken = true
return &clientCopy
}

// ResponseResult represents a result of an HTTP request.
// It embeds standard http.Response and adds custom API error representations.
type ResponseResult struct {
Expand Down
16 changes: 16 additions & 0 deletions pkg/v1/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -334,3 +334,19 @@ func TestDoRequestInvalidResponseFromServer(t *testing.T) {
response.Err.Error())
}
}

func TestClientWithOSToken(t *testing.T) {
token := testutils.Token
client := NewDomainsClientV1(token, "http://example.org")
OSClient := client.WithOSToken()

if client == OSClient {
t.Fatal(".WithOSToken() should create copy and point to different instance")
}
if client.isOpenstackToken != false {
t.Fatal("initial client should have value of .isOpenstackToken = false")
}
if OSClient.isOpenstackToken != true {
t.Fatal("OSClient should have value of .isOpenstackToken = true")
}
}

0 comments on commit 5c46772

Please sign in to comment.