MCPcopy
hub / github.com/larksuite/cli / CallAPI

Method CallAPI

cmd/event/runtime.go:21–62  ·  view source on GitHub ↗
(ctx context.Context, method, path string, body interface{})

Source from the content-addressed store, hash-verified

19}
20
21func (r *consumeRuntime) CallAPI(ctx context.Context, method, path string, body interface{}) (json.RawMessage, error) {
22 resp, err := r.client.DoAPI(ctx, client.RawApiRequest{
23 Method: method,
24 URL: path,
25 Data: body,
26 As: r.accessIdentity,
27 })
28 if err != nil {
29 if _, ok := errs.ProblemOf(err); ok {
30 return nil, err
31 }
32 return nil, errs.NewNetworkError(errs.SubtypeNetworkTransport,
33 "api %s %s: %s", method, path, err).WithCause(err)
34 }
35 // Non-JSON HTTP errors (gateway text/plain 404 etc.) skip OAPI envelope parsing.
36 ct := resp.Header.Get("Content-Type")
37 if resp.StatusCode >= 400 && !client.IsJSONContentType(ct) && ct != "" {
38 const maxBodyEcho = 256
39 body := string(resp.RawBody)
40 if len(body) > maxBodyEcho {
41 body = body[:maxBodyEcho] + "…(truncated)"
42 }
43 if resp.StatusCode >= 500 {
44 return nil, errs.NewNetworkError(errs.SubtypeNetworkServer,
45 "api %s %s returned %d: %s", method, path, resp.StatusCode, body).WithRetryable()
46 }
47 return nil, errs.NewInternalError(errs.SubtypeInvalidResponse,
48 "api %s %s returned %d: %s", method, path, resp.StatusCode, body)
49 }
50 result, err := client.ParseJSONResponse(resp)
51 if err != nil {
52 if _, ok := errs.ProblemOf(err); ok {
53 return nil, err
54 }
55 return nil, errs.NewInternalError(errs.SubtypeInvalidResponse,
56 "api %s %s: %s", method, path, err).WithCause(err)
57 }
58 if apiErr := r.client.CheckResponse(result, r.accessIdentity); apiErr != nil {
59 return json.RawMessage(resp.RawBody), apiErr
60 }
61 return json.RawMessage(resp.RawBody), nil
62}

Callers

nothing calls this directly

Calls 10

ProblemOfFunction · 0.92
NewNetworkErrorFunction · 0.92
IsJSONContentTypeFunction · 0.92
NewInternalErrorFunction · 0.92
ParseJSONResponseFunction · 0.92
CheckResponseMethod · 0.80
GetMethod · 0.65
DoAPIMethod · 0.45
WithCauseMethod · 0.45
WithRetryableMethod · 0.45

Tested by

no test coverage detected