From cad4a10319b98dd15c0a74d0fea13a2da4a0d3cc Mon Sep 17 00:00:00 2001 From: Stefan Majewsky Date: Mon, 29 Jan 2018 21:19:39 +0100 Subject: lay down the full Account API --- account.go | 69 +++++++++++++++++++++++--------------------------------------- 1 file changed, 26 insertions(+), 43 deletions(-) (limited to 'account.go') 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 } //////////////////////////////////////////////////////////////////////////////// -- cgit v1.2.3