From c83895ba4246fba9e8582d958c718962eb5be4f2 Mon Sep 17 00:00:00 2001 From: Robert Carosi Date: Wed, 27 Oct 2021 16:03:16 +0200 Subject: [PATCH] Update - Update auth urls - Add fetchZip method - Add constructor with deviceToken parameter - Return document ID on directory creation - Add TRUNCATE flag to sendRequest method --- .../es/jlarriba/jrmapi/Authentication.java | 10 ++++- src/main/java/es/jlarriba/jrmapi/Jrmapi.java | 37 +++++++++++++------ .../java/es/jlarriba/jrmapi/http/Net.java | 6 ++- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/src/main/java/es/jlarriba/jrmapi/Authentication.java b/src/main/java/es/jlarriba/jrmapi/Authentication.java index a09d78c..81ee009 100644 --- a/src/main/java/es/jlarriba/jrmapi/Authentication.java +++ b/src/main/java/es/jlarriba/jrmapi/Authentication.java @@ -32,8 +32,10 @@ */ public class Authentication { - private static final String DEVICE_AUTH_URL = "https://my.remarkable.com/token/json/2/device/new"; - private static final String USER_AUTH_URL = "https://my.remarkable.com/token/json/2/user/new"; + private static final String DEVICE_AUTH_URL = + "https://webapp-production-dot-remarkable-production.appspot.com/token/json/2/device/new"; + private static final String USER_AUTH_URL = + "https://webapp-production-dot-remarkable-production.appspot.com/token/json/2/user/new"; private static final File RMAPI_PROPERTIES_FILE = new File(System.getProperty("user.home"), "/.rmapi"); private static final String DEVICETOKEN_PROPERTY_NAME = "devicetoken"; @@ -65,6 +67,10 @@ public String userToken() { return net.post(USER_AUTH_URL, getDeviceToken()); } + public String userToken(String deviceToken) { + return net.post(USER_AUTH_URL, deviceToken); + } + protected String getDeviceToken() { try { Properties properties = new Properties(); diff --git a/src/main/java/es/jlarriba/jrmapi/Jrmapi.java b/src/main/java/es/jlarriba/jrmapi/Jrmapi.java index 521b30b..619cbb9 100644 --- a/src/main/java/es/jlarriba/jrmapi/Jrmapi.java +++ b/src/main/java/es/jlarriba/jrmapi/Jrmapi.java @@ -20,9 +20,6 @@ import es.jlarriba.jrmapi.commonsio.FilenameUtils; import es.jlarriba.jrmapi.http.Net; import es.jlarriba.jrmapi.model.DeleteDocument; - -import java.util.ArrayList; -import java.util.List; import es.jlarriba.jrmapi.model.Document; import es.jlarriba.jrmapi.model.MetadataDocument; import es.jlarriba.jrmapi.model.UploadDocumentRequest; @@ -30,8 +27,9 @@ import java.io.File; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; +import java.util.ArrayList; +import java.util.List; import java.util.UUID; - import net.lingala.zip4j.ZipFile; import net.lingala.zip4j.exception.ZipException; import org.apache.logging.log4j.LogManager; @@ -50,15 +48,21 @@ public class Jrmapi { private static final String UPLOAD_REQUEST = BASE_URL + "/document-storage/json/2/upload/request"; private static final String DELETE = BASE_URL + "/document-storage/json/2/delete"; - private final Gson gson; + private final Gson gson = new Gson(); private final String userToken; - private final Net net; + private final Net net = new Net(); public Jrmapi() { - Authentication auth = new Authentication(); - userToken = auth.userToken(); - net = new Net(); - gson = new Gson(); + userToken = new Authentication().userToken(); + createWorkdir(); + } + + public Jrmapi(String deviceToken) { + userToken = new Authentication().userToken(deviceToken); + createWorkdir(); + } + + private void createWorkdir() { File workdir = new File(Utils.WORKDIR); if (!workdir.exists()) { workdir.mkdir(); @@ -98,6 +102,16 @@ public void fetchDoc(Document doc, String path) { } } + public File fetchZip(Document doc, String path) { + String response = net.get(LIST_DOCS, userToken, doc.getID()); + List docs = gson.fromJson(response, new TypeToken>() { + }.getType()); + File file = new File(path + doc.getVissibleName() + ".zip"); + LOGGER.debug("Download zip to " + path + doc.getVissibleName() + ".zip"); + net.getStream(docs.get(0).getBlobURLGet(), userToken, file); + return file; + } + public void exportPdf(Document doc, String path, String filename) { String response = net.get(LIST_DOCS, userToken, doc.getID()); List docs = gson.fromJson(response, new TypeToken>(){}.getType()); @@ -115,7 +129,7 @@ public void exportPdf(Document doc, String path, String filename) { } } - public void createDir(String name, String parentID) { + public String createDir(String name, String parentID) { String id = UUID.randomUUID().toString(); UploadDocumentRequest docRequest = new UploadDocumentRequest(); docRequest.setID(id); @@ -141,6 +155,7 @@ public void createDir(String name, String parentID) { uploadMetadataDoc.add(metadataDoc); net.put(UPDATE_STATUS, userToken, uploadMetadataDoc); + return docResponse.get(0).getID(); } public void uploadDoc(File file, String parentID) { diff --git a/src/main/java/es/jlarriba/jrmapi/http/Net.java b/src/main/java/es/jlarriba/jrmapi/http/Net.java index b6923d9..791b7f1 100644 --- a/src/main/java/es/jlarriba/jrmapi/http/Net.java +++ b/src/main/java/es/jlarriba/jrmapi/http/Net.java @@ -1,5 +1,9 @@ package es.jlarriba.jrmapi.http; +import static java.nio.file.StandardOpenOption.CREATE; +import static java.nio.file.StandardOpenOption.TRUNCATE_EXISTING; +import static java.nio.file.StandardOpenOption.WRITE; + import com.google.gson.Gson; import java.io.File; import java.io.FileNotFoundException; @@ -121,7 +125,7 @@ private String sendRequest(HttpRequest request) { private void sendRequest(HttpRequest request, File file) { try { LOGGER.debug(request.uri()); - var response = client.send(request, BodyHandlers.ofFile(file.toPath())); + var response = client.send(request, BodyHandlers.ofFile(file.toPath(), CREATE, WRITE, TRUNCATE_EXISTING)); LOGGER.debug(response.statusCode()); } catch (IOException | InterruptedException e) { LOGGER.error("Error while launching request", e);