File tree Expand file tree Collapse file tree
main/java/org/devlive/sdk/openai
test/java/org/devlive/sdk/openai Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -43,3 +43,32 @@ Returns:
4343}
4444```
4545
46+ ### Create a key
47+
48+ ---
49+
50+ Create a new key.
51+
52+ ``` java
53+ UserKeyEntity configure= UserKeyEntity . builder()
54+ .name(" Create first key" )
55+ .action(" create" )
56+ .build();
57+ client. createUserAPIKey(configure)
58+ ```
59+
60+ Return:
61+
62+ ``` json
63+ {
64+ "result" : " success" ,
65+ "key" : {
66+ "sensitive_id" : " sk-xxx" ,
67+ "object" : " api_key" ,
68+ "name" : " Test" ,
69+ "created" : 1688525108 ,
70+ "last_use" : null ,
71+ "publishable" : false
72+ }
73+ }
74+ ```
Original file line number Diff line number Diff line change 44import org .devlive .sdk .openai .entity .CompletionChatEntity ;
55import org .devlive .sdk .openai .entity .CompletionEntity ;
66import org .devlive .sdk .openai .entity .ModelEntity ;
7+ import org .devlive .sdk .openai .entity .UserKeyEntity ;
78import org .devlive .sdk .openai .response .CompleteChatResponse ;
89import org .devlive .sdk .openai .response .CompleteResponse ;
910import org .devlive .sdk .openai .response .ModelResponse ;
@@ -46,4 +47,10 @@ public interface DefaultApi
4647 */
4748 @ GET (value = "dashboard/user/api_keys" )
4849 Single <UserKeyResponse > fetchUserAPIKeys ();
50+
51+ /**
52+ * Create a key for the given
53+ */
54+ @ POST (value = "dashboard/user/api_keys" )
55+ Single <UserKeyResponse > fetchCreateUserAPIKey (@ Body UserKeyEntity configure );
4956}
Original file line number Diff line number Diff line change 33import org .devlive .sdk .openai .entity .CompletionChatEntity ;
44import org .devlive .sdk .openai .entity .CompletionEntity ;
55import org .devlive .sdk .openai .entity .ModelEntity ;
6+ import org .devlive .sdk .openai .entity .UserKeyEntity ;
67import org .devlive .sdk .openai .response .CompleteChatResponse ;
78import org .devlive .sdk .openai .response .CompleteResponse ;
89import org .devlive .sdk .openai .response .ModelResponse ;
@@ -41,4 +42,10 @@ public UserKeyResponse getKeys()
4142 return this .api .fetchUserAPIKeys ()
4243 .blockingGet ();
4344 }
45+
46+ public UserKeyResponse createUserAPIKey (UserKeyEntity configure )
47+ {
48+ return this .api .fetchCreateUserAPIKey (configure )
49+ .blockingGet ();
50+ }
4451}
Original file line number Diff line number Diff line change @@ -35,5 +35,11 @@ public class UserKeyEntity
3535 private String lastUseTime ;
3636
3737 @ JsonProperty (value = "publishable" )
38- private boolean publishable ;
38+ private Boolean publishable ;
39+
40+ /**
41+ * When creating a key use
42+ */
43+ @ JsonProperty (value = "action" )
44+ private String action ;
3945}
Original file line number Diff line number Diff line change 88import okhttp3 .Request ;
99import okhttp3 .Response ;
1010import okhttp3 .ResponseBody ;
11+ import okio .Buffer ;
1112import org .apache .commons .lang3 .ObjectUtils ;
1213import org .apache .commons .lang3 .StringUtils ;
1314import org .devlive .sdk .openai .exception .AuthorizedException ;
@@ -50,6 +51,13 @@ public Response intercept(Chain chain) throws IOException
5051
5152 Request original = chain .request ();
5253 Request request = this .headers (original );
54+
55+ if (ObjectUtils .isNotEmpty (request .body ())) {
56+ Buffer buffer = new Buffer ();
57+ request .body ().writeTo (buffer );
58+ log .debug ("Request body {}" , buffer .readUtf8 ());
59+ }
60+
5361 Response response = chain .proceed (request );
5462 if (!response .isSuccessful ()) {
5563 log .error ("Failed to intercept request" );
@@ -72,7 +80,7 @@ public Response intercept(Chain chain) throws IOException
7280 }
7381
7482 // Has error
75- if (response .code () == 404 || response .code () == 400 ) {
83+ if (response .code () == 404 || response .code () == 400 || response . code () == 403 ) {
7684 ResponseBody body = response .body ();
7785 if (ObjectUtils .isEmpty (body )) {
7886 throw new NullPointerException ("Failed to intercept request because no body" );
Original file line number Diff line number Diff line change @@ -22,4 +22,10 @@ public class UserKeyResponse
2222
2323 @ JsonProperty (value = "data" )
2424 private List <UserKeyEntity > keys ;
25+
26+ @ JsonProperty (value = "result" )
27+ private String result ;
28+
29+ @ JsonProperty (value = "key" )
30+ private UserKeyEntity key ;
2531}
Original file line number Diff line number Diff line change 55import org .devlive .sdk .openai .entity .CompletionChatEntity ;
66import org .devlive .sdk .openai .entity .CompletionEntity ;
77import org .devlive .sdk .openai .entity .CompletionMessageEntity ;
8+ import org .devlive .sdk .openai .entity .UserKeyEntity ;
89import org .devlive .sdk .openai .exception .AuthorizedException ;
10+ import org .devlive .sdk .openai .exception .RequestException ;
911import org .devlive .sdk .openai .model .CompletionModel ;
1012import org .junit .Assert ;
1113import org .junit .Before ;
@@ -118,4 +120,14 @@ public void testGetKeys()
118120 {
119121 Assert .assertNotNull (client .getKeys ());
120122 }
123+
124+ @ Test
125+ public void testCreateUserAPIKey ()
126+ {
127+ UserKeyEntity configure = UserKeyEntity .builder ()
128+ .name ("Create first key" )
129+ .action ("create" )
130+ .build ();
131+ Assert .assertThrows (RequestException .class , () -> client .createUserAPIKey (configure ));
132+ }
121133}
You can’t perform that action at this time.
0 commit comments