(client requester, method string, pathStr string, query url.Values, body interface{}, checkServerErr bool)
| 30 | } |
| 31 | |
| 32 | func (ch *CredHub) request(client requester, method string, pathStr string, query url.Values, body interface{}, checkServerErr bool) (*http.Response, error) { |
| 33 | u := *ch.baseURL // clone |
| 34 | u.Path = pathStr |
| 35 | u.RawQuery = query.Encode() |
| 36 | |
| 37 | var req *http.Request |
| 38 | |
| 39 | jsonBody, err := json.Marshal(body) |
| 40 | if err != nil { |
| 41 | return nil, err |
| 42 | } |
| 43 | |
| 44 | if body != nil { |
| 45 | req, err = http.NewRequest(method, u.String(), bytes.NewReader(jsonBody)) |
| 46 | } else { |
| 47 | req, err = http.NewRequest(method, u.String(), nil) |
| 48 | } |
| 49 | if err != nil { |
| 50 | return nil, err |
| 51 | } |
| 52 | |
| 53 | req.Header.Set("Content-Type", "application/json") |
| 54 | |
| 55 | if os.Getenv("CREDHUB_DEBUG") == "true" { |
| 56 | dumpRequest(req) |
| 57 | } |
| 58 | |
| 59 | resp, err := client.Do(req) |
| 60 | if err != nil { |
| 61 | if os.Getenv("CREDHUB_DEBUG") == "true" { |
| 62 | fmt.Printf("[DEBUG] %s: %v", "An error occurred during the data request.\n", err) |
| 63 | } |
| 64 | return resp, err |
| 65 | } |
| 66 | |
| 67 | if os.Getenv("CREDHUB_DEBUG") == "true" { |
| 68 | dumpResponse(resp) |
| 69 | } |
| 70 | |
| 71 | if checkServerErr { |
| 72 | if err := ch.checkForServerError(resp); err != nil { |
| 73 | return nil, err |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | return resp, err |
| 78 | } |
| 79 | |
| 80 | func (ch *CredHub) checkForServerError(resp *http.Response) error { |
| 81 | if resp.StatusCode < 200 || resp.StatusCode >= 300 { |
no test coverage detected