Skip to content

Commit

Permalink
multipart upload (#20) & download (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
knighto82 authored May 31, 2023
1 parent 7c966d5 commit 31823e5
Show file tree
Hide file tree
Showing 43 changed files with 9,003,316 additions and 480 deletions.
55 changes: 42 additions & 13 deletions src/main/java/io/github/jpmorganchase/fusion/Fusion.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import io.github.jpmorganchase.fusion.api.APICallException;
import io.github.jpmorganchase.fusion.api.APIManager;
import io.github.jpmorganchase.fusion.api.ApiInputValidationException;
import io.github.jpmorganchase.fusion.api.FusionAPIManager;
import io.github.jpmorganchase.fusion.api.*;
import io.github.jpmorganchase.fusion.digest.AlgoSpecificDigestProducer;
import io.github.jpmorganchase.fusion.digest.DigestProducer;
import io.github.jpmorganchase.fusion.http.Client;
Expand Down Expand Up @@ -57,6 +54,10 @@ public class Fusion {

private APIManager api;

private APIUploader uploader;

private APIDownloader downloader;

@Builder.Default
private String defaultCatalog = DEFAULT_CATALOG;

Expand Down Expand Up @@ -399,7 +400,7 @@ public Map<String, Distribution> listDistributions(String dataset, String series
* @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.
* @throws APICallException if the call to the Fusion API fails
* @throws FusionException if the downloaded file cannot be saved to the specified target path
* @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) {
Expand All @@ -413,7 +414,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);
this.downloader.callAPIFileDownload(url, filepath, catalogName, dataset);
}

/**
Expand All @@ -424,7 +425,7 @@ public void download(String catalogName, String dataset, String seriesMember, St
* @param seriesMember a String representing the series member identifier.
* @param distribution a String representing the distribution identifier, this is the file extension.
* @throws APICallException if the call to the Fusion API fails
* @throws FusionException if the downloaded file cannot be saved to the default path
* @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) {
Expand All @@ -440,7 +441,7 @@ public void download(String catalogName, String dataset, String seriesMember, St
* @param seriesMembers a List of Strings representing the series member identifiers.
* @param distribution a String representing the distribution identifier, this is the file extension.
* @throws APICallException if the call to the Fusion API fails
* @throws FusionException if the downloaded file cannot be saved to the default path
* @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, List<String> seriesMembers, String distribution) {
Expand All @@ -457,13 +458,14 @@ public void download(String catalogName, String dataset, List<String> seriesMemb
* @param seriesMember a String representing the series member identifier.
* @param distribution a String representing the distribution identifier, this is the file extension.
* @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) {
String url = String.format(
"%scatalogs/%s/datasets/%s/datasetseries/%s/distributions/%s",
this.rootURL, catalogName, dataset, seriesMember, distribution);
return this.api.callAPIFileDownload(url);
return this.downloader.callAPIFileDownload(url, catalogName, dataset);
}

/**
Expand All @@ -479,6 +481,7 @@ public InputStream downloadStream(String catalogName, String dataset, String ser
* @param createdDate the creation date for the distribution
* @throws ApiInputValidationException if the specified file cannot be read
* @throws APICallException if the call to the Fusion API fails
* @throws FileUploadException if there is an issue handling the response from Fusion API
* @throws OAuthException if a token could not be retrieved for authentication
**/
public void upload(
Expand All @@ -497,7 +500,7 @@ public void upload(
String strFromDate = fromDate.format(dateTimeFormatter);
String strToDate = toDate.format(dateTimeFormatter);
String strCreatedDate = createdDate.format(dateTimeFormatter);
this.api.callAPIFileUpload(url, filename, catalogName, dataset, strFromDate, strToDate, strCreatedDate);
this.uploader.callAPIFileUpload(url, filename, catalogName, dataset, strFromDate, strToDate, strCreatedDate);
}

/**
Expand All @@ -511,6 +514,7 @@ public void upload(
* @param dataDate the earliest, latest, and created date are all the same.
* @throws ApiInputValidationException if the specified file cannot be read
* @throws APICallException if the call to the Fusion API fails
* @throws FileUploadException if there is an issue handling the response from Fusion API
* @throws OAuthException if a token could not be retrieved for authentication
**/
public void upload(
Expand All @@ -534,8 +538,9 @@ public void upload(
* @param fromDate the earliest date for which there is data in the distribution
* @param toDate the latest date for which there is data in the distribution
* @param createdDate the creation date for the distribution
* @throws ApiInputValidationException if the specified file cannot be read
* @throws ApiInputValidationException if the specified stream cannot be read
* @throws APICallException if the call to the Fusion API fails
* @throws FileUploadException if there is an issue handling the response from Fusion API
* @throws OAuthException if a token could not be retrieved for authentication
**/
public void upload(
Expand All @@ -554,7 +559,7 @@ public void upload(
String strFromDate = fromDate.format(dateTimeFormatter);
String strToDate = toDate.format(dateTimeFormatter);
String strCreatedDate = createdDate.format(dateTimeFormatter);
this.api.callAPIFileUpload(url, data, catalogName, dataset, strFromDate, strToDate, strCreatedDate);
this.uploader.callAPIFileUpload(url, data, catalogName, dataset, strFromDate, strToDate, strCreatedDate);
}

public static FusionBuilder builder() {
Expand All @@ -570,6 +575,8 @@ public static class FusionBuilder {
protected String credentialFile;
protected String rootURL;
protected APIManager api;
protected APIUploader uploader;
protected APIDownloader downloader;
protected Map<String, BearerToken> datasetBearerTokens = new HashMap<>();

protected DatasetTokenProvider datasetTokenProvider;
Expand Down Expand Up @@ -668,7 +675,29 @@ public Fusion build() {
AlgoSpecificDigestProducer.builder().sha256().build();

if (api == null) {
api = new FusionAPIManager(client, sessionTokenProvider, datasetTokenProvider, digestProducer);
api = FusionAPIManager.builder()
.httpClient(client)
.sessionTokenProvider(sessionTokenProvider)
.build();
}

if (uploader == null) {
uploader = FusionAPIUploader.builder()
.httpClient(client)
.sessionTokenProvider(sessionTokenProvider)
.datasetTokenProvider(datasetTokenProvider)
.digestProducer(digestProducer)
.uploadPartSize(16)
.build();
}

if (downloader == null) {
downloader = FusionAPIDownloader.builder()
.httpClient(client)
.sessionTokenProvider(sessionTokenProvider)
.datasetTokenProvider(datasetTokenProvider)
.digestProducer(digestProducer)
.build();
}

return super.build();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package io.github.jpmorganchase.fusion.api;

import io.github.jpmorganchase.fusion.exception.FusionException;

/**
* A custom exception to provide useful information on the response of an API call
*/
public class APICallException extends RuntimeException {
public class APICallException extends FusionException {

private final int responseCode;

Expand Down Expand Up @@ -42,6 +44,9 @@ public String getMessage() {
case 500:
errorMsg = "Internal API error. There was an error processing the request.";
break;
case 504:
errorMsg = "Request timed out. Please try again.";
break;
default:
errorMsg = "Unknown";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package io.github.jpmorganchase.fusion.api;

import java.io.InputStream;

public interface APIDownloader {

void callAPIFileDownload(String apiPath, String fileName, String catalog, String dataset)
throws APICallException, FileDownloadException;

InputStream callAPIFileDownload(String apiPath, String catalog, String dataset)
throws APICallException, FileDownloadException;
}
28 changes: 0 additions & 28 deletions src/main/java/io/github/jpmorganchase/fusion/api/APIManager.java
Original file line number Diff line number Diff line change
@@ -1,33 +1,5 @@
package io.github.jpmorganchase.fusion.api;

import java.io.InputStream;

public interface APIManager {
String callAPI(String apiPath) throws APICallException;

void callAPIFileDownload(String apiPath, String downloadFolder, String fileName) throws APICallException;

void callAPIFileDownload(String apiPath, String fileName) throws APICallException, FileDownloadException;

InputStream callAPIFileDownload(String apiPath) throws APICallException, FileDownloadException;

int callAPIFileUpload(
String apiPath,
String fileName,
String catalogName,
String dataset,
String fromDate,
String toDate,
String createdDate)
throws APICallException;

int callAPIFileUpload(
String apiPath,
InputStream data,
String catalogName,
String dataset,
String fromDate,
String toDate,
String createdDate)
throws APICallException;
}
26 changes: 26 additions & 0 deletions src/main/java/io/github/jpmorganchase/fusion/api/APIUploader.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package io.github.jpmorganchase.fusion.api;

import java.io.InputStream;

public interface APIUploader {

void callAPIFileUpload(
String apiPath,
String fileName,
String catalogName,
String dataset,
String fromDate,
String toDate,
String createdDate)
throws APICallException;

void callAPIFileUpload(
String apiPath,
InputStream data,
String catalogName,
String dataset,
String fromDate,
String toDate,
String createdDate)
throws APICallException;
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.jpmorganchase.fusion.api;

public class ApiInputValidationException extends RuntimeException {
import io.github.jpmorganchase.fusion.exception.FusionException;

public class ApiInputValidationException extends FusionException {

public ApiInputValidationException(String s) {
super(s);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package io.github.jpmorganchase.fusion.api;

public class FileDownloadException extends RuntimeException {
import io.github.jpmorganchase.fusion.exception.FusionException;

public class FileDownloadException extends FusionException {

public FileDownloadException(String s) {
super(s);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package io.github.jpmorganchase.fusion.api;

import io.github.jpmorganchase.fusion.exception.FusionException;

public class FileUploadException extends FusionException {

public FileUploadException(String s) {
super(s);
}

public FileUploadException(String s, Throwable throwable) {
super(s, throwable);
}
}
Loading

0 comments on commit 31823e5

Please sign in to comment.