blob: f616038ebded2639f155c497a319919a715475ba [file] [log] [blame]
Jon Perrittb6b1d022014-04-14 21:50:45 -05001package v1
Jon Perritt816d2a02014-03-11 20:49:46 -05002
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
Jon Perrittbef727e2014-05-12 22:41:55 -050016// NewClient creates and returns a *Client.
Jon Perritt816d2a02014-03-11 20:49:46 -050017func 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 Perrittbef727e2014-05-12 22:41:55 -050025// 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 Perritt816d2a02014-03-11 20:49:46 -050027func (c *Client) GetAccountURL() string {
28 return fmt.Sprintf("%s", c.endpoint)
29}
30
Jon Perrittbef727e2014-05-12 22:41:55 -050031// 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 Perritt816d2a02014-03-11 20:49:46 -050033func (c *Client) GetContainerURL(container string) string {
34 return fmt.Sprintf("%s/%s", c.endpoint, container)
35}
36
Jon Perrittbef727e2014-05-12 22:41:55 -050037// 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 Perritt816d2a02014-03-11 20:49:46 -050039func (c *Client) GetObjectURL(container, object string) string {
40 return fmt.Sprintf("%s/%s/%s", c.endpoint, container, object)
41}
42
Jon Perrittbef727e2014-05-12 22:41:55 -050043// 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 Perritt816d2a02014-03-11 20:49:46 -050045func (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.
57func (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}