Skip to content

Commit 57436f3

Browse files
committed
moved api key resolution to the ScanApi class
1 parent 84a793b commit 57436f3

2 files changed

Lines changed: 24 additions & 24 deletions

File tree

src/main/java/com/scanoss/Scanner.java

Lines changed: 1 addition & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ private Scanner(Boolean skipSnippets, Boolean allExtensions, Boolean obfuscate,
132132
this.timeout = timeout;
133133
this.retryLimit = retryLimit;
134134
this.url = url;
135-
this.apiKey = resolveApiKey(apiKey);
135+
this.apiKey = apiKey;
136136
this.scanFlags = scanFlags;
137137
this.sbomType = sbomType;
138138
this.sbom = sbom;
@@ -171,28 +171,6 @@ private Scanner(Boolean skipSnippets, Boolean allExtensions, Boolean obfuscate,
171171
this.folderFilter = Objects.requireNonNullElseGet(folderFilter, () -> FolderFilterFactory.build(this.filterConfig));
172172
}
173173

174-
/**
175-
* Resolve the API key for Scanoss API
176-
*
177-
* @param apiKey The API key provided by the user
178-
* @return The resolved API key, either from the user-provided value or environment variable
179-
*/
180-
private static String resolveApiKey(String apiKey) {
181-
if (apiKey != null && !apiKey.isBlank()) {
182-
return apiKey;
183-
}
184-
try {
185-
String envApiKey = System.getenv("SCANOSS_API_KEY");
186-
if (envApiKey != null && !envApiKey.isBlank()) {
187-
log.debug( "Using SCANOSS_API_KEY env value");
188-
return envApiKey;
189-
}
190-
} catch (RuntimeException e) {
191-
log.warn("Unable to read SCANOSS_API_KEY from environment: {}", e.getMessage());
192-
}
193-
return apiKey;
194-
}
195-
196174
/**
197175
* Generate a WFP/Fingerprint for the given file
198176
*

src/main/java/com/scanoss/rest/ScanApi.java

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private ScanApi(String scanType, Duration timeout, Integer retryLimit, String ur
8484
this.timeout = timeout;
8585
this.retryLimit = retryLimit;
8686
this.url = url;
87-
this.apiKey = apiKey;
87+
this.apiKey = resolveApiKey(apiKey);
8888
this.flags = flags;
8989
this.sbomType = sbomType;
9090
this.sbom = sbom;
@@ -130,6 +130,28 @@ private ScanApi(String scanType, Duration timeout, Integer retryLimit, String ur
130130
}
131131
}
132132

133+
/**
134+
* Resolve the API key for Scanoss API
135+
*
136+
* @param apiKey The API key provided by the user
137+
* @return The resolved API key, either from the user-provided value or environment variable
138+
*/
139+
private static String resolveApiKey(String apiKey) {
140+
if (apiKey != null && !apiKey.isBlank()) {
141+
return apiKey;
142+
}
143+
try {
144+
String envApiKey = System.getenv("SCANOSS_API_KEY");
145+
if (envApiKey != null && !envApiKey.isBlank()) {
146+
log.debug( "Using SCANOSS_API_KEY env value");
147+
return envApiKey;
148+
}
149+
} catch (RuntimeException e) {
150+
log.warn("Unable to read SCANOSS_API_KEY from environment: {}", e.getMessage());
151+
}
152+
return apiKey;
153+
}
154+
133155
/**
134156
* Scan the given WFP
135157
*

0 commit comments

Comments
 (0)