| 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 » _, err := lhttp.GetJSON(retry.Default, http.DefaultClient, s.host+resour
ce, v) |
| 30 » if status == http.StatusNotFound { | |
| 31 » » return errors.New("not found") | |
| 32 » } | |
| 33 return err | 30 return err |
| 34 } | 31 } |
| 35 | 32 |
| 36 // NewSwarming returns a new Swarming client. | 33 // NewSwarming returns a new Swarming client. |
| 37 func New(host string) (*Swarming, error) { | 34 func New(host string) (*Swarming, error) { |
| 38 host = strings.TrimRight(host, "/") | 35 host = strings.TrimRight(host, "/") |
| 39 » return &Swarming{host, http.DefaultClient}, nil | 36 » return &Swarming{host}, nil |
| 40 } | 37 } |
| 41 | 38 |
| 42 // FetchRequest returns the TaskRequest. | 39 // FetchRequest returns the TaskRequest. |
| 43 func (s *Swarming) FetchRequest(id TaskID) (*TaskRequest, error) { | 40 func (s *Swarming) FetchRequest(id TaskID) (*TaskRequest, error) { |
| 44 out := &TaskRequest{} | 41 out := &TaskRequest{} |
| 45 err := s.getJSON("/swarming/api/v1/client/task/"+string(id)+"/request",
out) | 42 err := s.getJSON("/swarming/api/v1/client/task/"+string(id)+"/request",
out) |
| 46 return out, err | 43 return out, err |
| 47 } | 44 } |
| 48 | 45 |
| 49 // TaskRequestProperties describes the idempotent properties of a task. | 46 // 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"` | 90 User string `json:"user"` |
| 94 } | 91 } |
| 95 | 92 |
| 96 // Duration returns the total duration of a task. | 93 // Duration returns the total duration of a task. |
| 97 func (s *TaskResult) Duration() (out time.Duration) { | 94 func (s *TaskResult) Duration() (out time.Duration) { |
| 98 for _, d := range s.Durations { | 95 for _, d := range s.Durations { |
| 99 out += time.Duration(d) * time.Second | 96 out += time.Duration(d) * time.Second |
| 100 } | 97 } |
| 101 return | 98 return |
| 102 } | 99 } |
| OLD | NEW |