-
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
5eaf39b
commit c902893
Showing
10 changed files
with
200 additions
and
8 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
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
25 changes: 25 additions & 0 deletions
25
src/main/java/io/github/stefanbratanov/jvm/openai/ModifyUserRequest.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,25 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
public record ModifyUserRequest(String role) { | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private String role; | ||
|
||
/** | ||
* @param role `owner` or `reader` | ||
*/ | ||
public Builder role(String role) { | ||
this.role = role; | ||
return this; | ||
} | ||
|
||
public ModifyUserRequest build() { | ||
return new ModifyUserRequest(role); | ||
} | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
/** Represents an individual user within an organization. */ | ||
public record User(String id, String name, String email, String role, long addedAt) {} |
100 changes: 100 additions & 0 deletions
100
src/main/java/io/github/stefanbratanov/jvm/openai/UsersClient.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,100 @@ | ||
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 users and their role in an organization. Users will be automatically added to the Default | ||
* project. | ||
* | ||
* <p>Based on <a href="https://platform.openai.com/docs/api-reference/users">Users</a> | ||
*/ | ||
public final class UsersClient extends OpenAIClient { | ||
|
||
private final URI baseUrl; | ||
|
||
UsersClient( | ||
URI baseUrl, | ||
String[] authenticationHeaders, | ||
HttpClient httpClient, | ||
Optional<Duration> requestTimeout) { | ||
super(authenticationHeaders, httpClient, requestTimeout); | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
/** | ||
* Lists all of the users in the organization. | ||
* | ||
* @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 PaginatedUsers listUsers(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.USERS.getPath() + queryParameters)) | ||
.GET() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), PaginatedUsers.class); | ||
} | ||
|
||
public record PaginatedUsers(List<User> data, String firstId, String lastId, boolean hasMore) {} | ||
|
||
/** | ||
* Modifies a user's role in the organization. | ||
* | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public User modifyUser(ModifyUserRequest request) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder(Constants.CONTENT_TYPE_HEADER, Constants.JSON_MEDIA_TYPE) | ||
.uri(baseUrl.resolve(Endpoint.USERS.getPath())) | ||
.POST(createBodyPublisher(request)) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), User.class); | ||
} | ||
|
||
/** | ||
* Retrieves a user by their identifier. | ||
* | ||
* @param userId The ID of the user. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public User retrieveUser(String userId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.USERS.getPath() + "/" + userId)) | ||
.GET() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), User.class); | ||
} | ||
|
||
/** | ||
* Deletes a user from the organization. | ||
* | ||
* @param userId The ID of the user. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public DeletionStatus deleteUser(String userId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.USERS.getPath() + "/" + userId)) | ||
.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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -732,6 +732,19 @@ public Invite randomInvite() { | |
randomLong(10_000, 1_000_000)); | ||
} | ||
|
||
public ModifyUserRequest randomModifyUserRequest() { | ||
return ModifyUserRequest.newBuilder().role(oneOf("owner", "reader")).build(); | ||
} | ||
|
||
public User randomUser() { | ||
return new User( | ||
randomString(5), | ||
randomString(7), | ||
"[email protected]", | ||
oneOf("owner", "reader"), | ||
randomLong(10_000, 1_000_000)); | ||
} | ||
|
||
private ChunkingStrategy.StaticChunkingStrategy randomStaticChunkingStrategy() { | ||
int randomMaxChunkSizeTokens = randomInt(100, 4096); | ||
return ChunkingStrategy.staticChunkingStrategy( | ||
|