MCPcopy Create free account
hub / github.com/chainreactors/EvilProxy / fetchModels

Function fetchModels

cmd/fetch_codex_models/main.go:231–282  ·  view source on GitHub ↗
(ctx context.Context, auth *coreauth.Auth, accessToken, clientVersion string)

Source from the content-addressed store, hash-verified

229}
230
231func fetchModels(ctx context.Context, auth *coreauth.Auth, accessToken, clientVersion string) ([]byte, int, error) {
232 modelsURL, errURL := codexModelsURL(clientVersion)
233 if errURL != nil {
234 return nil, 0, errURL
235 }
236
237 httpReq, errReq := http.NewRequestWithContext(ctx, http.MethodGet, modelsURL, nil)
238 if errReq != nil {
239 return nil, 0, errReq
240 }
241 httpReq.Close = true
242 httpReq.Header.Set("Accept", "application/json")
243 httpReq.Header.Set("Authorization", "Bearer "+accessToken)
244 httpReq.Header.Set("Originator", defaultCodexOriginator)
245 httpReq.Header.Set("User-Agent", defaultCodexUserAgent)
246 if accountID := metaStringValue(auth.Metadata, "account_id"); accountID != "" {
247 httpReq.Header.Set("Chatgpt-Account-Id", accountID)
248 }
249 if auth != nil {
250 util.ApplyCustomHeadersFromAttrs(httpReq, auth.Attributes)
251 }
252
253 httpClient := &http.Client{}
254 if auth != nil {
255 if transport, _, errProxy := proxyutil.BuildHTTPTransport(auth.ProxyURL); errProxy == nil && transport != nil {
256 httpClient.Transport = transport
257 }
258 }
259
260 httpResp, errDo := httpClient.Do(httpReq)
261 if errDo != nil {
262 return nil, 0, errDo
263 }
264
265 bodyBytes, errRead := io.ReadAll(httpResp.Body)
266 if errClose := httpResp.Body.Close(); errClose != nil && errRead == nil {
267 errRead = errClose
268 }
269 if errRead != nil {
270 return nil, 0, errRead
271 }
272
273 if httpResp.StatusCode < http.StatusOK || httpResp.StatusCode >= http.StatusMultipleChoices {
274 return nil, 0, fmt.Errorf("models request failed with status %d: %s", httpResp.StatusCode, strings.TrimSpace(string(bodyBytes)))
275 }
276
277 count, errCount := countModels(bodyBytes)
278 if errCount != nil {
279 return nil, 0, errCount
280 }
281 return bodyBytes, count, nil
282}
283
284func codexModelsURL(clientVersion string) (string, error) {
285 u, err := url.Parse(codexModelsBaseURL + codexModelsPath)

Callers 1

mainFunction · 0.70

Calls 8

BuildHTTPTransportFunction · 0.92
codexModelsURLFunction · 0.85
countModelsFunction · 0.85
metaStringValueFunction · 0.70
DoMethod · 0.65
CloseMethod · 0.65
SetMethod · 0.45

Tested by

no test coverage detected