-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b477db1
commit 702e55f
Showing
8 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 8 additions & 0 deletions
8
src/main/java/io/github/stefanbratanov/jvm/openai/ProjectApiKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
/** Represents an individual API key in a project. */ | ||
public record ProjectApiKey( | ||
String redactedValue, String name, long createdAt, String id, Owner owner) { | ||
|
||
public record Owner(String type, ProjectUser user, ProjectServiceAccount serviceAccount) {} | ||
} |
103 changes: 103 additions & 0 deletions
103
src/main/java/io/github/stefanbratanov/jvm/openai/ProjectApiKeysClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
import java.net.URI; | ||
import java.net.http.HttpClient; | ||
import java.net.http.HttpRequest; | ||
import java.net.http.HttpResponse; | ||
import java.time.Duration; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
/** | ||
* Manage API keys for a given project. Supports listing and deleting keys for users. This API does | ||
* not allow issuing keys for users, as users need to authorize themselves to generate keys. | ||
* | ||
* <p>Based on <a href="https://platform.openai.com/docs/api-reference/project-api-keys">Project API | ||
* Keys</a> | ||
*/ | ||
public final class ProjectApiKeysClient extends OpenAIClient { | ||
|
||
private static final String API_KEYS_SEGMENT = "/api_keys"; | ||
|
||
private final URI baseUrl; | ||
|
||
ProjectApiKeysClient( | ||
URI baseUrl, | ||
String[] authenticationHeaders, | ||
HttpClient httpClient, | ||
Optional<Duration> requestTimeout) { | ||
super(authenticationHeaders, httpClient, requestTimeout); | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
/** | ||
* Returns a list of API keys in the project. | ||
* | ||
* @param projectId The ID of the project. | ||
* @param after A cursor for use in pagination. after is an object ID that defines your place in | ||
* the list. | ||
* @param limit A limit on the number of objects to be returned. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public PaginatedProjectApiKeys listProjectApiKeys( | ||
String projectId, Optional<String> after, Optional<Integer> limit) { | ||
String queryParameters = | ||
createQueryParameters( | ||
Map.of(Constants.LIMIT_QUERY_PARAMETER, limit, Constants.AFTER_QUERY_PARAMETER, after)); | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri( | ||
baseUrl.resolve( | ||
Endpoint.PROJECTS.getPath() | ||
+ "/" | ||
+ projectId | ||
+ API_KEYS_SEGMENT | ||
+ queryParameters)) | ||
.GET() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), PaginatedProjectApiKeys.class); | ||
} | ||
|
||
public record PaginatedProjectApiKeys( | ||
List<ProjectApiKey> data, String firstId, String lastId, boolean hasMore) {} | ||
|
||
/** | ||
* Retrieves an API key in the project. | ||
* | ||
* @param projectId The ID of the project. | ||
* @param keyId The ID of the API key. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public ProjectApiKey retrieveProjectApiKey(String projectId, String keyId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri( | ||
baseUrl.resolve( | ||
Endpoint.PROJECTS.getPath() + "/" + projectId + API_KEYS_SEGMENT + "/" + keyId)) | ||
.GET() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), ProjectApiKey.class); | ||
} | ||
|
||
/** | ||
* Deletes an API key from the project. | ||
* | ||
* @param projectId The ID of the project. | ||
* @param keyId The ID of the API key. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public DeletionStatus deleteProjectApiKey(String projectId, String keyId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri( | ||
baseUrl.resolve( | ||
Endpoint.PROJECTS.getPath() + "/" + projectId + API_KEYS_SEGMENT + "/" + keyId)) | ||
.DELETE() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), DeletionStatus.class); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters