Skip to content

Commit

Permalink
Feature/headers in download request (#68)
Browse files Browse the repository at this point in the history
* Added logic to include headers in download request
  • Loading branch information
knighto82 authored Sep 3, 2024
1 parent c9198bf commit 0873ecb
Show file tree
Hide file tree
Showing 9 changed files with 221 additions and 45 deletions.
66 changes: 64 additions & 2 deletions src/main/java/io/github/jpmorganchase/fusion/Fusion.java
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,29 @@ public Map<String, Distribution> listDistributions(String dataset, String series
* @throws OAuthException if a token could not be retrieved for authentication
*/
public void download(String catalogName, String dataset, String seriesMember, String distribution, String path) {
download(catalogName, dataset, seriesMember, distribution, path, new HashMap<>());
}

/**
* Download a single distribution to the local filesystem
*
* @param catalogName identifier of the catalog to be queried
* @param dataset a String representing the dataset identifier to download.
* @param seriesMember a String representing the series member identifier.
* @param distribution a String representing the distribution identifier, this is the file extension.
* @param path the absolute file path where the file should be written.
* @param headers http headers to be provided in the request. For headers with multiple instances, the value should be a csv list
* @throws APICallException if the call to the Fusion API fails
* @throws FileDownloadException if there is an issue handling the response from Fusion API
* @throws OAuthException if a token could not be retrieved for authentication
*/
public void download(
String catalogName,
String dataset,
String seriesMember,
String distribution,
String path,
Map<String, String> headers) {

String url = String.format(
"%scatalogs/%s/datasets/%s/datasetseries/%s/distributions/%s",
Expand All @@ -405,7 +428,7 @@ public void download(String catalogName, String dataset, String seriesMember, St
throw new FusionException(String.format("Unable to save to target path %s", path), e);
}
String filepath = String.format("%s/%s_%s_%s.%s", path, catalogName, dataset, seriesMember, distribution);
this.api.callAPIFileDownload(url, filepath, catalogName, dataset);
this.api.callAPIFileDownload(url, filepath, catalogName, dataset, headers);
}

/**
Expand All @@ -420,6 +443,23 @@ public void download(String catalogName, String dataset, String seriesMember, St
* @throws OAuthException if a token could not be retrieved for authentication
*/
public void download(String catalogName, String dataset, String seriesMember, String distribution) {
this.download(catalogName, dataset, seriesMember, distribution, getDefaultPath(), new HashMap<>());
}

/**
* Download a single distribution to the local filesystem. By default, this will write to downloads folder.
*
* @param catalogName identifier of the catalog to be queried
* @param dataset a String representing the dataset identifier to download.
* @param seriesMember a String representing the series member identifier.
* @param distribution a String representing the distribution identifier, this is the file extension.
* @param headers http headers to be provided in the request. For headers with multiple instances, the value should be a csv list
* @throws APICallException if the call to the Fusion API fails
* @throws FileDownloadException if there is an issue handling the response from Fusion API
* @throws OAuthException if a token could not be retrieved for authentication
*/
public void download(
String catalogName, String dataset, String seriesMember, String distribution, Map<String, String> headers) {
this.download(catalogName, dataset, seriesMember, distribution, getDefaultPath());
}

Expand Down Expand Up @@ -456,7 +496,29 @@ public InputStream downloadStream(String catalogName, String dataset, String ser
String url = String.format(
"%scatalogs/%s/datasets/%s/datasetseries/%s/distributions/%s",
this.rootURL, catalogName, dataset, seriesMember, distribution);
return this.api.callAPIFileDownload(url, catalogName, dataset);
return downloadStream(catalogName, dataset, seriesMember, distribution, new HashMap<>());
}

/**
* Download a single distribution and return the data as an InputStream
* Note that users of this method are required to close the returned InputStream. Failure to do so will
* result in resource leaks, including the Http connection used to communicate with Fusion
*
* @param catalogName identifier of the catalog to be queried
* @param dataset a String representing the dataset identifier to download.
* @param seriesMember a String representing the series member identifier.
* @param distribution a String representing the distribution identifier, this is the file extension.
* @param headers http headers to be provided in the request. For headers with multiple instances, the value should be a csv list
* @throws APICallException if the call to the Fusion API fails
* @throws FileDownloadException if there is an issue handling the response from Fusion API
* @throws OAuthException if a token could not be retrieved for authentication
*/
public InputStream downloadStream(
String catalogName, String dataset, String seriesMember, String distribution, Map<String, String> headers) {
String url = String.format(
"%scatalogs/%s/datasets/%s/datasetseries/%s/distributions/%s",
this.rootURL, catalogName, dataset, seriesMember, distribution);
return this.api.callAPIFileDownload(url, catalogName, dataset, headers);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,16 @@ public String callAPI(String apiPath) throws APICallException {
}

@Override
public void callAPIFileDownload(String apiPath, String fileName, String catalog, String dataset)
public void callAPIFileDownload(
String apiPath, String fileName, String catalog, String dataset, Map<String, String> headers)
throws APICallException, FileDownloadException {
downloader.callAPIFileDownload(apiPath, fileName, catalog, dataset);
downloader.callAPIFileDownload(apiPath, fileName, catalog, dataset, headers);
}

@Override
public InputStream callAPIFileDownload(String apiPath, String catalog, String dataset)
public InputStream callAPIFileDownload(String apiPath, String catalog, String dataset, Map<String, String> headers)
throws APICallException, FileDownloadException {
return downloader.callAPIFileDownload(apiPath, catalog, dataset);
return downloader.callAPIFileDownload(apiPath, catalog, dataset, headers);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
import io.github.jpmorganchase.fusion.api.exception.APICallException;
import io.github.jpmorganchase.fusion.api.exception.FileDownloadException;
import java.io.InputStream;
import java.util.Map;

public interface APIDownloadOperations {

default void callAPIFileDownload(String apiPath, String fileName, String catalog, String dataset)
default void callAPIFileDownload(
String apiPath, String fileName, String catalog, String dataset, Map<String, String> headers)
throws APICallException, FileDownloadException {}

default InputStream callAPIFileDownload(String apiPath, String catalog, String dataset)
default InputStream callAPIFileDownload(String apiPath, String catalog, String dataset, Map<String, String> headers)
throws APICallException, FileDownloadException {
return null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,22 @@ public class FusionAPIDownloadOperations implements APIDownloadOperations {
* @param filePath the absolute path where the file will be persisted.
* @param catalog the catalog the distribution to be downloaded is part of
* @param dataset the dataset the distribution to be downloaded is a member of
* @param headers http headers to be provided in the request. For headers with multiple instances, the value should be a csv list
* @throws APICallException if the call to the Fusion API fails
* @throws FileDownloadException if there is an issue handling the response from Fusion API
* @throws OAuthException if a token could not be retrieved for authentication
*/
@Override
public void callAPIFileDownload(String apiPath, String filePath, String catalog, String dataset)
public void callAPIFileDownload(
String apiPath, String filePath, String catalog, String dataset, Map<String, String> headers)
throws APICallException, FileDownloadException {

DownloadRequest dr = DownloadRequest.builder()
.apiPath(apiPath)
.filePath(filePath)
.catalog(catalog)
.dataset(dataset)
.headers(headers)
.build();

downloadToFile(dr);
Expand All @@ -73,19 +76,21 @@ public void callAPIFileDownload(String apiPath, String filePath, String catalog,
* @param apiPath the URL of the API endpoint to call
* @param catalog the catalog the distribution to be downloaded is part of
* @param dataset the dataset the distribution to be downloaded is a member of
* @param headers http headers to be provided in the request. For headers with multiple instances, the value should be a csv list
* @throws APICallException if the call to the Fusion API fails
* @throws FileDownloadException if there is an issue handling the response from Fusion API
* @throws OAuthException if a token could not be retrieved for authentication
*/
@Override
public InputStream callAPIFileDownload(String apiPath, String catalog, String dataset)
public InputStream callAPIFileDownload(String apiPath, String catalog, String dataset, Map<String, String> headers)
throws APICallException, FileDownloadException {

DownloadRequest dr = DownloadRequest.builder()
.apiPath(apiPath)
.catalog(catalog)
.dataset(dataset)
.isDownloadToStream(true)
.headers(headers)
.build();

return downloadToStream(dr);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.github.jpmorganchase.fusion.api.request;

import java.util.Map;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
Expand All @@ -18,4 +19,5 @@ public class DownloadRequest {
private String dataset;
private String filePath;
private boolean isDownloadToStream;
private Map<String, String> headers;
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import lombok.Builder;
import lombok.Getter;
import lombok.ToString;
Expand Down Expand Up @@ -70,7 +71,7 @@ private HttpResponse<InputStream> callClientForInputStream(PartRequest pr) {

private Map<String, String> getSecurityHeaders(PartRequest pr) {
DownloadRequest dr = pr.getDownloadRequest();
Map<String, String> headers = new HashMap<>();
Map<String, String> headers = Optional.ofNullable(dr.getHeaders()).orElse(new HashMap<>());
headers.put("Authorization", "Bearer " + credentials.getSessionBearerToken());
headers.put(
"Fusion-Authorization",
Expand Down
9 changes: 6 additions & 3 deletions src/test/java/io/github/jpmorganchase/fusion/FusionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,8 @@ public void testFileDownloadInteraction() throws Exception {
config.getRootURL(), "common", "sample_dataset", "20230308", "csv"),
String.format("%s/%s_%s_%s.%s", TMP_PATH, "common", "sample_dataset", "20230308", "csv"),
"common",
"sample_dataset");
"sample_dataset",
new HashMap<>());

f.download("common", "sample_dataset", "20230308", "csv", TMP_PATH);
}
Expand All @@ -204,7 +205,8 @@ public void testFileDownloadInteractionWithDefaultPath() throws Exception {
config.getRootURL(), "common", "sample_dataset", "20230308", "csv"),
String.format("%s/%s_%s_%s.%s", "downloads", "common", "sample_dataset", "20230308", "csv"),
"common",
"sample_dataset");
"sample_dataset",
new HashMap<>());

f.download("common", "sample_dataset", "20230308", "csv");
}
Expand All @@ -218,7 +220,8 @@ public void testFileDownloadAsStreamInteraction() throws Exception {
"%scatalogs/%s/datasets/%s/datasetseries/%s/distributions/%s",
config.getRootURL(), "common", "sample_dataset", "20230308", "csv"),
"common",
"sample_dataset"))
"sample_dataset",
new HashMap<>()))
.thenReturn(new ByteArrayInputStream("A,B,C\nD,E,F".getBytes()));

InputStream response = f.downloadStream("common", "sample_dataset", "20230308", "csv");
Expand Down
Loading

0 comments on commit 0873ecb

Please sign in to comment.