| 1 |
package api |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"encoding/json" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"io/ioutil" |
| 9 |
"log" |
| 10 |
"net/http" |
| 11 |
"net/http/httputil" |
| 12 |
"net/url" |
| 13 |
"os" |
| 14 |
) |
| 15 |
|
| 16 |
var debug = os.Getenv("DEBUG") != "" |
| 17 |
|
| 18 |
const baseUriDefault = "https://api.ctl.io/v2" |
| 19 |
const userAgentDefault = "CenturyLinkCloud/clc-sdk" |
| 20 |
|
| 21 |
func New(config Config) *Client { |
| 22 |
return &Client{ |
| 23 |
config: config, |
| 24 |
client: http.DefaultClient, |
| 25 |
} |
| 26 |
} |
| 27 |
|
| 28 |
type HTTP interface { |
| 29 |
Get(url string, resp interface{}) error |
| 30 |
Post(url string, body, resp interface{}) error |
| 31 |
Put(url string, body, resp interface{}) error |
| 32 |
Patch(url string, body, resp interface{}) error |
| 33 |
Delete(url string, resp interface{}) error |
| 34 |
Config() *Config |
| 35 |
} |
| 36 |
|
| 37 |
type Client struct { |
| 38 |
config Config |
| 39 |
Token Token |
| 40 |
|
| 41 |
client *http.Client |
| 42 |
} |
| 43 |
|
| 44 |
func (c *Client) Config() *Config { |
| 45 |
return &c.config |
| 46 |
} |
| 47 |
|
| 48 |
func (c *Client) Get(url string, resp interface{}) error { |
| 49 |
return c.DoWithAuth("GET", url, nil, resp) |
| 50 |
} |
| 51 |
|
| 52 |
func (c *Client) Post(url string, body, resp interface{}) error { |
| 53 |
return c.DoWithAuth("POST", url, body, resp) |
| 54 |
} |
| 55 |
|
| 56 |
func (c *Client) Put(url string, body, resp interface{}) error { |
| 57 |
return c.DoWithAuth("PUT", url, body, resp) |
| 58 |
} |
| 59 |
|
| 60 |
func (c *Client) Patch(url string, body, resp interface{}) error { |
| 61 |
return c.DoWithAuth("PATCH", url, body, resp) |
| 62 |
} |
| 63 |
|
| 64 |
func (c *Client) Delete(url string, resp interface{}) error { |
| 65 |
return c.DoWithAuth("DELETE", url, nil, resp) |
| 66 |
} |
| 67 |
|
| 68 |
func (c *Client) Auth() error { |
| 69 |
url := fmt.Sprintf("%s/authentication/login", c.config.BaseURL) |
| 70 |
body, err := c.serialize(c.config.User) |
| 71 |
if err != nil { |
| 72 |
return err |
| 73 |
} |
| 74 |
|
| 75 |
req, err := http.NewRequest("POST", url, body) |
| 76 |
if err != nil { |
| 77 |
return err |
| 78 |
} |
| 79 |
|
| 80 |
err = c.Do(req, &c.Token) |
| 81 |
if err == nil && c.config.Alias == "" { |
| 82 |
|
| 83 |
c.config.Alias = c.Token.Alias |
| 84 |
} |
| 85 |
return err |
| 86 |
} |
| 87 |
|
| 88 |
func (c *Client) Do(req *http.Request, ret interface{}) error { |
| 89 |
if debug { |
| 90 |
v, _ := httputil.DumpRequest(req, true) |
| 91 |
log.Println(string(v)) |
| 92 |
} |
| 93 |
|
| 94 |
req.Header.Add("User-Agent", c.config.UserAgent) |
| 95 |
req.Header.Add("Api-Client", c.config.UserAgent) |
| 96 |
req.Header.Add("Accept", "application/json") |
| 97 |
if req.Body != nil { |
| 98 |
req.Header.Add("Content-Type", "application/json") |
| 99 |
} |
| 100 |
|
| 101 |
resp, err := c.client.Do(req) |
| 102 |
if err != nil { |
| 103 |
log.Println(err) |
| 104 |
return err |
| 105 |
} |
| 106 |
if debug && resp != nil { |
| 107 |
v, _ := httputil.DumpResponse(resp, true) |
| 108 |
log.Println(string(v)) |
| 109 |
} |
| 110 |
if resp.StatusCode >= 400 { |
| 111 |
b, _ := ioutil.ReadAll(resp.Body) |
| 112 |
return fmt.Errorf("http err: [%s] - %s", resp.Status, b) |
| 113 |
} |
| 114 |
|
| 115 |
if ret == nil { |
| 116 |
return nil |
| 117 |
} |
| 118 |
|
| 119 |
|
| 120 |
return json.NewDecoder(resp.Body).Decode(ret) |
| 121 |
} |
| 122 |
|
| 123 |
func (c *Client) DoWithAuth(method, url string, body, ret interface{}) error { |
| 124 |
if !c.Token.Valid() { |
| 125 |
err := c.Auth() |
| 126 |
if err != nil { |
| 127 |
return err |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
b, err := c.serialize(body) |
| 132 |
if err != nil { |
| 133 |
return err |
| 134 |
} |
| 135 |
|
| 136 |
req, err := http.NewRequest(method, url, b) |
| 137 |
if err != nil { |
| 138 |
return err |
| 139 |
} |
| 140 |
req.Header.Add("Authorization", "Bearer "+c.Token.Token) |
| 141 |
|
| 142 |
return c.Do(req, ret) |
| 143 |
} |
| 144 |
|
| 145 |
func (c *Client) serialize(body interface{}) (io.Reader, error) { |
| 146 |
if body == nil { |
| 147 |
return nil, nil |
| 148 |
} |
| 149 |
|
| 150 |
b := new(bytes.Buffer) |
| 151 |
err := json.NewEncoder(b).Encode(body) |
| 152 |
return b, err |
| 153 |
} |
| 154 |
|
| 155 |
type Config struct { |
| 156 |
User User `json:"user"` |
| 157 |
Alias string `json:"alias"` |
| 158 |
BaseURL *url.URL `json:"-"` |
| 159 |
UserAgent string `json:"agent,omitempty"` |
| 160 |
} |
| 161 |
|
| 162 |
func (c Config) Valid() bool { |
| 163 |
return c.User.Username != "" && c.User.Password != "" && c.BaseURL != nil |
| 164 |
} |
| 165 |
|
| 166 |
func EnvConfig() (Config, error) { |
| 167 |
user := os.Getenv("CLC_USERNAME") |
| 168 |
pass := os.Getenv("CLC_PASSWORD") |
| 169 |
config, err := NewConfig(user, pass) |
| 170 |
if err != nil { |
| 171 |
return config, err |
| 172 |
} |
| 173 |
|
| 174 |
if !config.Valid() { |
| 175 |
return config, fmt.Errorf("missing environment variables [%s]", config) |
| 176 |
} |
| 177 |
return config, nil |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
func NewConfig(username, password string) (Config, error) { |
| 183 |
alias := os.Getenv("CLC_ALIAS") |
| 184 |
agent := userAgentDefault |
| 185 |
if v := os.Getenv("CLC_USER_AGENT"); v != "" { |
| 186 |
agent = v |
| 187 |
} |
| 188 |
base := baseUriDefault |
| 189 |
if v := os.Getenv("CLC_BASE_URL"); v != "" { |
| 190 |
base = v |
| 191 |
} |
| 192 |
uri, err := url.Parse(base) |
| 193 |
return Config{ |
| 194 |
User: User{ |
| 195 |
Username: username, |
| 196 |
Password: password, |
| 197 |
}, |
| 198 |
Alias: alias, |
| 199 |
BaseURL: uri, |
| 200 |
UserAgent: agent, |
| 201 |
}, err |
| 202 |
} |
| 203 |
|
| 204 |
func FileConfig(file string) (Config, error) { |
| 205 |
config := Config{} |
| 206 |
b, err := ioutil.ReadFile(file) |
| 207 |
if err != nil { |
| 208 |
return config, err |
| 209 |
} |
| 210 |
|
| 211 |
err = json.Unmarshal(b, &config) |
| 212 |
|
| 213 |
u, err := url.Parse(baseUriDefault) |
| 214 |
config.BaseURL = u |
| 215 |
return config, err |
| 216 |
} |
| 217 |
|
| 218 |
type User struct { |
| 219 |
Username string `json:"username"` |
| 220 |
Password string `json:"password"` |
| 221 |
} |
| 222 |
|
| 223 |
type Token struct { |
| 224 |
Username string `json:"userName"` |
| 225 |
Alias string `json:"accountAlias"` |
| 226 |
Location string `json:"locationAlias"` |
| 227 |
Roles []string `json:"roles"` |
| 228 |
Token string `json:"bearerToken"` |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
func (t Token) Valid() bool { |
| 233 |
return t.Token != "" |
| 234 |
} |
| 235 |
|
| 236 |
type Update struct { |
| 237 |
Op string `json:"op"` |
| 238 |
Member string `json:"member"` |
| 239 |
Value interface{} `json:"value"` |
| 240 |
} |