MCPcopy Create free account
hub / github.com/remotemobprogramming/mob / SendRequest

Method SendRequest

httpclient/httpclient.go:39–81  ·  view source on GitHub ↗
(requestBody []byte, requestMethod string, requestUrl string)

Source from the content-addressed store, hash-verified

37}
38
39func (c HttpClient) SendRequest(requestBody []byte, requestMethod string, requestUrl string) (string, error) {
40 say.Info(requestMethod + " " + requestUrl + " " + string(requestBody))
41
42 responseBody := bytes.NewBuffer(requestBody)
43 request, requestCreationError := http.NewRequest(requestMethod, requestUrl, responseBody)
44
45 if requestCreationError != nil {
46 return "", fmt.Errorf("failed to create the http request object: %w", requestCreationError)
47 }
48
49 request.Header.Set("Content-Type", "application/json")
50 response, responseErr := c.netHttpClient.Do(request)
51 if e, ok := responseErr.(*url.Error); ok {
52 switch e.Err.(type) {
53 case x509.UnknownAuthorityError:
54 say.Error("The timer.mob.sh SSL certificate is signed by an unknown authority!")
55 say.Fix("HINT: You can ignore that by adding MOB_TIMER_INSECURE=true to your configuration or environment.",
56 "echo MOB_TIMER_INSECURE=true >> ~/.mob")
57 return "", fmt.Errorf("failed, to make the http request: %w", responseErr)
58
59 default:
60 return "", fmt.Errorf("failed to make the http request: %w", responseErr)
61
62 }
63 }
64
65 if responseErr != nil {
66 return "", fmt.Errorf("failed to make the http request: %w", responseErr)
67 }
68 if response.StatusCode >= 300 {
69 return "", errors.New("got an error from the server: " + requestUrl + " " + response.Status)
70 }
71 defer response.Body.Close()
72 bodyBytes, responseReadingErr := ioutil.ReadAll(response.Body)
73 body := string(bodyBytes)
74 if responseReadingErr != nil {
75 return "", fmt.Errorf("failed to read the http response: %w", responseReadingErr)
76 }
77 if string(body) != "" {
78 say.Info(body)
79 }
80 return body, nil
81}

Callers

nothing calls this directly

Calls 3

InfoFunction · 0.92
ErrorFunction · 0.92
FixFunction · 0.92

Tested by

no test coverage detected