Jon Perritt | b6b1d02 | 2014-04-14 21:50:45 -0500 | [diff] [blame] | 1 | package v1 |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
| 5 | "github.com/rackspace/gophercloud/openstack/identity" |
| 6 | ) |
| 7 | |
| 8 | // Client is a structure that contains information for communicating with a provider. |
| 9 | type Client struct { |
| 10 | endpoint string |
| 11 | authority identity.AuthResults |
| 12 | options identity.AuthOptions |
| 13 | token *identity.Token |
| 14 | } |
| 15 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 16 | // NewClient creates and returns a *Client. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 17 | func NewClient(e string, a identity.AuthResults, o identity.AuthOptions) *Client { |
| 18 | return &Client{ |
| 19 | endpoint: e, |
| 20 | authority: a, |
| 21 | options: o, |
| 22 | } |
| 23 | } |
| 24 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 25 | // GetAccountURL returns the URI for making Account requests. This function is exported to allow |
| 26 | // the 'Accounts' subpackage to use it. It is not meant for public consumption. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 27 | func (c *Client) GetAccountURL() string { |
| 28 | return fmt.Sprintf("%s", c.endpoint) |
| 29 | } |
| 30 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 31 | // GetContainerURL returns the URI for making Container requests. This function is exported to allow |
| 32 | // the 'Containers' subpackage to use it. It is not meant for public consumption. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 33 | func (c *Client) GetContainerURL(container string) string { |
| 34 | return fmt.Sprintf("%s/%s", c.endpoint, container) |
| 35 | } |
| 36 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 37 | // GetObjectURL returns the URI for making Object requests. This function is exported to allow |
| 38 | // the 'Objects' subpackage to use it. It is not meant for public consumption. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 39 | func (c *Client) GetObjectURL(container, object string) string { |
| 40 | return fmt.Sprintf("%s/%s/%s", c.endpoint, container, object) |
| 41 | } |
| 42 | |
Jon Perritt | bef727e | 2014-05-12 22:41:55 -0500 | [diff] [blame] | 43 | // GetHeaders is a function that gets the header for token authentication against a client's endpoint. |
| 44 | // This function is exported to allow the subpackages to use it. It is not meant for public consumption. |
Jon Perritt | 816d2a0 | 2014-03-11 20:49:46 -0500 | [diff] [blame] | 45 | func (c *Client) GetHeaders() (map[string]string, error) { |
| 46 | t, err := c.getAuthToken() |
| 47 | if err != nil { |
| 48 | return map[string]string{}, err |
| 49 | } |
| 50 | |
| 51 | return map[string]string{ |
| 52 | "X-Auth-Token": t, |
| 53 | }, nil |
| 54 | } |
| 55 | |
| 56 | // getAuthToken is a function that tries to retrieve an authentication token from a client's endpoint. |
| 57 | func (c *Client) getAuthToken() (string, error) { |
| 58 | var err error |
| 59 | |
| 60 | if c.token == nil { |
| 61 | c.token, err = identity.GetToken(c.authority) |
| 62 | if err != nil { |
| 63 | return "", err |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | return c.token.Id, err |
| 68 | } |