Skip to content

Commit 4e66bd0

Browse files
committed
refactor:Refactor project structure
1 parent 4326f25 commit 4e66bd0

File tree

18 files changed

+150
-162
lines changed

18 files changed

+150
-162
lines changed

README.md

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,15 @@ API 接口的包装和内部实现,可以帮助开发者更加方便的集成
5353
package com.company.example;
5454

5555
import io.agora.rest.AgoraException;
56-
import io.agora.rest.AgoraService;
57-
import io.agora.rest.core.AgoraProperty;
56+
import io.agora.rest.core.AgoraConfig;
5857
import io.agora.rest.core.BasicAuthCredential;
5958
import io.agora.rest.core.Credential;
6059
import io.agora.rest.core.RegionArea;
6160
import io.agora.rest.services.cloudrecording.api.req.StartResourceReq;
6261
import io.agora.rest.services.cloudrecording.api.res.AcquireResourceRes;
6362
import io.agora.rest.services.cloudrecording.api.res.StartResourceRes;
6463
import io.agora.rest.services.cloudrecording.api.res.StopResourceRes;
64+
import io.agora.rest.services.cloudrecording.CloudRecordingService;
6565
import io.agora.rest.services.cloudrecording.scenario.mix.req.AcquireMixRecordingResourceClientReq;
6666
import io.agora.rest.services.cloudrecording.scenario.mix.req.StartMixRecordingResourceClientReq;
6767
import io.agora.rest.services.cloudrecording.scenario.mix.res.QueryMixHLSAndMP4RecordingResourceRes;
@@ -94,26 +94,28 @@ public class Main {
9494

9595
public static void main(String[] args) throws Exception {
9696

97-
Credential basicAuthCredential = new BasicAuthCredential(username, password);
97+
Credential credential = new BasicAuthCredential(username, password);
9898

99-
// Initialize AgoraService
100-
AgoraService agoraService = new AgoraService(
101-
AgoraProperty.builder()
102-
.appId(appId)
103-
.credential(basicAuthCredential)
104-
// Specify the region where the server is located.
105-
// Optional values are CN, NA, EU, AP, and the client will automatically
106-
// switch to use the best domain name according to the configured region
107-
.regionArea(RegionArea.CNRegionArea)
108-
.build()
109-
);
99+
// Initialize AgoraConfig
100+
AgoraConfig agoraConfig = AgoraConfig.builder()
101+
.appId(appId)
102+
.credential(credential)
103+
// Specify the region where the server is located.
104+
// Optional values are CN, NA, EU, AP, and the client will automatically
105+
// switch to use the best domain name according to the configured region
106+
.regionArea(RegionArea.CNRegionArea)
107+
.build();
108+
109+
// Initialize CloudRecordingService
110+
111+
CloudRecordingService cloudRecordingService = CloudRecordingService.create(agoraConfig);
110112

111113

112114
AcquireResourceRes acquireResourceRes;
113115

114116
// Acquire resource
115117
try {
116-
acquireResourceRes = agoraService.cloudRecording()
118+
acquireResourceRes = cloudRecordingService
117119
.mixScenario()
118120
.acquire(cname, uid, AcquireMixRecordingResourceClientReq.builder()
119121
.build())
@@ -176,7 +178,7 @@ public class Main {
176178

177179
// Start resource
178180
try {
179-
startResourceRes = agoraService.cloudRecording()
181+
startResourceRes = cloudRecordingService
180182
.mixScenario()
181183
.start(cname, uid,
182184
acquireResourceRes.getResourceId(),
@@ -203,11 +205,11 @@ public class Main {
203205

204206
Thread.sleep(3000);
205207

206-
QueryMixHLSAndMP4RecordingResourceRes queryResourceResp;
208+
QueryMixHLSAndMP4RecordingResourceRes queryResourceRes;
207209

208210
// Query resource
209211
try {
210-
queryResourceRes = agoraService.cloudRecording()
212+
queryResourceRes = cloudRecordingService
211213
.mixScenario()
212214
.queryHLSAndMP4(startResourceRes.getResourceId(), startResourceRes.getSid())
213215
.block();
@@ -220,7 +222,7 @@ public class Main {
220222
return;
221223
}
222224

223-
if (queryResourceRes == null || queryResourceResp.getServerResponse() == null) {
225+
if (queryResourceRes == null || queryResourceRes.getServerResponse() == null) {
224226
System.out.println("failed to query resource");
225227
return;
226228
}
@@ -233,7 +235,7 @@ public class Main {
233235

234236
// Stop resource
235237
try {
236-
stopResourceRes = agoraService.cloudRecording()
238+
stopResourceRes = cloudRecordingService
237239
.mixScenario()
238240
.stop(cname, uid, startResourceRes.getResourceId(), startResourceRes.getSid(),
239241
true)
@@ -256,7 +258,6 @@ public class Main {
256258
}
257259
}
258260

259-
260261
```
261262

262263
更多的示例可在 [Example](./examples) 查看

agora-rest-client-core/src/main/java/io/agora/rest/AgoraService.java

Lines changed: 0 additions & 34 deletions
This file was deleted.

agora-rest-client-core/src/main/java/io/agora/rest/core/AgoraProperty.java renamed to agora-rest-client-core/src/main/java/io/agora/rest/core/AgoraConfig.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package io.agora.rest.core;
22

3-
public class AgoraProperty {
3+
public class AgoraConfig {
44
private final String appId;
55

66
private final Credential credential;
@@ -9,7 +9,7 @@ public class AgoraProperty {
99

1010
private final HttpProperty httpProperty;
1111

12-
private AgoraProperty(Builder builder) {
12+
private AgoraConfig(Builder builder) {
1313
this.appId = builder.appId;
1414
this.credential = builder.credential;
1515
this.regionArea = builder.regionArea;
@@ -38,7 +38,7 @@ public HttpProperty getHttpProperty() {
3838

3939
@Override
4040
public String toString() {
41-
return "AgoraProperty{" +
41+
return "AgoraConfig{" +
4242
"appId='" + appId + '\'' +
4343
", credential=" + credential +
4444
", regionArea=" + regionArea +
@@ -79,11 +79,11 @@ private Builder httpProperty(HttpProperty httpProperty) {
7979
return this;
8080
}
8181

82-
public AgoraProperty build() {
82+
public AgoraConfig build() {
8383
if (httpProperty == null) {
8484
this.httpProperty = HttpProperty.builder().build();
8585
}
86-
return new AgoraProperty(this);
86+
return new AgoraConfig(this);
8787
}
8888
}
8989
}

agora-rest-client-core/src/main/java/io/agora/rest/core/Context.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,5 @@ public interface Context {
77

88
<T> Mono<T> sendRequest(String path, HttpMethod method, Object requestBody, Class<T> clazz);
99

10-
AgoraProperty getProperty();
10+
AgoraConfig getAgoraConfig();
1111
}

agora-rest-client-core/src/main/java/io/agora/rest/core/DefaultContext.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,18 @@ public class DefaultContext implements Context {
1616

1717
private static final Logger logger = LoggerFactory.getLogger(DefaultContext.class);
1818

19-
private final AgoraProperty property;
19+
private final AgoraConfig agoraConfig;
2020

2121
private final HttpClient httpClient;
2222

2323
private final Codec codec;
2424

2525
private final DomainPool domainPool;
2626

27-
public DefaultContext(AgoraProperty property) {
28-
this.property = property;
29-
this.httpClient = HttpClientFactory.createHttpClient(property);
30-
this.domainPool = new DomainPool(property.getRegionArea());
27+
public DefaultContext(AgoraConfig agoraConfig) {
28+
this.agoraConfig = agoraConfig;
29+
this.httpClient = HttpClientFactory.createHttpClient(agoraConfig);
30+
this.domainPool = new DomainPool(agoraConfig.getRegionArea());
3131
this.codec = new JsonCodec();
3232
}
3333

@@ -73,7 +73,7 @@ public <T> Mono<T> sendRequest(String path, HttpMethod method, Object requestBod
7373
}
7474

7575
@Override
76-
public AgoraProperty getProperty() {
77-
return this.property;
76+
public AgoraConfig getAgoraConfig() {
77+
return this.agoraConfig;
7878
}
7979
}

agora-rest-client-core/src/main/java/io/agora/rest/core/HttpClientFactory.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,14 @@ public class HttpClientFactory {
1313

1414
private static final Logger logger = LoggerFactory.getLogger(HttpClientFactory.class);
1515

16-
public static HttpClient createHttpClient(AgoraProperty agoraProperty) {
16+
public static HttpClient createHttpClient(AgoraConfig agoraConfig) {
1717
ConnectionProvider connectionProvider = ConnectionProvider.builder("agora-rest-client")
18-
.maxConnections(agoraProperty.getHttpProperty().getHttpConnectionPoolSize())
19-
.pendingAcquireTimeout(Duration.ofMillis(agoraProperty.getHttpProperty().getHttpConnectionPendingAcquireTimout()))
20-
.maxIdleTime(Duration.ofMillis(agoraProperty.getHttpProperty().getHttpConnectionMaxIdleTime()))
21-
.maxLifeTime(Duration.ofMillis(agoraProperty.getHttpProperty().getHttpConnectionMaxLifeTime()))
22-
.evictInBackground(Duration.ofMillis(agoraProperty.getHttpProperty().getHttpConnectionEvictInBackground()))
23-
.pendingAcquireMaxCount(agoraProperty.getHttpProperty().getHttpConnectionPendingAcquireMaxCount())
18+
.maxConnections(agoraConfig.getHttpProperty().getHttpConnectionPoolSize())
19+
.pendingAcquireTimeout(Duration.ofMillis(agoraConfig.getHttpProperty().getHttpConnectionPendingAcquireTimout()))
20+
.maxIdleTime(Duration.ofMillis(agoraConfig.getHttpProperty().getHttpConnectionMaxIdleTime()))
21+
.maxLifeTime(Duration.ofMillis(agoraConfig.getHttpProperty().getHttpConnectionMaxLifeTime()))
22+
.evictInBackground(Duration.ofMillis(agoraConfig.getHttpProperty().getHttpConnectionEvictInBackground()))
23+
.pendingAcquireMaxCount(agoraConfig.getHttpProperty().getHttpConnectionPendingAcquireMaxCount())
2424
.lifo()
2525
.build();
2626

@@ -32,11 +32,11 @@ public static HttpClient createHttpClient(AgoraProperty agoraProperty) {
3232
System.getProperty("os.arch"),
3333
System.getProperty("os.name"),
3434
AgoraVersion.getVersion()));
35-
if (agoraProperty.getCredential() != null) {
36-
agoraProperty.getCredential().setAuthorization(h);
35+
if (agoraConfig.getCredential() != null) {
36+
agoraConfig.getCredential().setAuthorization(h);
3737
}
3838
})
39-
.wiretap("io.agora.rest.core.http", LogLevel.DEBUG, agoraProperty.getHttpProperty().getHttpLogFormat())
39+
.wiretap("io.agora.rest.core.http", LogLevel.DEBUG, agoraConfig.getHttpProperty().getHttpLogFormat())
4040
.doOnRequestError((req, t) -> logger.error("request error:{}", t.getMessage()));
4141

4242
}
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package io.agora.rest.services.cloudrecording;
22

3+
import io.agora.rest.core.AgoraConfig;
34
import io.agora.rest.core.Context;
5+
import io.agora.rest.core.DefaultContext;
46
import io.agora.rest.services.cloudrecording.api.*;
57
import io.agora.rest.services.cloudrecording.api.req.*;
68
import io.agora.rest.services.cloudrecording.api.res.*;
@@ -10,7 +12,7 @@
1012
import io.agora.rest.services.cloudrecording.scenario.web.WebScenario;
1113
import reactor.core.publisher.Mono;
1214

13-
public class CloudRecordingApi {
15+
public class CloudRecordingService {
1416

1517
private final AcquireResourceAPI acquireResourceAPI;
1618

@@ -28,7 +30,11 @@ public class CloudRecordingApi {
2830

2931
private final MixScenario mixScenario;
3032

31-
public CloudRecordingApi(Context context) {
33+
public static CloudRecordingService create(AgoraConfig agoraConfig) {
34+
return new CloudRecordingService(new DefaultContext(agoraConfig));
35+
}
36+
37+
protected CloudRecordingService(Context context) {
3238
this.acquireResourceAPI = new AcquireResourceAPI(context);
3339
this.queryResourceAPI = new QueryResourceAPI(context);
3440
this.startResourceAPI = new StartResourceAPI(context);

agora-rest-client-core/src/main/java/io/agora/rest/services/cloudrecording/README.md

Lines changed: 21 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
> ![](https://fullapp.oss-cn-beijing.aliyuncs.com/scenario_api/callapi/config/rtm_config2.jpg)
2727
> ![](https://fullapp.oss-cn-beijing.aliyuncs.com/agora-rest-client/go/open_cloud_recording.png)
2828
29-
## API V1 接口调用示例
29+
## API 接口调用示例
3030
### 获取云端录制资源
3131
> 在开始云端录制之前,你需要调用 acquire 方法获取一个 Resource ID。一个 Resource ID 只能用于一次云端录制服务。
3232
@@ -38,7 +38,7 @@
3838
- uid: 用户 UID
3939
- 更多 clientRequest中的参数见 [Acquire](https://doc.shengwang.cn/doc/cloud-recording/restful/cloud-recording/operations/post-v1-apps-appid-cloud_recording-acquire) 接口文档
4040

41-
通过调用`Acquire().Do`方法来实现获取云端录制资源
41+
通过调用`acquire`方法来实现获取云端录制资源
4242
```java
4343
String appId = "";
4444
String cname = "";
@@ -47,13 +47,20 @@
4747
String password = "";
4848

4949
Credential basicAuthCredential = new BasicAuthCredential(username, password);
50-
AgoraService agoraService = new AgoraService(
51-
AgoraProperty.builder()
52-
.appId(appId)
53-
.credential(basicAuthCredential)
54-
.regionArea(RegionArea.CNRegionArea)
55-
.build()
56-
);
50+
51+
// Initialize AgoraConfig
52+
AgoraConfig agoraConfig = AgoraConfig.builder()
53+
.appId(appId)
54+
.credential(credential)
55+
// Specify the region where the server is located.
56+
// Optional values are CN, NA, EU, AP, and the client will automatically
57+
// switch to use the best domain name according to the configured region
58+
.regionArea(region)
59+
.build();
60+
61+
// Initialize CloudRecordingService
62+
63+
CloudRecordingService cloudRecordingService = CloudRecordingService.create(agoraConfig);
5764

5865
AcquireResourceReq acquireResourceReq = AcquireResourceReq.builder().cname(cname).uid(uid)
5966
.clientRequest(AcquireResourceReq.ClientRequest.builder().scene(1)
@@ -64,7 +71,7 @@
6471

6572
AcquireResourceRes acquireResourceRes = null;
6673
try {
67-
acquireResourceRes = agoraService.cloudRecording().acquire(acquireResourceReq).block();
74+
acquireResourceRes = cloudRecordingService.acquire(acquireResourceReq).block();
6875

6976
assertNotNull(acquireResourceResp);
7077
logger.info("acquire resource response:{}", acquireResourceRes);
@@ -146,7 +153,7 @@
146153
StartResourceRes startResourceRes = null;
147154

148155
try {
149-
startResourceRes = agoraService.cloudRecording()
156+
startResourceRes = cloudRecordingService
150157
.start(resourceId,mode, startResourceReq)
151158
.block();
152159
logger.info("start resource response:{}", startResourceRes);
@@ -184,7 +191,7 @@
184191

185192
StopResourceRes stopResourceRes;
186193
try {
187-
stopResourceRes = agoraService.cloudRecording()
194+
stopResourceRes = cloudRecordingService
188195
.stop(resourceId, sid, mode, stopResourceReq)
189196
.block();
190197
logger.info("stop resource response:{}", stopResourceRes);
@@ -214,7 +221,7 @@
214221
QueryResourceRes queryResourceRes = null;
215222

216223
try {
217-
queryResourceRes = agoraService.cloudRecording()
224+
queryResourceRes = cloudRecordingService
218225
.query(resourceId, sid,mode)
219226
.block();
220227

@@ -284,7 +291,7 @@
284291

285292
UpdateResourceRes updateResourceRes;
286293
try {
287-
updateResourceRes = agoraService.cloudRecording()
294+
updateResourceRes = cloudRecordingService
288295
.update(resourceId, sid, mode, updateResourceReq)
289296
.block();
290297
logger.info("update resource response:{}", updateResourceRes);

agora-rest-client-core/src/main/java/io/agora/rest/services/cloudrecording/api/AcquireResourceAPI.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public AcquireResourceAPI(Context context) {
1616

1717
public Mono<AcquireResourceRes> handle(AcquireResourceReq request) {
1818
String path = String.format("/v1/apps/%s/cloud_recording/acquire",
19-
this.context.getProperty().getAppId());
19+
this.context.getAgoraConfig().getAppId());
2020
return this.context.sendRequest(path, HttpMethod.POST, request, AcquireResourceRes.class);
2121
}
2222
}

0 commit comments

Comments
 (0)