MCPcopy Create free account
hub / github.com/daodst/chat / PostJSONNoSpan

Function PostJSONNoSpan

new_feature/notice_func.go:25–66  ·  view source on GitHub ↗

PostJSONNoSpan performs a POST request with JSON on an internal HTTP API

(
	ctx context.Context, httpClient *http.Client,
	apiURL string, request, response interface{},
)

Source from the content-addressed store, hash-verified

23
24// PostJSONNoSpan performs a POST request with JSON on an internal HTTP API
25func 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
68type sendNoticeRequest struct {
69 UserID string `json:"user_id,omitempty"`

Callers 2

SendServerNoticeFunction · 0.85
SendRoomNoticeFunction · 0.85

Calls 4

SetMethod · 0.65
DoMethod · 0.65
CloseMethod · 0.65
StringMethod · 0.45

Tested by

no test coverage detected