MCPcopy Create free account
hub / github.com/bytebase/bytebase / getToken

Function getToken

backend/plugin/webhook/feishu/app.go:88–124  ·  view source on GitHub ↗
(ctx context.Context, c *http.Client, id, secret string)

Source from the content-addressed store, hash-verified

86}
87
88func getToken(ctx context.Context, c *http.Client, id, secret string) (*tokenValue, error) {
89 const getTenantAccessTokenReq = `{"app_id": "%s","app_secret": "%s"}`
90 const url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal"
91 body := strings.NewReader(fmt.Sprintf(getTenantAccessTokenReq, id, secret))
92 req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, body)
93 if err != nil {
94 return nil, errors.Wrapf(err, "construct POST %s", url)
95 }
96 req.Header.Set("Content-Type", "application/json")
97 resp, err := c.Do(req)
98 if err != nil {
99 return nil, errors.Wrapf(err, "POST %s", url)
100 }
101
102 b, err := io.ReadAll(resp.Body)
103 if err != nil {
104 return nil, errors.Wrapf(err, "read body of POST %s", url)
105 }
106 defer resp.Body.Close()
107
108 if resp.StatusCode != http.StatusOK {
109 return nil, errors.Errorf("non-200 POST status code %d with body %q", resp.StatusCode, b)
110 }
111
112 var response tenantAccessTokenResponse
113 if err := json.Unmarshal(b, &response); err != nil {
114 return nil, errors.Wrapf(err, "unmarshal body from POST %s", url)
115 }
116 if response.Code != 0 {
117 return nil, errors.Errorf("failed to get tenant access token, code %d, msg %s", response.Code, response.Msg)
118 }
119
120 return &tokenValue{
121 token: response.Token,
122 expireAt: time.Now().Add(time.Second * time.Duration(response.Expire)),
123 }, nil
124}
125
126var tokenCacheLock sync.Mutex
127

Callers 1

getTokenCachedFunction · 0.70

Calls 5

DoMethod · 0.80
ErrorfMethod · 0.80
UnmarshalMethod · 0.80
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected