aboutsummaryrefslogtreecommitdiff
path: root/account.go
diff options
context:
space:
mode:
authorStefan Majewsky <majewsky@gmx.net>2018-01-29 21:19:39 +0100
committerStefan Majewsky <majewsky@gmx.net>2018-01-29 21:43:45 +0100
commitcad4a10319b98dd15c0a74d0fea13a2da4a0d3cc (patch)
tree0703764e2d3a94fce3a553720f2a182e57612de2 /account.go
parent3834e49c90c39f4c95e3b9e7bb52b35204a75625 (diff)
downloadgo-schwift-cad4a10319b98dd15c0a74d0fea13a2da4a0d3cc.tar.gz
lay down the full Account API
Diffstat (limited to 'account.go')
-rw-r--r--account.go69
1 files changed, 26 insertions, 43 deletions
diff --git a/account.go b/account.go
index d89299f..a2bb6a7 100644
--- a/account.go
+++ b/account.go
@@ -20,7 +20,6 @@ package schwift
import (
"fmt"
- "net/http"
"regexp"
"github.com/gophercloud/gophercloud"
@@ -33,7 +32,7 @@ type Account struct {
baseURL string
name string
//cache
- metadata *AccountMetadata
+ headers *AccountHeaders
}
////////////////////////////////////////////////////////////////////////////////
@@ -86,24 +85,13 @@ func (a *Account) Client() *gophercloud.ServiceClient {
}
////////////////////////////////////////////////////////////////////////////////
-// account metadata
-
-//AccountMetadata contains the metadata for an account. The `Raw` attribute
-//contains the raw set of headers returned from a HEAD or GET request on the
-//account. The other attributes contain the parsed values of common headers.
-type AccountMetadata struct {
- Exists bool
- BytesUsed uint64 //from X-Account-Bytes-Used
- ContainerCount uint64 //from X-Account-Container-Count
- ObjectCount uint64 //from X-Account-Object-Count
- //TODO account quota
- Raw http.Header
-}
+// account headers
-//Metadata returns the metadata for this account. If the account does not exist,
-func (a *Account) Metadata() (AccountMetadata, error) {
- if a.metadata != nil {
- return *a.metadata, nil
+//Headers returns the AccountHeaders for this account. If the AccountHeaders
+//has not been cached yet, a HEAD request is issued on the account.
+func (a *Account) Headers() (AccountHeaders, error) {
+ if a.headers != nil {
+ return *a.headers, nil
}
resp, err := Request{
@@ -111,36 +99,31 @@ func (a *Account) Metadata() (AccountMetadata, error) {
ExpectStatusCodes: []int{200},
}.Do(a.client)
if err != nil {
- return AccountMetadata{}, err
+ return AccountHeaders{}, err
}
- a.metadata, err = parseAccountMetadata(resp)
+ var headers AccountHeaders
+ err = parseHeaders(resp.Header, &headers)
if err != nil {
- return AccountMetadata{}, err
+ return AccountHeaders{}, err
}
- return *a.metadata, nil
+ return *a.headers, nil
}
-func parseAccountMetadata(resp *http.Response) (*AccountMetadata, error) {
- bytesUsed, err := parseUnsignedIntHeader(resp, "X-Account-Bytes-Used")
- if err != nil {
- return nil, err
- }
- containerCount, err := parseUnsignedIntHeader(resp, "X-Account-Container-Count")
- if err != nil {
- return nil, err
- }
- objectCount, err := parseUnsignedIntHeader(resp, "X-Account-Object-Count")
- if err != nil {
- return nil, err
- }
- return &AccountMetadata{
- Exists: true,
- BytesUsed: bytesUsed,
- ContainerCount: containerCount,
- ObjectCount: objectCount,
- Raw: resp.Header,
- }, nil
+//Invalidate clears the internal cache of this Account instance. The next call
+//to Headers() on this instance will issue a HEAD request on the account.
+func (a *Account) Invalidate() {
+ a.headers = nil
+}
+
+//Post creates or updates the account using a POST request.
+func (a *Account) Post(headers AccountHeaders) error {
+ _, err := Request{
+ Method: "POST",
+ AdditionalHeaders: compileHeaders(headers),
+ ExpectStatusCodes: []int{204},
+ }.Do(a.client)
+ return err
}
////////////////////////////////////////////////////////////////////////////////