Sends HTTP request to Github API
(method, url, contentType string, reqBody io.Reader, bodySize int64)
| 248 | |
| 249 | // Sends HTTP request to Github API |
| 250 | func doRequest(method, url, contentType string, reqBody io.Reader, bodySize int64) ([]byte, error) { |
| 251 | req, err := http.NewRequest(method, url, reqBody) |
| 252 | if err != nil { |
| 253 | return nil, err |
| 254 | } |
| 255 | |
| 256 | req.Header.Set("Authorization", fmt.Sprintf("token %s", githubToken)) |
| 257 | req.Header.Set("Content-type", contentType) |
| 258 | req.Header.Set("Accept", "application/vnd.github.v3+json") |
| 259 | req.ContentLength = bodySize |
| 260 | |
| 261 | if debug { |
| 262 | log.Println("================ REQUEST DUMP ==================") |
| 263 | dump, err := httputil.DumpRequestOut(req, true) |
| 264 | if err != nil { |
| 265 | log.Println(err.Error()) |
| 266 | } |
| 267 | log.Println(string(dump[:])) |
| 268 | } |
| 269 | |
| 270 | resp, err := http.DefaultClient.Do(req) |
| 271 | |
| 272 | if debug { |
| 273 | log.Println("================ RESPONSE DUMP ==================") |
| 274 | dump, err := httputil.DumpResponse(resp, true) |
| 275 | if err != nil { |
| 276 | log.Println(err.Error()) |
| 277 | } |
| 278 | log.Println(string(dump[:])) |
| 279 | } |
| 280 | |
| 281 | if err != nil { |
| 282 | return nil, err |
| 283 | } |
| 284 | |
| 285 | defer resp.Body.Close() |
| 286 | |
| 287 | respBody, err := ioutil.ReadAll(resp.Body) |
| 288 | if err != nil { |
| 289 | return nil, err |
| 290 | } |
| 291 | |
| 292 | if resp.StatusCode != http.StatusOK && resp.StatusCode != http.StatusCreated { |
| 293 | return respBody, fmt.Errorf("Github returned an error:\n Code: %s. \n Body: %s", resp.Status, respBody) |
| 294 | } |
| 295 | |
| 296 | return respBody, nil |
| 297 | } |
no outgoing calls
no test coverage detected