/
common.go
49 lines (46 loc) · 1.43 KB
/
common.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package credentialhelper
import (
"bytes"
"context"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"github.com/hashicorp/go-cleanhttp"
)
func doRequest(ctx context.Context, port int, reqBody, respBody interface{}) error {
transport := cleanhttp.DefaultTransport()
transport.Proxy = nil
httpClient := &http.Client{
Transport: transport,
}
method := http.MethodPost
url := fmt.Sprintf("http://127.0.0.1:%d/git", port)
reqBodyBytes, err := json.Marshal(reqBody)
if err != nil {
return fmt.Errorf("error marshalling body of request %s %s: %w", method, url, err)
}
reqBodyReader := bytes.NewReader(reqBodyBytes)
req, err := http.NewRequestWithContext(ctx, method, url, reqBodyReader)
if err != nil {
return fmt.Errorf("error creating request %s %s: %w", method, url, err)
}
req.Header.Set("Content-Type", "application/json")
resp, err := httpClient.Do(req)
if err != nil {
return err
}
defer resp.Body.Close()
respBodyBytes, err2 := ioutil.ReadAll(resp.Body)
if resp.StatusCode != 200 {
return fmt.Errorf("response of request %s %s has unexpected status %d, body: %s", method, url, resp.StatusCode, string(respBodyBytes))
}
if err2 != nil {
return fmt.Errorf("error reading body of success response of request %s %s: %w", method, url, err)
}
err = json.Unmarshal(respBodyBytes, respBody)
if err != nil {
return fmt.Errorf("error unmarshalling body of success response of request %s %s: %w", method, url, err)
}
return nil
}