Skip to content

Commit e62dca6

Browse files
committed
refactor: Enhance code documentation and improve method visibility
1 parent 188c8e7 commit e62dca6

File tree

8 files changed

+60
-37
lines changed

8 files changed

+60
-37
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public Builder domainArea(DomainArea domainArea) {
7474
return this;
7575
}
7676

77-
private Builder httpProperty(HttpProperty httpProperty) {
77+
public Builder httpProperty(HttpProperty httpProperty) {
7878
this.httpProperty = httpProperty;
7979
return this;
8080
}

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
import io.netty.handler.codec.http.HttpHeaders;
44

5+
/**
6+
* @brief BasicAuthCredential provides basic authentication for agora rest client
7+
* @since v0.3.0
8+
*/
59
public class BasicAuthCredential implements Credential {
610

711
private final String username;
@@ -10,6 +14,10 @@ public class BasicAuthCredential implements Credential {
1014

1115
private final static String name = "BasicAuth";
1216

17+
/**
18+
* @param username Customer ID obtained from the Agora console
19+
* @param password Customer secret obtained from the Agora console
20+
*/
1321
public BasicAuthCredential(String username, String password) {
1422
this.username = username;
1523
this.password = password;

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

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,8 @@ public <T> Mono<T> sendRequest(String path, HttpMethod method, Object requestBod
3838

3939
return Mono.defer(() -> httpClient
4040
.doOnRequest((req, conn) ->
41-
logger.debug("request:{},{}{}", req.method(), req.requestHeaders().get("host"), req.uri()))
41+
logger.debug("start request:{},{}", req.method(), req.resourceUrl())
42+
)
4243
.headers(h -> h.add("Content-Type", "application/json"))
4344
.request(method)
4445
.uri(Mono.just(this.domainPool.getCurrentUrl() + path))
@@ -53,11 +54,16 @@ public <T> Mono<T> sendRequest(String path, HttpMethod method, Object requestBod
5354

5455
return Mono.just(byteBuf);
5556
})
56-
).map(buf -> {
57-
T response = codec.decode(buf, clazz);
58-
buf.release();
59-
logger.debug("response:{}", response);
60-
return response;
57+
).flatMap(buf -> {
58+
if (clazz == Void.class) {
59+
buf.release();
60+
return Mono.empty();
61+
} else {
62+
T response = codec.decode(buf, clazz);
63+
buf.release();
64+
logger.debug("response:{}", response);
65+
return Mono.just(response);
66+
}
6167
}))
6268
.retryWhen(Retry.fixedDelay(3, Duration.ofMillis(500)).
6369
filter(e -> e instanceof ChannelBindException || e instanceof UnknownHostException)

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,10 +36,11 @@ public void checkError(ByteBuf buf) {
3636

3737
public AgoraException convertException(HttpClientResponse response, ByteBuf buf) {
3838
HttpResponseStatus status = response.status();
39+
String xRequestId = response.responseHeaders().get("x-request-id");
3940

4041
byte[] body = ByteUtils.toByteArray(buf);
41-
String reason = String.format("%s %s -> %d %s,body:%s", response.method(), response.uri(), status.code(),
42-
status.reasonPhrase(), new String(body));
42+
String reason = String.format("%s %s -> %d %s,body:%s,x-request-id:%s", response.method(), response.resourceUrl(), status.code(),
43+
status.reasonPhrase(), new String(body), xRequestId);
4344
logger.info("reason:{}", reason);
4445

4546
AgoraException exception = new AgoraUnknownException(reason);

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

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,35 +4,36 @@
44
import java.util.Collections;
55
import java.util.List;
66

7-
public enum DomainArea {
8-
/**
9-
* Unknown domain area
10-
*/
11-
Unknown(Collections.emptyList(), Collections.emptyList()),
127

8+
/**
9+
* @brief DomainArea represents the global regions where the Open API gateway endpoint is located
10+
* @note Choose the appropriate area based on your service deployment region.
11+
* @since v0.3.0
12+
*/
13+
public enum DomainArea {
1314
/**
14-
* US domain area
15+
* US represents the western and eastern regions of the United States
1516
*/
1617
US(
1718
Arrays.asList("api-us-west-1", "api-us-east-1"),
1819
Arrays.asList("agora.io", "sd-rtn.com")),
1920

2021
/**
21-
* EU domain area
22+
* EU represents the western and central regions of Europe
2223
*/
2324
EU(
2425
Arrays.asList("api-eu-west-1", "api-eu-central-1"),
2526
Arrays.asList("agora.io", "sd-rtn.com")),
2627

2728
/**
28-
* AP domain area
29+
* AP represents the southeastern and northeastern regions of Asia-Pacific
2930
*/
3031
AP(
3132
Arrays.asList("api-ap-southeast-1", "api-ap-northeast-1"),
3233
Arrays.asList("agora.io", "sd-rtn.com")),
3334

3435
/**
35-
* CN domain area
36+
* CN represents the eastern and northern regions of Chinese mainland
3637
*/
3738
CN(
3839
Arrays.asList("api-cn-east-1", "api-cn-north-1"),

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public class DomainPool {
3131
private final Logger logger = LoggerFactory.getLogger(DomainPool.class);
3232

3333
public DomainPool(DomainArea domainArea) {
34-
if (domainArea == null || domainArea == DomainArea.Unknown) {
34+
if (domainArea == null) {
3535
throw new AgoraInvalidArgumentException("invalid domain area");
3636
}
3737

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import org.slf4j.LoggerFactory;
77
import reactor.netty.http.client.HttpClient;
88
import reactor.netty.resources.ConnectionProvider;
9+
import reactor.netty.transport.logging.AdvancedByteBufFormat;
910

1011
import java.time.Duration;
1112

@@ -36,7 +37,7 @@ public static HttpClient createHttpClient(AgoraConfig agoraConfig) {
3637
agoraConfig.getCredential().setAuthorization(h);
3738
}
3839
})
39-
.wiretap("io.agora.rest.core.http", LogLevel.DEBUG, agoraConfig.getHttpProperty().getHttpLogFormat())
40+
.wiretap("io.agora.rest.core.http", LogLevel.DEBUG, AdvancedByteBufFormat.SIMPLE)
4041
.doOnRequestError((req, t) -> logger.error("request error:{}", t.getMessage()));
4142

4243
}

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

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

3-
import reactor.netty.transport.logging.AdvancedByteBufFormat;
4-
3+
/**
4+
* @brief HttpProperty represents the properties of the HTTP client
5+
* @since v0.3.0
6+
*/
57
public class HttpProperty {
68

9+
/**
10+
* Maximum number of connections in the connection pool
11+
*/
712
private final int httpConnectionPoolSize;
813

14+
/**
15+
* Maximum idle time of a connection in the connection pool, unit: ms
16+
*/
917
private final int httpConnectionMaxIdleTime;
1018

19+
/**
20+
* Maximum lifetime of a connection in the connection pool,unit: ms
21+
*/
1122
private final int httpConnectionMaxLifeTime;
1223

24+
/**
25+
* Time to evict connections in the background, unit: ms
26+
*/
1327
private final int httpConnectionEvictInBackground;
1428

29+
/**
30+
* Timeout for acquiring a connection from the connection pool, unit: ms
31+
*/
1532
private final int httpConnectionPendingAcquireTimout;
1633

34+
/**
35+
* The maximum number of registered requests for acquire to keep in a pending queue
36+
*/
1737
private final int httpConnectionPendingAcquireMaxCount;
1838

19-
private final AdvancedByteBufFormat httpLogFormat;
20-
2139
public static Builder builder() {
2240
return new Builder();
2341
}
@@ -29,7 +47,6 @@ private HttpProperty(Builder builder) {
2947
httpConnectionEvictInBackground = builder.httpConnectionEvictInBackground;
3048
httpConnectionPendingAcquireTimout = builder.httpConnectionPendingAcquireTimout;
3149
httpConnectionPendingAcquireMaxCount = builder.httpConnectionPendingAcquireMaxCount;
32-
httpLogFormat = builder.httpLogFormat;
3350
}
3451

3552
public int getHttpConnectionPoolSize() {
@@ -56,10 +73,6 @@ public int getHttpConnectionPendingAcquireMaxCount() {
5673
return httpConnectionPendingAcquireMaxCount;
5774
}
5875

59-
public AdvancedByteBufFormat getHttpLogFormat() {
60-
return httpLogFormat;
61-
}
62-
6376
@Override
6477
public String toString() {
6578
return "HttpProperty{" +
@@ -69,10 +82,10 @@ public String toString() {
6982
", httpConnectionEvictInBackground=" + httpConnectionEvictInBackground +
7083
", httpConnectionPendingAcquireTimout=" + httpConnectionPendingAcquireTimout +
7184
", httpConnectionPendingAcquireMaxCount=" + httpConnectionPendingAcquireMaxCount +
72-
", httpLogFormat=" + httpLogFormat +
7385
'}';
7486
}
7587

88+
7689
public static final class Builder {
7790
private int httpConnectionPoolSize = 50;
7891

@@ -86,8 +99,6 @@ public static final class Builder {
8699

87100
private int httpConnectionPendingAcquireMaxCount = 100;
88101

89-
private AdvancedByteBufFormat httpLogFormat = AdvancedByteBufFormat.SIMPLE;
90-
91102
private Builder() {
92103

93104
}
@@ -122,11 +133,6 @@ public Builder httpConnectionPendingAcquireMaxCount(int val) {
122133
return this;
123134
}
124135

125-
public Builder httpLogFormat(AdvancedByteBufFormat val) {
126-
httpLogFormat = val;
127-
return this;
128-
}
129-
130136
public HttpProperty build() {
131137
return new HttpProperty(this);
132138
}

0 commit comments

Comments
 (0)