Skip to content
This repository has been archived by the owner on Jul 21, 2024. It is now read-only.

Commit

Permalink
Api 1.0.4 App 3.0.8 Workaround for OpenApi QPS rate limit
Browse files Browse the repository at this point in the history
  • Loading branch information
eritpchy committed Apr 8, 2023
1 parent acd9cd4 commit d8f6e46
Show file tree
Hide file tree
Showing 17 changed files with 141 additions and 33 deletions.
20 changes: 10 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,18 @@ implementation "com.squareup.okhttp3:logging-interceptor:3.12.13" //api19
implementation "com.google.code.gson:gson:2.8.9"
//主要
implementation "net.xdow:aliyundrive-sdk-openapi:1.0.3"
implementation "net.xdow:aliyundrive-sdk-webapi:1.0.3"
implementation "net.xdow:aliyundrive-sdk-openapi:1.0.4"
implementation "net.xdow:aliyundrive-sdk-webapi:1.0.4"
//可选
implementation "net.xdow:webdav:1.0.3"
implementation "net.xdow:webdav-jakarta:1.0.3"
implementation "net.xdow:webdav-javax:1.0.3"
implementation "net.xdow:aliyundrive-webdav-internal:1.0.3"
implementation "net.xdow:aliyundrive-android-core:1.0.3"
implementation "net.xdow:jap-http:1.0.3"
implementation "net.xdow:jap-http-jakarta-adapter:1.0.3"
implementation "net.xdow:jap-http-javax-adapter:1.0.3"
implementation "net.xdow:webdav:1.0.4"
implementation "net.xdow:webdav-jakarta:1.0.4"
implementation "net.xdow:webdav-javax:1.0.4"
implementation "net.xdow:aliyundrive-webdav-internal:1.0.4"
implementation "net.xdow:aliyundrive-android-core:1.0.4"
implementation "net.xdow:jap-http:1.0.4"
implementation "net.xdow:jap-http-jakarta-adapter:1.0.4"
implementation "net.xdow:jap-http-javax-adapter:1.0.4"
```
## 基础用法
```java
Expand Down
2 changes: 1 addition & 1 deletion aliyundrive-android-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ buildscript {
apply plugin: 'com.android.library'

group 'net.xdow'
version '1.0.3'
version '1.0.4'
description 'AliyunDrive Webdav Android Core'

repositories {
Expand Down
2 changes: 1 addition & 1 deletion aliyundrive-sdk-openapi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'net.xdow'
version '1.0.3'
version '1.0.4'
description 'AliyunDrive OpenApi SDK'

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,24 @@ public Response intercept(Chain chain) throws IOException {
.dns(new Dns() {
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
return Arrays.asList(Address.getAllByName(hostname));
List<InetAddress> list = new ArrayList<>();
UnknownHostException unknownHostException = null;
try {
list.addAll(Dns.SYSTEM.lookup(hostname));
} catch (UnknownHostException e) {
unknownHostException = e;
}
try {
list.addAll(Arrays.asList(Address.getAllByName(hostname)));
} catch (UnknownHostException e) {
if (unknownHostException == null) {
unknownHostException = e;
}
}
if (list.size() <= 0 && unknownHostException != null) {
throw unknownHostException;
}
return list;
}
})
.followRedirects(true)
Expand Down
2 changes: 1 addition & 1 deletion aliyundrive-sdk-webapi/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'net.xdow'
version '1.0.3'
version '1.0.4'
description 'AliyunDrive WebApi SDK'

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,24 @@ public Request authenticate(Route route, Response response) throws IOException {
.dns(new Dns() {
@Override
public List<InetAddress> lookup(String hostname) throws UnknownHostException {
return Arrays.asList(Address.getAllByName(hostname));
List<InetAddress> list = new ArrayList<>();
UnknownHostException unknownHostException = null;
try {
list.addAll(Dns.SYSTEM.lookup(hostname));
} catch (UnknownHostException e) {
unknownHostException = e;
}
try {
list.addAll(Arrays.asList(Address.getAllByName(hostname)));
} catch (UnknownHostException e) {
if (unknownHostException == null) {
unknownHostException = e;
}
}
if (list.size() <= 0 && unknownHostException != null) {
throw unknownHostException;
}
return list;
}
})
.followRedirects(true)
Expand Down
2 changes: 1 addition & 1 deletion aliyundrive-webdav-internal/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'net.xdow'
version '1.0.3'
version '1.0.4'
description 'AliyunDrive Webdav internal'

java {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class AliyunDriveProperties {
private transient String clientId;

private transient String authorizationCode;
private transient String aliyunAccessTokenUrl = "https://adrive.xdow.net/oauth/access_token?code=%s&refresh_token=%s";
private transient String aliyunAccessTokenUrl = "https://adrive.xdow.net/oauth/access_token?code=%s&refresh_token=%s&ver=1.0.4";
private transient String aliyunAuthorizeUrl = "https://adrive.xdow.net/oauth/authorize?redirect_uri=%s";

private transient Auth auth = new Auth();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,17 @@ public Set<AliyunDriveFileInfo> load(String key) throws Exception {
return AliyunDriveClientService.this.getTFiles2(key);
}
});

private LoadingCache<String, AliyunDriveResponse.FileGetDownloadUrlInfo> tFileDownloadUrlCache = CacheBuilder.newBuilder()
.initialCapacity(128)
.maximumSize(10240)
.expireAfterWrite(AliyunDriveConstant.MAX_DOWNLOAD_URL_EXPIRE_TIME_SEC, TimeUnit.SECONDS)
.build(new CacheLoader<String, AliyunDriveResponse.FileGetDownloadUrlInfo>() {
@Override
public AliyunDriveResponse.FileGetDownloadUrlInfo load(String key) throws Exception {
return fileGetDownloadUrlInternal(key);
}
});
private final T mAliyunDrive;

public AliyunDriveClientService(Class<? extends IAliyunDrive> aliyunDriveCls, AliyunDriveProperties aliyunDriveProperties) {
Expand Down Expand Up @@ -124,6 +135,24 @@ private List<AliyunDriveFileInfo> fileListFromApi(String nodeId, String marker,
query.setOrderBy(AliyunDriveEnum.OrderBy.UpdatedAt);
query.setOrderDirection(AliyunDriveEnum.OrderDirection.Desc);
AliyunDriveResponse.FileListInfo res = this.mAliyunDrive.fileList(query).execute();
if (res.isError()) {
if ("TooManyRequests".equals(res.getCode())) {
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
}
res = this.mAliyunDrive.fileList(query).execute();
}
}
if (res.isError()) {
if ("TooManyRequests".equals(res.getCode())) {
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
}
res = this.mAliyunDrive.fileList(query).execute();
}
}
all.addAll(res.getItems());
String nextMarker = res.getNextMarker();
if (StringUtils.isEmpty(nextMarker)) {
Expand Down Expand Up @@ -398,16 +427,12 @@ public AliyunDriveFileInfo getTFileByPath(String path) {

public Response download(String path, JapHttpRequest request, long size) {
AliyunDriveFileInfo file = getTFileByPath(path);
AliyunDriveRequest.FileGetDownloadUrlInfo query = new AliyunDriveRequest.FileGetDownloadUrlInfo(
getDefaultDriveId(), file.getFileId()
);
query.setExpireSec(AliyunDriveConstant.MAX_DOWNLOAD_URL_EXPIRE_TIME_SEC);
AliyunDriveResponse.FileGetDownloadUrlInfo res = this.mAliyunDrive.fileGetDownloadUrl(query).execute();
String range = extractRangeHeader(request, size);
String ifRange = extractIfRangeHeader(request);
AliyunDriveResponse.FileGetDownloadUrlInfo res = fileGetDownloadUrlInfo(file.getFileId());
if (res.isError()) {
throw new WebdavException(new WebdavException(res.getCode(), res.getMessage()));
}
String range = extractRangeHeader(request, size);
String ifRange = extractIfRangeHeader(request);
String url = res.getUrl().replaceAll("^https://", "http://");
try {
return this.mAliyunDrive.download(url, range, ifRange).execute();
Expand All @@ -416,6 +441,55 @@ public Response download(String path, JapHttpRequest request, long size) {
}
}

private synchronized AliyunDriveResponse.FileGetDownloadUrlInfo fileGetDownloadUrlInfo(String fileId) {
AliyunDriveResponse.FileGetDownloadUrlInfo res;
try {
res = tFileDownloadUrlCache.get(fileId);
Date expirationDate = res.getExpiration();
if (expirationDate != null && new Date().after(expirationDate)) {
tFileDownloadUrlCache.invalidate(fileId);
res = tFileDownloadUrlCache.get(fileId);
}
return res;
} catch (ExecutionException e) {
res = new AliyunDriveResponse.FileGetDownloadUrlInfo();
res.setCode(e.getMessage());
res.setMessage(e.toString());
return res;
}
}

private synchronized AliyunDriveResponse.FileGetDownloadUrlInfo fileGetDownloadUrlInternal(String fileId) {
AliyunDriveRequest.FileGetDownloadUrlInfo query = new AliyunDriveRequest.FileGetDownloadUrlInfo(
getDefaultDriveId(), fileId
);
query.setExpireSec(AliyunDriveConstant.MAX_DOWNLOAD_URL_EXPIRE_TIME_SEC);
AliyunDriveResponse.FileGetDownloadUrlInfo res = this.mAliyunDrive.fileGetDownloadUrl(query).execute();
if (res.isError()) {
if ("TooManyRequests".equals(res.getCode())) {
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
}
res = this.mAliyunDrive.fileGetDownloadUrl(query).execute();
}
}
if (res.isError()) {
if ("TooManyRequests".equals(res.getCode())) {
try {
TimeUnit.SECONDS.sleep(6);
} catch (InterruptedException e) {
}
res = this.mAliyunDrive.fileGetDownloadUrl(query).execute();
}
}
if (res.isError()) {
LOGGER.error("fileGetDownloadUrlInternal code: " + res.getCode() + " message: " + res.getMessage());
return null;
}
return res;
}

private String extractIfRangeHeader(JapHttpRequest request) {
String ifRange = request.getHeader("if-range");
if (ifRange == null) {
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ plugins {
}

group 'com.github'
version '3.0.6'
version '3.0.8'
sourceCompatibility = '17'

configurations {
Expand Down
4 changes: 2 additions & 2 deletions jap-http-adapter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<dependency>
<groupId>com.fujieid.jap.http.adapter</groupId>
<artifactId>jap-http-jakarta-adapter</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
</dependency>
```
2. 使用适配器重新构造
Expand All @@ -39,7 +39,7 @@ new JakartaResponseAdapter(HttpServletResponse);
<dependency>
<groupId>com.fujieid.jap.http.adapter</groupId>
<artifactId>jap-http-blade-adapter</artifactId>
<version>1.0.3</version>
<version>1.0.4</version>
</dependency>
```
2. 使用适配器重新构造
Expand Down
2 changes: 1 addition & 1 deletion jap-http-adapter/jap-http-jakarta-adapter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'net.xdow'
version '1.0.3'
version '1.0.4'
description 'jap-http servlet interface jakarta adapter'

java {
Expand Down
2 changes: 1 addition & 1 deletion jap-http-adapter/jap-http-javax-adapter/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'net.xdow'
version '1.0.3'
version '1.0.4'
description 'jap-http servlet interface javax adapter'

java {
Expand Down
2 changes: 1 addition & 1 deletion jap-http/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'net.xdow'
version '1.0.3'
version '1.0.4'
description 'jap-http servlet interface'

java {
Expand Down
2 changes: 1 addition & 1 deletion webdav-jakarta/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'net.xdow'
version '1.0.3'
version '1.0.4'
description 'AliyunDrive Webdav jakarta implement'

java {
Expand Down
2 changes: 1 addition & 1 deletion webdav-javax/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'net.xdow'
version '1.0.3'
version '1.0.4'
description 'AliyunDrive Webdav javax implement'

java {
Expand Down
2 changes: 1 addition & 1 deletion webdav/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ plugins {
}

group 'net.xdow'
version '1.0.3'
version '1.0.4'
description 'AliyunDrive Webdav core'

java {
Expand Down

0 comments on commit d8f6e46

Please sign in to comment.