| 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 common | 5 package common |
| 6 | 6 |
| 7 import ( | 7 import ( |
| 8 "bytes" | |
| 9 "encoding/json" | 8 "encoding/json" |
| 10 "fmt" | 9 "fmt" |
| 11 "net/http" | |
| 12 "os" | 10 "os" |
| 13 "strings" | |
| 14 ) | 11 ) |
| 15 | 12 |
| 16 const jsonContentType = "application/json; charset=utf-8" | |
| 17 | |
| 18 // GetJSON does a simple HTTP GET on a JSON endpoint. | |
| 19 // | |
| 20 // Returns the status code and the error, if any. | |
| 21 func GetJSON(c *http.Client, url string, out interface{}) (int, error) { | |
| 22 if c == nil { | |
| 23 c = http.DefaultClient | |
| 24 } | |
| 25 resp, err := c.Get(url) | |
| 26 if err != nil { | |
| 27 return 0, fmt.Errorf("couldn't resolve %s: %s", url, err) | |
| 28 } | |
| 29 return decodeJSONResponse(resp, url, out) | |
| 30 } | |
| 31 | |
| 32 // PostJSON does a HTTP POST on a JSON endpoint. | |
| 33 // | |
| 34 // Returns the status code and the error, if any. | |
| 35 func PostJSON(c *http.Client, url string, in, out interface{}) (int, error) { | |
| 36 if c == nil { | |
| 37 c = http.DefaultClient | |
| 38 } | |
| 39 if in == nil { | |
| 40 in = map[string]string{} | |
| 41 } | |
| 42 encoded, err := json.Marshal(in) | |
| 43 if err != nil { | |
| 44 return 0, nil | |
| 45 } | |
| 46 resp, err := c.Post(url, jsonContentType, bytes.NewBuffer(encoded)) | |
| 47 if err != nil { | |
| 48 return 0, fmt.Errorf("couldn't resolve %s: %s", url, err) | |
| 49 } | |
| 50 return decodeJSONResponse(resp, url, out) | |
| 51 } | |
| 52 | |
| 53 func decodeJSONResponse(resp *http.Response, url string, out interface{}) (int,
error) { | |
| 54 defer resp.Body.Close() | |
| 55 if out == nil { | |
| 56 // The client doesn't care about the response. Still ensure the
response is | |
| 57 // valid json. | |
| 58 out = &map[string]interface{}{} | |
| 59 } | |
| 60 if err := json.NewDecoder(resp.Body).Decode(out); err != nil { | |
| 61 return resp.StatusCode, fmt.Errorf("bad response %s: %s", url, e
rr) | |
| 62 } | |
| 63 ct := strings.ToLower(resp.Header.Get("Content-Type")) | |
| 64 if ct != jsonContentType { | |
| 65 return resp.StatusCode, fmt.Errorf("unexpected Content-Type, exp
ected \"%s\", got \"%s\"", jsonContentType, ct) | |
| 66 } | |
| 67 if resp.StatusCode >= 400 { | |
| 68 return resp.StatusCode, fmt.Errorf("http status %d", resp.Status
Code) | |
| 69 } | |
| 70 return resp.StatusCode, nil | |
| 71 } | |
| 72 | |
| 73 // ReadJSONFile reads a file and decode it as JSON. | 13 // ReadJSONFile reads a file and decode it as JSON. |
| 74 func ReadJSONFile(filePath string, object interface{}) error { | 14 func ReadJSONFile(filePath string, object interface{}) error { |
| 75 f, err := os.Open(filePath) | 15 f, err := os.Open(filePath) |
| 76 if err != nil { | 16 if err != nil { |
| 77 return fmt.Errorf("failed to open %s: %s", filePath, err) | 17 return fmt.Errorf("failed to open %s: %s", filePath, err) |
| 78 } | 18 } |
| 79 defer f.Close() | 19 defer f.Close() |
| 80 if err = json.NewDecoder(f).Decode(object); err != nil { | 20 if err = json.NewDecoder(f).Decode(object); err != nil { |
| 81 return fmt.Errorf("failed to decode %s: %s", filePath, err) | 21 return fmt.Errorf("failed to decode %s: %s", filePath, err) |
| 82 } | 22 } |
| (...skipping 11 matching lines...) Expand all Loading... |
| 94 f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) | 34 f, err := os.OpenFile(filePath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0600) |
| 95 if err != nil { | 35 if err != nil { |
| 96 return fmt.Errorf("failed to open %s: %s", filePath, err) | 36 return fmt.Errorf("failed to open %s: %s", filePath, err) |
| 97 } | 37 } |
| 98 defer f.Close() | 38 defer f.Close() |
| 99 if _, err := f.Write(d); err != nil { | 39 if _, err := f.Write(d); err != nil { |
| 100 return fmt.Errorf("failed to write %s: %s", filePath, err) | 40 return fmt.Errorf("failed to write %s: %s", filePath, err) |
| 101 } | 41 } |
| 102 return nil | 42 return nil |
| 103 } | 43 } |
| OLD | NEW |