-
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
cf1aca9
commit 5eaf39b
Showing
10 changed files
with
301 additions
and
23 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
11 changes: 11 additions & 0 deletions
11
src/main/java/io/github/stefanbratanov/jvm/openai/Invite.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,11 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
/** Represents an individual `invite` to the organization. */ | ||
public record Invite( | ||
String id, | ||
String email, | ||
String role, | ||
String status, | ||
long invitedAt, | ||
long expiresAt, | ||
Long acceptedAt) {} |
34 changes: 34 additions & 0 deletions
34
src/main/java/io/github/stefanbratanov/jvm/openai/InviteRequest.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,34 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
public record InviteRequest(String email, String role) { | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static class Builder { | ||
|
||
private String email; | ||
private String role; | ||
|
||
/** | ||
* @param email Send an email to this address | ||
*/ | ||
public Builder email(String email) { | ||
this.email = email; | ||
return this; | ||
} | ||
|
||
/** | ||
* @param role `owner` or `reader` | ||
*/ | ||
public Builder role(String role) { | ||
this.role = role; | ||
return this; | ||
} | ||
|
||
public InviteRequest build() { | ||
return new InviteRequest(email, role); | ||
} | ||
} | ||
} |
102 changes: 102 additions & 0 deletions
102
src/main/java/io/github/stefanbratanov/jvm/openai/InvitesClient.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,102 @@ | ||
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; | ||
|
||
/** | ||
* Invite and manage invitations for an organization. Invited users are automatically added to the | ||
* Default project. | ||
* | ||
* <p>Based on <a href="https://platform.openai.com/docs/api-reference/invite">Invites</a> | ||
*/ | ||
public class InvitesClient extends OpenAIClient { | ||
|
||
private final URI baseUrl; | ||
|
||
InvitesClient( | ||
URI baseUrl, | ||
String[] authenticationHeaders, | ||
HttpClient httpClient, | ||
Optional<Duration> requestTimeout) { | ||
super(authenticationHeaders, httpClient, requestTimeout); | ||
this.baseUrl = baseUrl; | ||
} | ||
|
||
/** | ||
* Returns a list of invites 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 PaginatedInvites listInvites(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.INVITES.getPath() + queryParameters)) | ||
.GET() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), PaginatedInvites.class); | ||
} | ||
|
||
public record PaginatedInvites( | ||
List<Invite> data, String firstId, String lastId, boolean hasMore) {} | ||
|
||
/** | ||
* Create an invite for a user to the organization. The invite must be accepted by the user before | ||
* they have access to the organization. | ||
* | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Invite createInvite(InviteRequest request) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder(Constants.CONTENT_TYPE_HEADER, Constants.JSON_MEDIA_TYPE) | ||
.uri(baseUrl.resolve(Endpoint.INVITES.getPath())) | ||
.POST(createBodyPublisher(request)) | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Invite.class); | ||
} | ||
|
||
/** | ||
* Retrieves an invite. | ||
* | ||
* @param inviteId The ID of the invite to retrieve. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public Invite retrieveInvite(String inviteId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.INVITES.getPath() + "/" + inviteId)) | ||
.GET() | ||
.build(); | ||
HttpResponse<byte[]> httpResponse = sendHttpRequest(httpRequest); | ||
return deserializeResponse(httpResponse.body(), Invite.class); | ||
} | ||
|
||
/** | ||
* Delete an invite. If the invite has already been accepted, it cannot be deleted. | ||
* | ||
* @param inviteId The ID of the invite to delete. | ||
* @throws OpenAIException in case of API errors | ||
*/ | ||
public DeletionStatus deleteInvite(String inviteId) { | ||
HttpRequest httpRequest = | ||
newHttpRequestBuilder() | ||
.uri(baseUrl.resolve(Endpoint.INVITES.getPath() + "/" + inviteId)) | ||
.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
48 changes: 48 additions & 0 deletions
48
src/test/java/io/github/stefanbratanov/jvm/openai/OpenAIAdminIntegrationTest.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,48 @@ | ||
package io.github.stefanbratanov.jvm.openai; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
import java.time.Instant; | ||
import java.util.List; | ||
import java.util.Optional; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
import org.junit.jupiter.api.condition.EnabledIfEnvironmentVariable; | ||
|
||
@EnabledIfEnvironmentVariable(named = "OPENAI_ADMIN_KEY", matches = ".*\\S.*") | ||
class OpenAIAdminIntegrationTest { | ||
|
||
private static OpenAI openAI; | ||
|
||
@BeforeAll | ||
public static void setUp() { | ||
String adminKey = System.getenv("OPENAI_ADMIN_KEY"); | ||
openAI = OpenAI.newBuilder().adminKey(adminKey).build(); | ||
} | ||
|
||
@Test | ||
void testInvitesClient() { | ||
InvitesClient invitesClient = openAI.invitesClient(); | ||
|
||
InviteRequest inviteRequest = | ||
InviteRequest.newBuilder().email("foobar@example.com").role("reader").build(); | ||
|
||
Invite invite = invitesClient.createInvite(inviteRequest); | ||
|
||
assertThat(invite.email()).isEqualTo("foobar@example.com"); | ||
assertThat(invite.status()).isEqualTo("pending"); | ||
assertThat(invite.expiresAt()).isGreaterThan(Instant.now().getEpochSecond()); | ||
|
||
List<Invite> invites = invitesClient.listInvites(Optional.empty(), Optional.empty()).data(); | ||
|
||
assertThat(invites).hasSize(1).containsExactly(invite); | ||
|
||
Invite retrievedInvite = invitesClient.retrieveInvite(invite.id()); | ||
|
||
assertThat(retrievedInvite).isEqualTo(invite); | ||
|
||
// cleanup | ||
DeletionStatus deletionStatus = invitesClient.deleteInvite(invite.id()); | ||
assertThat(deletionStatus.deleted()).isTrue(); | ||
} | ||
} |
Oops, something went wrong.