/
openapi.go
74 lines (61 loc) · 2.13 KB
/
openapi.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package http
import (
"context"
"encoding/json"
"strconv"
"github.com/byted-apaas/server-common-go/constants"
cExceptions "github.com/byted-apaas/server-common-go/exceptions"
"github.com/byted-apaas/server-common-go/structs"
"github.com/byted-apaas/server-common-go/utils"
)
func GetAppTokenHttp(ctx context.Context, clientID, clientSecret string) (*structs.AppTokenResp, error) {
data := map[string]interface{}{
"clientId": clientID,
"clientSecret": clientSecret,
"withTenantInfo": true,
}
body, err := utils.ErrorWrapper(GetOpenapiClient().PostJson(ctx, OpenapiPathGetToken, nil, data))
if err != nil {
return nil, err
}
tokenResult := structs.AppTokenResp{}
if err = json.Unmarshal(body, &tokenResult); err != nil {
return nil, cExceptions.InternalError("[AppCredential] fetchToken Unmarshal TokenResult failed, err: %v", err)
}
return &tokenResult, nil
}
func GetFunctionMetaHttp(ctx context.Context, apiName string) (funcMeta *structs.FunctionMeta, err error) {
headers := map[string][]string{
constants.HttpHeaderKeyUser: {strconv.FormatInt(utils.GetUserIDFromCtx(ctx), 10)},
}
body, extra, err := GetOpenapiClient().PostJson(ctx, GetInnerAPIPathGetFunction(), headers, map[string]interface{}{"apiName": apiName}, AppTokenMiddleware, TenantAndUserMiddleware, ServiceIDMiddleware)
if err != nil {
return nil, cExceptions.ErrWrap(err)
}
data, err := utils.ErrorWrapper(body, extra, err)
if err != nil {
return nil, err
}
var resp struct {
Detail *struct {
APIID string `json:"apiID"`
APIName string `json:"apiName"`
Input []*structs.IOParamItem `json:"input"`
Output []*structs.IOParamItem `json:"output"`
} `json:"detail"`
}
logid := utils.GetLogIDFromExtra(extra)
if err := utils.JsonUnmarshalBytes(data, &resp); err != nil {
return nil, cExceptions.InternalError("InvokeFunctionWithAuth failed, err: %v, logid: %v", err, logid)
}
if resp.Detail == nil {
return nil, nil
}
return &structs.FunctionMeta{
ApiName: resp.Detail.APIName,
IOParam: structs.FunctionIOParam{
Input: resp.Detail.Input,
Output: resp.Detail.Output,
},
}, nil
}