Created
March 16, 2014 01:27
-
-
Save mattetti/9577075 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func APIToken(usr, password string) (string, error) { | |
req, err := http.NewRequest("GET", apiURL, nil) | |
if err != nil { | |
return "", fmt.Errorf("creating request") | |
} | |
req.SetBasicAuth(usr, password) | |
resp, err := client.Do(req) | |
if err != nil { | |
return "", err | |
} | |
body, err := ioutil.ReadAll(resp.Body) | |
resp.Body.Close() | |
if err != nil { | |
return "", err | |
} | |
var meResp struct { | |
APIToken string `json:"api_token"` | |
Error string `json:"error"` | |
} | |
err = json.Unmarshal(body, &meResp) | |
if err != nil { | |
return "", fmt.Errorf("unmarshal response: %v", err) | |
} | |
// if there's API called failed, return the message as an error. | |
if meResp.Error != "" { | |
return "", fmt.Errorf(meResp.Error) | |
} | |
return meResp.APIToken, nil | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment