Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fill out functionality #19

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions account.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ package acd

import (
"net/http"
"net/url"
"time"
)

Expand All @@ -18,6 +19,45 @@ type AccountService struct {
client *Client
}

// AccountEndpoints represents information about the current customer's endpoints
type AccountEndpoints struct {
CustomerExists bool `json:"customerExists"`
ContentURL string `json:"contentUrl"`
MetadataURL string `json:"metadataUrl"`
}

// GetEndpoints retrives the current endpoints for this customer
//
// It also updates the endpoints in the client
func (s *AccountService) GetEndpoints() (*AccountEndpoints, *http.Response, error) {
req, err := s.client.NewMetadataRequest("GET", "account/endpoint", nil)
if err != nil {
return nil, nil, err
}

endpoints := &AccountEndpoints{}
resp, err := s.client.Do(req, endpoints)
if err != nil {
return nil, resp, err
}

// Update the client endpoints
if endpoints.MetadataURL != "" {
u, err := url.Parse(endpoints.MetadataURL)
if err == nil {
s.client.MetadataURL = u
}
}
if endpoints.ContentURL != "" {
u, err := url.Parse(endpoints.ContentURL)
if err == nil {
s.client.ContentURL = u
}
}

return endpoints, resp, err
}

// AccountInfo represents information about an Amazon Cloud Drive account.
type AccountInfo struct {
TermsOfUse *string `json:"termsOfUse"`
Expand Down
25 changes: 20 additions & 5 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,19 +125,34 @@ func (c *Client) newRequest(base *url.URL, method, urlStr string, body interface
// JSON decoded and stored in the value pointed to by v, or returned as an
// error if an API error has occurred. If v implements the io.Writer
// interface, the raw response body will be written to v, without attempting to
// first decode it.
// first decode it. If v is nil then the resp.Body won't be closed - this is
// your responsibility.
//
func (c *Client) Do(req *http.Request, v interface{}) (*http.Response, error) {
//buf, _ := httputil.DumpRequest(req, true)
//buf, _ := httputil.DumpRequest(req, false)
//log.Printf("req = %s", string(buf))

resp, err := c.httpClient.Do(req)
if err != nil {
return nil, err
}

defer resp.Body.Close()
if v != nil {
defer resp.Body.Close()
}
//buf, _ = httputil.DumpResponse(resp, true)
//buf, _ = httputil.DumpResponse(resp, false)
//log.Printf("resp = %s", string(buf))

err = CheckResponse(resp)
if err != nil {
// even though there was an error, we still return the response
// in case the caller wants to inspect it further
// in case the caller wants to inspect it further. We do close the
// Body though
if v == nil {
resp.Body.Close()
}
return resp, err
}

Expand All @@ -161,11 +176,11 @@ func CheckResponse(r *http.Response) error {
}

errBody := ""
if data, err := ioutil.ReadAll(r.Body); err != nil {
if data, err := ioutil.ReadAll(r.Body); err == nil {
errBody = string(data)
}

errMsg := fmt.Sprintf("HTTP code %v, ", c)
errMsg := fmt.Sprintf("HTTP code %v: %q, ", c, r.Status)
if errBody == "" {
errMsg += "no response body"
} else {
Expand Down
Loading