func (c *Client) do(method, url string, header map[string]string, body []byte) ([]byte, error) { r := bytes.NewReader(body) req, err := http.NewRequest(method, url, r) if err != nil { return nil, err } for k, v := range header { req.Header.Set(k, v) } req.Header.Set("X-Soracom-Token", c.token) req.Header.Set("X-Soracom-API-Key", c.apiKey) client := http.Client{} resp, err := client.Do(req) if err != nil { return nil, err } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } if resp.StatusCode == 200 || resp.StatusCode == 201 || resp.StatusCode == 204 { return data, nil } return nil, errors.NewError(data) }
func Authenticate(email, passwd string) (map[string]string, error) { auth := map[string]interface{}{ "email": email, "password": passwd, "tokenTimeoutSeconds": 0, } buf, _ := json.Marshal(auth) body := bytes.NewReader(buf) url := config.MakeUrl("/auth") resp, err := http.Post(url, "application/json", body) if err != nil { return nil, err } defer resp.Body.Close() data, err := ioutil.ReadAll(resp.Body) if err != nil { return nil, err } if resp.StatusCode != 200 { return nil, errors.NewError(data) } return parseAuthenticate(data) }