Kiln » gitkiln Read More
Clone URL:  
Pushed to one repository · View In Graph Contained in master

clean up internal API to remove redundancy in coming API hooks

Changeset a23f1a72c96e

Parent 6374fc553797

by Profile picture of User 12Benjamin Pollack <benjamin@fogcreek.com>

Changes to one file · Browse files at a23f1a72c96e Showing diff from parent 6374fc553797 Diff from another changeset...

Change 1 of 3 Show Entire File kiln/​kiln.go Stacked
 
32
33
34
 
 
35
36
37
 
78
79
80
81
82
 
83
84
85
86
87
88
89
90
91
92
 
93
94
95
96
97
98
 
99
100
101
 
195
196
197
198
 
199
200
201
202
 
 
 
 
 
203
204
205
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
206
207
208
 
32
33
34
35
36
37
38
39
 
80
81
82
 
 
83
84
85
86
 
 
 
 
 
87
 
88
89
90
91
92
93
 
94
95
96
97
 
191
192
193
 
194
195
196
197
198
199
200
201
202
203
204
205
 
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
@@ -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