MCPcopy
hub / github.com/cli/cli / httpRequest

Function httpRequest

pkg/cmd/api/http.go:16–82  ·  view source on GitHub ↗
(client *http.Client, hostname string, method string, p string, params interface{}, headers []string)

Source from the content-addressed store, hash-verified

14)
15
16func httpRequest(client *http.Client, hostname string, method string, p string, params interface{}, headers []string) (*http.Response, error) {
17 isGraphQL := p == "graphql"
18 var requestURL string
19 if strings.Contains(p, "://") {
20 requestURL = p
21 } else if isGraphQL {
22 requestURL = ghinstance.GraphQLEndpoint(hostname)
23 } else {
24 requestURL = ghinstance.RESTPrefix(hostname) + strings.TrimPrefix(p, "/")
25 }
26
27 var body io.Reader
28 var bodyIsJSON bool
29
30 switch pp := params.(type) {
31 case map[string]interface{}:
32 if strings.EqualFold(method, "GET") {
33 requestURL = addQuery(requestURL, pp)
34 } else {
35 if isGraphQL {
36 pp = groupGraphQLVariables(pp)
37 }
38 b, err := json.Marshal(pp)
39 if err != nil {
40 return nil, fmt.Errorf("error serializing parameters: %w", err)
41 }
42 body = bytes.NewBuffer(b)
43 bodyIsJSON = true
44 }
45 case io.Reader:
46 body = pp
47 case nil:
48 body = nil
49 default:
50 return nil, fmt.Errorf("unrecognized parameters type: %v", params)
51 }
52
53 req, err := http.NewRequest(strings.ToUpper(method), requestURL, body)
54 if err != nil {
55 return nil, err
56 }
57
58 for _, h := range headers {
59 idx := strings.IndexRune(h, ':')
60 if idx == -1 {
61 return nil, fmt.Errorf("header %q requires a value separated by ':'", h)
62 }
63 name, value := h[0:idx], strings.TrimSpace(h[idx+1:])
64 if strings.EqualFold(name, "Content-Length") {
65 length, err := strconv.ParseInt(value, 10, 0)
66 if err != nil {
67 return nil, err
68 }
69 req.ContentLength = length
70 } else {
71 req.Header.Add(name, value)
72 }
73 }

Callers 2

apiRunFunction · 0.85
Test_httpRequestFunction · 0.85

Calls 10

GraphQLEndpointFunction · 0.92
RESTPrefixFunction · 0.92
addQueryFunction · 0.85
groupGraphQLVariablesFunction · 0.85
ContainsMethod · 0.80
ErrorfMethod · 0.65
AddMethod · 0.65
GetMethod · 0.65
SetMethod · 0.65
DoMethod · 0.65

Tested by 1

Test_httpRequestFunction · 0.68