MCPcopy
hub / github.com/QuantumNous/new-api / fetchCodexChannelWhamData

Function fetchCodexChannelWhamData

controller/codex_usage.go:55–166  ·  view source on GitHub ↗
(
	c *gin.Context,
	fetch codexWhamFetchFunc,
	logPrefix string,
	userMessage string,
)

Source from the content-addressed store, hash-verified

53) (statusCode int, body []byte, err error)
54
55func fetchCodexChannelWhamData(
56 c *gin.Context,
57 fetch codexWhamFetchFunc,
58 logPrefix string,
59 userMessage string,
60) {
61 channelId, err := strconv.Atoi(c.Param("id"))
62 if err != nil {
63 common.ApiError(c, fmt.Errorf("invalid channel id: %w", err))
64 return
65 }
66
67 ch, err := model.GetChannelById(channelId, true)
68 if err != nil {
69 common.ApiError(c, err)
70 return
71 }
72 if ch == nil {
73 c.JSON(http.StatusOK, gin.H{"success": false, "message": "channel not found"})
74 return
75 }
76 if ch.Type != constant.ChannelTypeCodex {
77 c.JSON(http.StatusOK, gin.H{"success": false, "message": "channel type is not Codex"})
78 return
79 }
80 if ch.ChannelInfo.IsMultiKey {
81 c.JSON(http.StatusOK, gin.H{"success": false, "message": "multi-key channel is not supported"})
82 return
83 }
84
85 oauthKey, err := codex.ParseOAuthKey(strings.TrimSpace(ch.Key))
86 if err != nil {
87 common.SysError("failed to parse oauth key: " + err.Error())
88 c.JSON(http.StatusOK, gin.H{"success": false, "message": "解析凭证失败,请检查渠道配置"})
89 return
90 }
91 accessToken := strings.TrimSpace(oauthKey.AccessToken)
92 accountID := strings.TrimSpace(oauthKey.AccountID)
93 if accessToken == "" {
94 c.JSON(http.StatusOK, gin.H{"success": false, "message": "codex channel: access_token is required"})
95 return
96 }
97 if accountID == "" {
98 c.JSON(http.StatusOK, gin.H{"success": false, "message": "codex channel: account_id is required"})
99 return
100 }
101
102 client, err := service.NewProxyHttpClient(ch.GetSetting().Proxy)
103 if err != nil {
104 common.ApiError(c, err)
105 return
106 }
107
108 ctx, cancel := context.WithTimeout(c.Request.Context(), 15*time.Second)
109 defer cancel()
110
111 statusCode, body, err := fetch(ctx, client, ch.GetBaseURL(), accessToken, accountID)
112 if err != nil {

Callers 3

GetCodexChannelUsageFunction · 0.85
ResetCodexChannelUsageFunction · 0.85

Calls 14

ApiErrorFunction · 0.92
GetChannelByIdFunction · 0.92
ParseOAuthKeyFunction · 0.92
SysErrorFunction · 0.92
NewProxyHttpClientFunction · 0.92
MarshalFunction · 0.92
InitChannelCacheFunction · 0.92
ResetProxyClientCacheFunction · 0.92
UnmarshalFunction · 0.92
GetBaseURLMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected