blob: 94307b075bb42ee358e47f795e02580ef470f7aa [file] [log] [blame]
Jon Perritt816d2a02014-03-11 20:49:46 -05001package storage
2
3import (
4 "fmt"
5 "github.com/rackspace/gophercloud/openstack/identity"
6)
7
8// Client is a structure that contains information for communicating with a provider.
9type Client struct {
10 endpoint string
11 authority identity.AuthResults
12 options identity.AuthOptions
13 token *identity.Token
14}
15
16func NewClient(e string, a identity.AuthResults, o identity.AuthOptions) *Client {
17 return &Client{
18 endpoint: e,
19 authority: a,
20 options: o,
21 }
22}
23
24func (c *Client) GetAccountURL() string {
25 return fmt.Sprintf("%s", c.endpoint)
26}
27
28func (c *Client) GetContainerURL(container string) string {
29 return fmt.Sprintf("%s/%s", c.endpoint, container)
30}
31
32func (c *Client) GetObjectURL(container, object string) string {
33 return fmt.Sprintf("%s/%s/%s", c.endpoint, container, object)
34}
35
36// GetHeaders is a function that sets the header for token authentication against a client's endpoint.
37func (c *Client) GetHeaders() (map[string]string, error) {
38 t, err := c.getAuthToken()
39 if err != nil {
40 return map[string]string{}, err
41 }
42
43 return map[string]string{
44 "X-Auth-Token": t,
45 }, nil
46}
47
48// getAuthToken is a function that tries to retrieve an authentication token from a client's endpoint.
49func (c *Client) getAuthToken() (string, error) {
50 var err error
51
52 if c.token == nil {
53 c.token, err = identity.GetToken(c.authority)
54 if err != nil {
55 return "", err
56 }
57 }
58
59 return c.token.Id, err
60}