MCPcopy Create free account
hub / github.com/bitswired/rustgpt / valid_openai_api_key

Function valid_openai_api_key

src/middleware.rs:80–110  ·  view source on GitHub ↗
(
    Extension(current_user): Extension<Option<User>>,
    req: Request<B>,
    next: Next<B>,
)

Source from the content-addressed store, hash-verified

78}
79
80pub async fn valid_openai_api_key<B>(
81 Extension(current_user): Extension<Option<User>>,
82 req: Request<B>,
83 next: Next<B>,
84) -> Response
85where
86 B: Send + 'static,
87{
88 let key = current_user
89 .unwrap()
90 .openai_api_key
91 .unwrap_or(String::new());
92
93 let client = reqwest::Client::new();
94 match client
95 .get("https://api.openai.com/v1/engines")
96 .bearer_auth(&key)
97 .send()
98 .await
99 {
100 Ok(res) => {
101 if res.status().is_success() {
102 next.run(req).await
103 } else {
104 println!("failure!");
105 error_response(403, "You API key is not set or invalid. Go to Settings.")
106 }
107 }
108 Err(_) => error_response(403, "You API key is not set or invalid. Go to Settings"),
109 }
110}
111
112pub async fn handle_error<B>(
113 Extension(current_user): Extension<Option<User>>,

Callers

nothing calls this directly

Calls 1

error_responseFunction · 0.85

Tested by

no test coverage detected