MCPcopy Create free account
hub / github.com/dashscope/dashscope-sdk-java / ApiKey

Class ApiKey

src/main/java/com/alibaba/dashscope/utils/ApiKey.java:13–56  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

11import lombok.extern.slf4j.Slf4j;
12
13@Slf4j
14public final class ApiKey {
15 public static String getApiKey(String apiKey) throws NoApiKeyException {
16 if (apiKey != null) { // api specified
17 return apiKey;
18 }
19 if (Constants.apiKey != null) { // Constants.apiKey=xxx
20 return Constants.apiKey;
21 }
22 String value = System.getenv(Constants.DASHSCOPE_API_KEY_ENV);
23 if (value != null) { // from environment variable.
24 return value;
25 }
26 String apiKeyFilePath = System.getenv(Constants.DASHSCOPE_API_KEY_FILE_PATH_ENV);
27 if (apiKeyFilePath == null || Files.notExists(Paths.get(apiKeyFilePath))) {
28 // no api key file specified, check default api key file.
29 Path homePath = Paths.get(System.getProperty("user.home"));
30 Path defaultApiKeyPath = homePath.resolve(".dashscope").resolve("api_key");
31 if (Files.notExists(defaultApiKeyPath)) {
32 apiKeyFilePath = null;
33 } else {
34 try {
35 BufferedReader reader = new BufferedReader(new FileReader(defaultApiKeyPath.toString()));
36 apiKey = reader.readLine().trim();
37 reader.close();
38 return apiKey;
39 } catch (Exception e) {
40 throw new NoApiKeyException();
41 }
42 }
43 }
44 if (apiKeyFilePath != null) {
45 try {
46 BufferedReader reader = new BufferedReader(new FileReader(apiKeyFilePath));
47 apiKey = reader.readLine().trim();
48 reader.close();
49 return apiKey;
50 } catch (Exception e) {
51 throw new NoApiKeyException();
52 }
53 }
54 throw new NoApiKeyException();
55 }
56}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected