PostJSONNoSpan performs a POST request with JSON on an internal HTTP API
(
ctx context.Context, httpClient *http.Client,
apiURL string, request, response interface{},
)
| 23 | |
| 24 | // PostJSONNoSpan performs a POST request with JSON on an internal HTTP API |
| 25 | func PostJSONNoSpan( |
| 26 | ctx context.Context, httpClient *http.Client, |
| 27 | apiURL string, request, response interface{}, |
| 28 | ) error { |
| 29 | jsonBytes, err := json.Marshal(request) |
| 30 | if err != nil { |
| 31 | return err |
| 32 | } |
| 33 | |
| 34 | parsedAPIURL, err := url.Parse(apiURL) |
| 35 | if err != nil { |
| 36 | return err |
| 37 | } |
| 38 | |
| 39 | parsedAPIURL.Path = strings.TrimLeft(parsedAPIURL.Path, "/") |
| 40 | apiURL = parsedAPIURL.String() |
| 41 | |
| 42 | req, err := http.NewRequest(http.MethodPost, apiURL, bytes.NewReader(jsonBytes)) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | |
| 47 | req.Header.Set("Content-Type", "application/json") |
| 48 | |
| 49 | res, err := httpClient.Do(req.WithContext(ctx)) |
| 50 | if res != nil { |
| 51 | defer (func() { err = res.Body.Close() })() |
| 52 | } |
| 53 | if err != nil { |
| 54 | return err |
| 55 | } |
| 56 | if res.StatusCode != http.StatusOK { |
| 57 | var errorBody struct { |
| 58 | Message string `json:"message"` |
| 59 | } |
| 60 | if msgerr := json.NewDecoder(res.Body).Decode(&errorBody); msgerr == nil { |
| 61 | return fmt.Errorf("internal API: %d from %s: %s", res.StatusCode, apiURL, errorBody.Message) |
| 62 | } |
| 63 | return fmt.Errorf("internal API: %d from %s", res.StatusCode, apiURL) |
| 64 | } |
| 65 | return json.NewDecoder(res.Body).Decode(response) |
| 66 | } |
| 67 | |
| 68 | type sendNoticeRequest struct { |
| 69 | UserID string `json:"user_id,omitempty"` |
no test coverage detected