Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 1 | package gophercloud |
| 2 | |
| 3 | import ( |
| 4 | "fmt" |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 5 | "strings" |
Jon Perritt | fa2c65e | 2014-10-02 20:32:43 -0500 | [diff] [blame] | 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // WaitFor polls a predicate function once per second up to secs times to wait for a certain state to arrive. |
| 10 | func WaitFor(secs int, predicate func() (bool, error)) error { |
| 11 | for i := 0; i < secs; i++ { |
| 12 | time.Sleep(1 * time.Second) |
| 13 | |
| 14 | satisfied, err := predicate() |
| 15 | if err != nil { |
| 16 | return err |
| 17 | } |
| 18 | if satisfied { |
| 19 | return nil |
| 20 | } |
| 21 | } |
| 22 | return fmt.Errorf("Time out in WaitFor.") |
| 23 | } |
Ash Wilson | a844064 | 2014-10-07 09:55:58 -0400 | [diff] [blame] | 24 | |
| 25 | // NormalizeURL ensures that each endpoint URL has a closing `/`, as expected by ServiceClient. |
| 26 | func NormalizeURL(url string) string { |
| 27 | if !strings.HasSuffix(url, "/") { |
| 28 | return url + "/" |
| 29 | } |
| 30 | return url |
| 31 | } |
Jamie Hannaford | ee049ec | 2014-10-22 17:02:55 +0200 | [diff] [blame] | 32 | |
| 33 | // BuildQuery constructs the query section of a URI from a map. |
| 34 | func BuildQuery(params map[string]string) string { |
| 35 | if len(params) == 0 { |
| 36 | return "" |
| 37 | } |
| 38 | |
| 39 | query := "?" |
| 40 | for k, v := range params { |
| 41 | query += k + "=" + v + "&" |
| 42 | } |
| 43 | query = query[:len(query)-1] |
| 44 | return query |
| 45 | } |