| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package swarming | 5 package swarming |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "errors" | 8 "errors" |
| 9 "net/http" | 9 "net/http" |
| 10 "strings" | 10 "strings" |
| 11 "time" | 11 "time" |
| 12 | 12 |
| 13 » "github.com/luci/luci-go/client/internal/common" | 13 » "github.com/luci/luci-go/client/internal/lhttp" |
| 14 » "github.com/luci/luci-go/client/internal/retry" |
| 14 ) | 15 ) |
| 15 | 16 |
| 16 // TaskID is a unique reference to a Swarming task. | 17 // TaskID is a unique reference to a Swarming task. |
| 17 type TaskID string | 18 type TaskID string |
| 18 | 19 |
| 19 // Swarming defines a Swarming client. | 20 // Swarming defines a Swarming client. |
| 20 type Swarming struct { | 21 type Swarming struct { |
| 21 » host string | 22 » host string |
| 22 » client *http.Client | |
| 23 } | 23 } |
| 24 | 24 |
| 25 func (s *Swarming) getJSON(resource string, v interface{}) error { | 25 func (s *Swarming) getJSON(resource string, v interface{}) error { |
| 26 if len(resource) == 0 || resource[0] != '/' { | 26 if len(resource) == 0 || resource[0] != '/' { |
| 27 return errors.New("resource must start with '/'") | 27 return errors.New("resource must start with '/'") |
| 28 } | 28 } |
| 29 » status, err := common.GetJSON(s.client, s.host+resource, v) | 29 » return lhttp.GetJSON(retry.Default, http.DefaultClient, s.host+resource,
v) |
| 30 » if status == http.StatusNotFound { | |
| 31 » » return errors.New("not found") | |
| 32 » } | |
| 33 » return err | |
| 34 } | 30 } |
| 35 | 31 |
| 36 // NewSwarming returns a new Swarming client. | 32 // NewSwarming returns a new Swarming client. |
| 37 func New(host string) (*Swarming, error) { | 33 func New(host string) (*Swarming, error) { |
| 38 host = strings.TrimRight(host, "/") | 34 host = strings.TrimRight(host, "/") |
| 39 » return &Swarming{host, http.DefaultClient}, nil | 35 » return &Swarming{host}, nil |
| 40 } | 36 } |
| 41 | 37 |
| 42 // FetchRequest returns the TaskRequest. | 38 // FetchRequest returns the TaskRequest. |
| 43 func (s *Swarming) FetchRequest(id TaskID) (*TaskRequest, error) { | 39 func (s *Swarming) FetchRequest(id TaskID) (*TaskRequest, error) { |
| 44 out := &TaskRequest{} | 40 out := &TaskRequest{} |
| 45 err := s.getJSON("/swarming/api/v1/client/task/"+string(id)+"/request",
out) | 41 err := s.getJSON("/swarming/api/v1/client/task/"+string(id)+"/request",
out) |
| 46 return out, err | 42 return out, err |
| 47 } | 43 } |
| 48 | 44 |
| 49 // TaskRequestProperties describes the idempotent properties of a task. | 45 // TaskRequestProperties describes the idempotent properties of a task. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 93 User string `json:"user"` | 89 User string `json:"user"` |
| 94 } | 90 } |
| 95 | 91 |
| 96 // Duration returns the total duration of a task. | 92 // Duration returns the total duration of a task. |
| 97 func (s *TaskResult) Duration() (out time.Duration) { | 93 func (s *TaskResult) Duration() (out time.Duration) { |
| 98 for _, d := range s.Durations { | 94 for _, d := range s.Durations { |
| 99 out += time.Duration(d) * time.Second | 95 out += time.Duration(d) * time.Second |
| 100 } | 96 } |
| 101 return | 97 return |
| 102 } | 98 } |
| OLD | NEW |