Skip to content

feat: Initialize Java SDK with support for all Coze APIs#1

Merged
chyroc merged 19 commits intocoze-dev:mainfrom
hanzeINGH:init_code
Dec 12, 2024
Merged

feat: Initialize Java SDK with support for all Coze APIs#1
chyroc merged 19 commits intocoze-dev:mainfrom
hanzeINGH:init_code

Conversation

@hanzeINGH
Copy link
Copy Markdown
Contributor

@hanzeINGH hanzeINGH commented Dec 12, 2024

  • Created initial project structure for the Java SDK
  • Implemented client classes for all Coze API endpoints
  • Added authentication and authorization mechanisms
  • Developed request and response models for each API
  • Implemented error handling and exception classes
  • Created utility classes for HTTP requests and JSON parsing
  • Added logging support for debugging and monitoring
  • Included basic unit tests for core functionality
  • Created documentation with usage examples and API reference
  • Set up build configuration and dependency management with Maven/Gradle
  • Added README with installation and quick start guide

Change-Id: I1a74861a59dfffacbd4e535e5f2c535aa9967387
Change-Id: I9a1213d5f702d9aac8ee4d30828a3ec00d0999a0
@chyroc
Copy link
Copy Markdown
Contributor

chyroc commented Dec 12, 2024

在 ApiResponseCallAdapter 中的 TODO 注释("todo 添加日志")应该被实现或移除
建议使用日志框架(如 SLF4J)替代直接的 System.out.println

@chyroc
Copy link
Copy Markdown
Contributor

chyroc commented Dec 12, 2024

MyInterceptor 中的调试日志可能会记录敏感信息,建议在生产环境中完全移除
建议添加日志级别控制,避免在生产环境中输出过多调试信息

Comment thread example/src/main/java/example/audio/room/CreateExample.java Outdated
Comment thread example/src/main/java/example/audio/room/CreateExample.java Outdated
Comment thread example/src/main/java/example/audio/room/CreateExample.java Outdated
Comment thread example/src/main/java/example/audio/room/CreateExample.java Outdated
Comment thread example/src/main/java/example/audio/room/CreateExample.java Outdated
Comment thread example/src/main/java/example/audio/room/CreateExample.java Outdated
Comment thread example/src/main/java/example/audio/room/CreateExample.java Outdated
Comment thread example/src/main/java/example/audio/room/CreateExample.java Outdated
Comment thread example/src/main/java/example/audio/speech/CreateExample.java Outdated
Comment thread example/src/main/java/example/audio/speech/CreateExample.java
Comment thread example/src/main/java/example/audio/voice/CloneExample.java Outdated
Comment thread example/src/main/java/example/auth/DevicesOAuthExample.java Outdated
Comment thread example/src/main/java/example/auth/DevicesOAuthExample.java Outdated
Comment thread example/src/main/java/example/auth/JWTsOauthExample.java Outdated
Comment thread example/src/main/java/example/auth/PkceOauthExample.java
Comment thread example/src/main/java/example/auth/PkceOauthExample.java Outdated
Change-Id: I191e5827d4362dce159c3f2d0306083a7b14cd14
Change-Id: Ia076aa54ad924055486c86e545154aff5c84bb1b
Change-Id: I5b140e44fd8e87850a410ca529c3155723a95b96
Change-Id: If780e3f5b04fd4a5ac4b507cdf4ef27ea0085834
Change-Id: I5c91800473c1031df7f9d3efe5e8f3836a1359cc
Change-Id: Icb1583d6a1bf2e3f4a282afefb428fe634be1d3b
Change-Id: I770693dd42982535b3ccc3e7fb8262121f0251b4
Change-Id: If4a92028dc7a725a865262c59d5e198170720519
Change-Id: Id12c3f2dd5d20b26fe09c22c8c114108ccf935d3
Change-Id: I887361495abb40023197763a197e2b46b0d978e7
Change-Id: I887361495abb40023197763a197e2b46b0d978e7
Change-Id: Iabfe94f6e3f30dfe05b494469630ac861ad45a9c
Change-Id: I23699ff6e3ca051fda8974ade055baa1e00b75bc
Change-Id: I5df08c090cd52a5535e40e21fc219d82e49fc670
Comment thread codecov.yaml
.botID(botID)
.voiceID(voiceID)
.build();
CreateRoomResp resp = coze.audio().rooms().create(req);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

print 一下


import java.io.IOException;

public class CreateSpeechExample {
Copy link
Copy Markdown
Contributor

@chyroc chyroc Dec 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改成这个格式:
coze.audio().speech().create -> AudioSpeechCreateExample

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

全改吗,我现在的 Example 都是动词在前,其实也是可以根据包名来区分实体的

Change-Id: Iabd29d9e741fb75f3c8534891ecee0dc9ca41d6c
.auth(authCli)
.readTimeout(10000)
.build();;
String saveFilePath = System.getenv("SAVE_FILE_PATH");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个保存到 Downloads 目录下一个随机的文件

import com.coze.openapi.service.service.CozeAPI;
import com.coze.openapi.service.auth.TokenAuth;

public class CloneVoiceExample {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AudioVoicesClone,其他的也改一下

.language(LanguageCode.ZH)
.voiceID(System.getenv("COZE_VOICE_ID"))
.text("your text")
.previewText("your preview text")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉这个,这个不是必填

.audioFormat(AudioFormat.M4A)
.language(LanguageCode.ZH)
.voiceID(System.getenv("COZE_VOICE_ID"))
.text("your text")
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

去掉这个


import java.util.Iterator;

public class ListVoiceExample {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

改一下


PageResp<Voice> resp = coze.audio().voices().list(ListVoiceReq.builder().pageSize(10).build());
Iterator<Voice> iterator = resp.getIterator();
while (iterator.hasNext()) {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个是自带的迭代器吗

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对,java 原生的

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

java 定义了迭代器,是一个 interface,我们可以自定义实现

/*
* The space permissions for which the Access Token is granted can be specified. As following codes:
* */
DeviceAuthResp wCodeResp = oauth.getDeviceCode("workspaceID");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这一行是不是应该注释?这个文件可以 run 起来吗,申请了两个 code?

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

是,我改成注解

/*
* The space permissions for which the Access Token is granted can be specified. As following codes:
* */
oauthURL = oauth.getOAuthURL(redirectURI, null, "workspaceID");
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有两个地方,统一改了

import com.coze.openapi.client.common.BaseReq;
public interface BotAPI {
@GET("/v1/space/published_bots_list")
Call<BaseResponse<ListBotResp>> list(@Query("space_id") String spaceID, @Query("page_index") Integer page, @Query("page_size") Integer pageSize, @Tag BaseReq baseReq);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个参数不是结构体吗

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这整个目录对于使用者都是不感知的,这里可以理解成请求我们 openapi 的代码

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

baseReq 是我用来让用户可以配置请求级别的超时时间的

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

在执行请求的时候不会写到 Request 里面

Change-Id: I55ef6dfb256e60821a9e6d59b821560179b3813e
public interface ChatAPI {

@POST("/v3/chat")
Call<BaseResponse<Chat>> chat(@Query("conversation_id") String conversationID, @Body CreateChatReq req, @Tag BaseReq baseReq);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create

public interface CozeAuthAPI {
@Headers({"Content-Type: application/json"})
@POST("/api/permission/oauth2/token")
Single<Response<OAuthToken>> retrieve(@HeaderMap Map<String, String> headers, @Body GetAccessTokenReq req);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个为啥是retrieve

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

刷新 token,获取 Access token 都是用这个接口,具体是做啥就由入参来控制,我就叫这个了


@POST("/open_api/knowledge/document/create")
@Headers({"Content-Type: application/json","Agw-Js-Conv: str"})
Call<CreateDocumentResp> CreateDocument(@Body CreateDocumentReq req, @Tag BaseReq baseReq);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.documents.create

public interface WorkflowRunAPI {

@POST("/v1/workflow/run")
Call<RunWorkflowResp> run(@Body RunWorkflowReq req, @Tag BaseReq baseReq);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.workflows.runs.create


@POST("/v1/workflow/stream_run")
@Streaming
Call<ResponseBody> stream(@Body RunWorkflowReq req, @Tag BaseReq baseReq);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.workflows.runs.stream


@POST("/v1/workflow/stream_resume")
@Streaming
Call<ResponseBody> resume(@Body ResumeRunReq req, @Tag BaseReq baseReq);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.workflows.runs.resume

@chyroc chyroc changed the title init sdk feat: Initialize Java SDK with support for all Coze APIs Dec 12, 2024
@chyroc chyroc added the feature label Dec 12, 2024
Change-Id: Iacb00780c3eb6ea23a932157b9f74aa41a5ffdbf
@chyroc chyroc merged commit b590e05 into coze-dev:main Dec 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants