Skip to content

Code Generator #610

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 27 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
optimize api
  • Loading branch information
YangSen-qn committed Aug 28, 2024
commit 4312d5ece982417be027d8bb3be7e14a0824d442
34 changes: 26 additions & 8 deletions src/main/java/com/qiniu/storage/Api.java
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,10 @@ public static class Request implements Cloneable {
* @param urlPrefix 请求的 urlPrefix, scheme + host
*/
protected Request(String urlPrefix) {
if (StringUtils.isNullOrEmpty(urlPrefix)) {
return;
}

try {
URL url = new URL(urlPrefix);
this.scheme = url.getProtocol();
Expand Down Expand Up @@ -628,6 +632,15 @@ public StringMap getHeader() throws QiniuException {
return header;
}

/**
* 构造 header 信息,如果需要设置请求体,子类需要重写
*
* @throws QiniuException 组装 header 时的异常,一般为缺失必要参数的异常
*/
protected void buildHeader() throws QiniuException {

}

/**
* 获取 URL
*
Expand Down Expand Up @@ -798,6 +811,7 @@ boolean canRetry() {
protected void prepareToRequest() throws QiniuException {
buildPath();
buildQuery();
buildHeader();
buildBodyInfo();
}

Expand Down Expand Up @@ -1046,14 +1060,18 @@ protected RequestBody get() {
}

final MultipartBody.Builder b = new MultipartBody.Builder();
b.addFormDataPart(name, fileName, body);

fields.forEach(new StringMap.Consumer() {
@Override
public void accept(String key, Object value) {
b.addFormDataPart(key, value.toString());
}
});
if (StringUtils.isNullOrEmpty(name)) {
b.addFormDataPart(name, fileName, body);
}

if (fields != null) {
fields.forEach(new StringMap.Consumer() {
@Override
public void accept(String key, Object value) {
b.addFormDataPart(key, value.toString());
}
});
}

b.setType(MediaType.parse("multipart/form-data"));

Expand Down
11 changes: 8 additions & 3 deletions src/main/java/com/qiniu/storage/ApiInterceptorRetryHosts.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,14 @@ Api.Response intercept(Api.Request request, Api.Handler handler) throws QiniuExc
}

String reqHost = request.getHost();
String firstHost = hostProvider.provider();
if (!hostProvider.isHostValid(reqHost) && !StringUtils.isNullOrEmpty(firstHost)) {
request.setHost(firstHost);
if (!hostProvider.isHostValid(reqHost)) {
// 支持不配置默认的 host,未配置则从 provider 中获取
String firstHost = hostProvider.provider();
if (!StringUtils.isNullOrEmpty(firstHost)) {
request.setHost(firstHost);
} else {
throw QiniuException.unrecoverable("no host provide");
}
}

if (retryMax == 0) {
Expand Down