Changeset a23f1a72c96e…
Parent 6374fc553797…
by Benjamin Pollack <benjamin@fogcreek.com>
Changes to one file · Browse files at a23f1a72c96e Showing diff from parent 6374fc553797 Diff from another changeset...
@@ -32,6 +32,8 @@ Token string `json:"token"`
}
+type apiParams map[string]string
+
type KilnCredentials map[string]map[string]string
func NewKilnClient(kilnUrl *url.URL) *KilnClient {
@@ -78,24 +80,18 @@// KilnClient if successful, and returning an error otherwise
func (k *KilnClient) Logon() error {
login, password := requestUserCredentials()
- values := url.Values{"sUser": {login}, "sPassword": {password}}
- resp, err := http.Get(k.kilnRoute("Api/1.0/Auth/Login") + "?" + values.Encode())
+ resp, err := k.apiGet("Auth/Login", apiParams{"sUser": login, "sPassword": password})
if err != nil {
return fmt.Errorf("unable to contact Kiln: %v\n", err)
}
- defer resp.Body.Close()
- raw, err := ioutil.ReadAll(resp.Body)
- if err != nil {
- return fmt.Errorf("unable to contact Kiln: %v\n", err)
- }
var errors map[string][]KilnError
- err = json.Unmarshal(raw, &errors)
+ err = json.Unmarshal(resp, &errors)
if err == nil {
if err, _ := errors["errors"]; len(err) > 0 {
return fmt.Errorf("failed: %v\n", err[0].Description)
}
}
- k.credentials.Token = string(raw)
+ k.credentials.Token = string(resp)
return nil
}
@@ -195,14 +191,33 @@ return strings.TrimPrefix(absPath, root), nil
}
-// Returns the full URL for a given API call or Kiln route
+// Returns the full URL for relative Kiln URL
func (k *KilnClient) kilnRoute(route string) string {
return strings.TrimRight(k.credentials.KilnUrl, "/") + "/" + strings.TrimLeft(route, "/")
}
+// Returns the full URL for an API call in Kiln
+func (k *KilnClient) apiRoute(route string) string {
+ return k.kilnRoute("Api/1.0/"+strings.TrimLeft(route, "/"))
+}
+
// Returns the full URL for a given API call or Kiln route
func (k *KilnClient) repoRoute(repo string, action string) string {
- return k.kilnRoute(fmt.Sprintf("/Code/%v/%v", repo, action))
+ return k.kilnRoute(fmt.Sprintf("Code/%v/%v", repo, action))
+}
+
+// Returns the body from a Get API call
+func (k *KilnClient) apiGet(route string, params apiParams) ([]byte, error) {
+ v := url.Values{}
+ for key, value := range params {
+ v.Set(key, value)
+ }
+ resp, err := http.Get(k.apiRoute(route) + "?" + v.Encode())
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+ return ioutil.ReadAll(resp.Body)
}
// Encode a path by Kiln's hex encoding
|
Loading...