-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOpenAIAuthenticator.java
More file actions
31 lines (25 loc) · 948 Bytes
/
OpenAIAuthenticator.java
File metadata and controls
31 lines (25 loc) · 948 Bytes
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
package com.UoB.AILearningTool;
import java.io.IOException;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OpenAIAuthenticator {
private final Logger log = LoggerFactory.getLogger(OpenAIAuthenticator.class);
private String apiKey;
// Constructor that initializes the API key
public OpenAIAuthenticator() {
this.apiKey = "$OPENAI_API_KEY";
log.info("OpenAI API key set.");
}
// Returns the API key as a "Bearer token" (used in authorization headers)
public String getBearerToken() {
if (this.apiKey == null || this.apiKey.equals("OPENAI_API_KEY") || this.apiKey.isEmpty()) {
log.error("API Key not set or is empty.");
throw new IllegalStateException("API Key for OpenAI is not set.");
}
return this.apiKey;
}
}