Skip to content
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

Remove unused downloadFile helpers #637

Merged
merged 1 commit into from
Feb 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 4 additions & 4 deletions .apigentools-info
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
"spec_versions": {
"v1": {
"apigentools_version": "1.4.1.dev2",
"regenerated": "2021-02-03 08:38:29.208682",
"spec_repo_commit": "f02c467"
"regenerated": "2021-02-03 16:24:07.623332",
"spec_repo_commit": "113773d"
},
"v2": {
"apigentools_version": "1.4.1.dev2",
"regenerated": "2021-02-03 08:38:39.829272",
"spec_repo_commit": "f02c467"
"regenerated": "2021-02-03 16:24:15.926643",
"spec_repo_commit": "113773d"
}
}
}
66 changes: 0 additions & 66 deletions src/main/java/com/datadog/api/v1/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -688,17 +688,6 @@ public ApiClient setDebugging(boolean debugging) {
return this;
}

/**
* The path of temporary folder used to store downloaded files from endpoints
* with file response. The default value is <code>null</code>, i.e. using
* the system's default tempopary folder.
*
* @return Temp folder path
*/
public String getTempFolderPath() {
return tempFolderPath;
}

/**
* Set temp folder path
* @param tempFolderPath Temp folder path
Expand Down Expand Up @@ -1108,10 +1097,6 @@ public <T> T deserialize(Response response, GenericType<T> returnType) throws Ap
if ("byte[]".equals(returnType.toString())) {
// Handle binary response (byte array).
return (T) response.readEntity(byte[].class);
} else if (returnType.getRawType() == File.class) {
// Handle file downloading.
T file = (T) downloadFileFromResponse(response);
return file;
}

String contentType = null;
Expand All @@ -1125,57 +1110,6 @@ public <T> T deserialize(Response response, GenericType<T> returnType) throws Ap
return response.readEntity(returnType);
}

/**
* Download file from the given response.
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(Response response) throws ApiException {
try {
File file = prepareDownloadFile(response);
Files.copy(response.readEntity(InputStream.class), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}

public File prepareDownloadFile(Response response) throws IOException {
String filename = null;
String contentDisposition = (String) response.getHeaders().getFirst("Content-Disposition");
if (contentDisposition != null && !"".equals(contentDisposition)) {
// Get filename from the Content-Disposition header.
Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
Matcher matcher = pattern.matcher(contentDisposition);
if (matcher.find())
filename = matcher.group(1);
}

String prefix;
String suffix = null;
if (filename == null) {
prefix = "download-";
suffix = "";
} else {
int pos = filename.lastIndexOf('.');
if (pos == -1) {
prefix = filename + "-";
} else {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
}

/**
* Invoke API by sending HTTP request with the given options.
*
Expand Down
66 changes: 0 additions & 66 deletions src/main/java/com/datadog/api/v2/client/ApiClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,17 +505,6 @@ public ApiClient setDebugging(boolean debugging) {
return this;
}

/**
* The path of temporary folder used to store downloaded files from endpoints
* with file response. The default value is <code>null</code>, i.e. using
* the system's default tempopary folder.
*
* @return Temp folder path
*/
public String getTempFolderPath() {
return tempFolderPath;
}

/**
* Set temp folder path
* @param tempFolderPath Temp folder path
Expand Down Expand Up @@ -925,10 +914,6 @@ public <T> T deserialize(Response response, GenericType<T> returnType) throws Ap
if ("byte[]".equals(returnType.toString())) {
// Handle binary response (byte array).
return (T) response.readEntity(byte[].class);
} else if (returnType.getRawType() == File.class) {
// Handle file downloading.
T file = (T) downloadFileFromResponse(response);
return file;
}

String contentType = null;
Expand All @@ -942,57 +927,6 @@ public <T> T deserialize(Response response, GenericType<T> returnType) throws Ap
return response.readEntity(returnType);
}

/**
* Download file from the given response.
* @param response Response
* @return File
* @throws ApiException If fail to read file content from response and write to disk
*/
public File downloadFileFromResponse(Response response) throws ApiException {
try {
File file = prepareDownloadFile(response);
Files.copy(response.readEntity(InputStream.class), file.toPath(), StandardCopyOption.REPLACE_EXISTING);
return file;
} catch (IOException e) {
throw new ApiException(e);
}
}

public File prepareDownloadFile(Response response) throws IOException {
String filename = null;
String contentDisposition = (String) response.getHeaders().getFirst("Content-Disposition");
if (contentDisposition != null && !"".equals(contentDisposition)) {
// Get filename from the Content-Disposition header.
Pattern pattern = Pattern.compile("filename=['\"]?([^'\"\\s]+)['\"]?");
Matcher matcher = pattern.matcher(contentDisposition);
if (matcher.find())
filename = matcher.group(1);
}

String prefix;
String suffix = null;
if (filename == null) {
prefix = "download-";
suffix = "";
} else {
int pos = filename.lastIndexOf('.');
if (pos == -1) {
prefix = filename + "-";
} else {
prefix = filename.substring(0, pos) + "-";
suffix = filename.substring(pos);
}
// File.createTempFile requires the prefix to be at least three characters long
if (prefix.length() < 3)
prefix = "download-";
}

if (tempFolderPath == null)
return File.createTempFile(prefix, suffix);
else
return File.createTempFile(prefix, suffix, new File(tempFolderPath));
}

/**
* Invoke API by sending HTTP request with the given options.
*
Expand Down