diff options
| author | Stefan Majewsky <majewsky@gmx.net> | 2018-02-26 20:46:52 +0100 |
|---|---|---|
| committer | Stefan Majewsky <majewsky@gmx.net> | 2018-02-26 20:53:42 +0100 |
| commit | f5e33c280f225b04652b6898d586c5d9ec60052e (patch) | |
| tree | 82044c5cb78a38364de54dbf2030259be0853ca6 /account.go | |
| parent | a62b8abb2858437a803e74858157ff2672c8874a (diff) | |
| download | go-schwift-f5e33c280f225b04652b6898d586c5d9ec60052e.tar.gz | |
add struct Capabilities, Account.{,Raw}Capabilities
Diffstat (limited to 'account.go')
| -rw-r--r-- | account.go | 50 |
1 files changed, 50 insertions, 0 deletions
@@ -19,8 +19,11 @@ package schwift import ( + "encoding/json" "fmt" + "net/http" "regexp" + "strings" ) //Account represents a Swift account. @@ -31,6 +34,7 @@ type Account struct { name string //cache headers *AccountHeaders + caps *Capabilities } var endpointURLRegexp = regexp.MustCompile(`^(.*/)v1/(.*)/$`) @@ -169,3 +173,49 @@ func (a *Account) Create(headers AccountHeaders, opts *RequestOptions) error { func (a *Account) Containers() *ContainerIterator { return &ContainerIterator{Account: a} } + +//Capabilities queries the GET /info endpoint of the Swift server providing +//this account. Capabilities are cached, so the GET request will only be sent +//once during the first call to this method. +func (a *Account) Capabilities() (Capabilities, error) { + if a.caps != nil { + return *a.caps, nil + } + + buf, err := a.RawCapabilities() + if err != nil { + return Capabilities{}, err + } + + var caps Capabilities + err = json.Unmarshal(buf, &caps) + if err != nil { + return caps, err + } + + a.caps = &caps + return caps, nil +} + +//RawCapabilities queries the GET /info endpoint of the Swift server providing +//this account, and returns the response body. Unlike Account.Capabilities, +//this method does not employ any caching. +func (a *Account) RawCapabilities() ([]byte, error) { + //This method is the only one in Schwift that bypasses struct Request since + //the request URL is not below the endpoint URL. + + uri := a.backend.EndpointURL() + uri = strings.TrimPrefix(uri, "/") + uri = strings.TrimPrefix(uri, "/v1") + uri += "/info" + + req, err := http.NewRequest("GET", uri, nil) + if err != nil { + return nil, err + } + resp, err := a.backend.Do(req) + if err != nil { + return nil, err + } + return collectResponseBody(resp) +} |
