Java SDK
Installation
Die JARs können manuell über die Maven Central Search heruntergeladen oder aus dem Maven-Repository installiert werden.
Bestehende Nutzer sollten die Abschaltung von JCenter beachten und ihre Build-Dateien aktualisieren, um die neuesten Versionen zu erhalten.
Gradle:
implementation 'com.transloadit.sdk:transloadit:2.1.0'
Maven:
<dependency>
<groupId>com.transloadit.sdk</groupId>
<artifactId>transloadit</artifactId>
<version>2.1.0</version>
</dependency>
Verwendung
Alle Interaktionen mit dem SDK beginnen mit der Klasse com.transloadit.sdk.Transloadit.
Authentifizierung
Das SDK unterstützt zwei Authentifizierungsmethoden:
1. API-Schlüssel + geheimer Schlüssel
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
2. Externer Signaturanbieter
Verwenden Sie diese Methode in Client-Apps, damit keine geheimen Schlüssel mitgeliefert werden:
import com.transloadit.sdk.SignatureProvider;
SignatureProvider signatureProvider = new SignatureProvider() {
@Override
public String generateSignature(String paramsJson) throws Exception {
// Fetch a precomputed Transloadit signature from your backend.
return "sha384:...";
}
};
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", signatureProvider);
Assembly erstellen
Um eine Assembly zu erstellen, verwenden Sie die Methode newAssembly.
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.AssemblyResponse;
import java.io.File;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
Assembly assembly = transloadit.newAssembly();
Map<String, Object> stepOptions = new HashMap<>();
stepOptions.put("width", 75);
stepOptions.put("height", 75);
assembly.addStep("resize", "/image/resize", stepOptions);
assembly.addFile(new File("PATH/TO/FILE.jpg"));
// you can skip this part if you don't want to wait till the
// assembly is complete
assembly.setShouldWaitForCompletion(true);
try {
AssemblyResponse response = assembly.save();
System.out.println(response.getId());
System.out.println(response.getUrl());
System.out.println(response.json());
} catch (RequestException | LocalOperationException e) {
// handle exception here
}
}
}
Assembly abrufen
Die Methode getAssembly ruft den JSON-Status der durch die angegebene
assembly_Id identifizierten Assembly ab.
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.AssemblyResponse;
public class Main {
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
try {
AssemblyResponse response = transloadit.getAssembly("ASSEMBLY_ID");
System.out.println(response.getUrl());
System.out.println(response.json());
} catch (RequestException | LocalOperationException e) {
// handle exception here
}
}
}
Mit der Methode getAssemblyByUrl können Sie eine Assembly auch über ihre URL abrufen.
AssemblyResponse response = transloadit.getAssemblyByUrl("ASSEMBLY_URL");
Assembly abbrechen
Um eine ausgeführte Assembly abzubrechen, verwenden Sie die Methode cancelAssembly und übergeben
die Assembly ID als Parameter.
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.AssemblyResponse;
public class Main {
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
try {
AssemblyResponse response = transloadit.cancelAssembly("ASSEMBLY_ID");
System.out.println(response.isCancelled()); // prints true
} catch (RequestException | LocalOperationException e) {
// handle exception here
}
}
}
Assemblies auflisten
Die Methode listAssemblies ruft anhand der angegebenen options ein Array von Assemblies ab.
Gültige options sind page, pagesize, type, fromdate und todate. Weitere Einzelheiten
finden Sie in der Transloadit-API-Dokumentation.
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.ListResponse;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
try {
Map<String, Object> options = new HashMap<>();
options.put("pagesize", 10);
ListResponse response = transloadit.listAssemblies(options);
System.out.println(response.size()); // number of assemblies on the list.
System.out.println(response.getItems()); // returns an iterable json array
} catch (RequestException | LocalOperationException e) {
// handle exception here
}
}
}
Template erstellen
Um ein neues Template zu erstellen, verwenden Sie die Methode newTemplate und übergeben den
Namen des Templates als Parameter.
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.Response;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
Template template = transloadit.newTemplate("MY_TEMPLATE_NAME");
Map<String, Object> resizeOptions = new HashMap<>();
resizeOptions.put("width", 75);
resizeOptions.put("height", 75);
Map<String, Object> optimizeOptions = new HashMap<>();
optimizeOptions.put("use", ":original");
template.addStep("resize", "/image/resize", resizeOptions);
template.addStep("resize", "/image/optimize", optimizeOptions);
try {
Response response = template.save();
System.out.println(response.json());
System.out.println(response.json().getString("id")); // gets the Template ID.
} catch (RequestException | LocalOperationException e) {
// handle exception here
}
}
}
Template aktualisieren
Um ein Template zu aktualisieren, verwenden Sie die Methode updateTemplate und übergeben die
Template ID sowie die zu aktualisierenden Optionen als Parameter.
import com.transloadit.sdk.Steps;
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.Response;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
Steps steps = new Steps();
Map<String, Object> stepOptions = new HashMap<>();
stepOptions.put("width", 150);
stepOptions.put("height", 150);
steps.addStep("resize", "/image/resize", stepOptions);
Map<String, Object> templateOptions = new HashMap<>();
templateOptions.put("steps", steps.toMap());
templateOptions.put("name", "MY_NEW_TEMPLATE_NAME");
try {
Response response = transloadit.updateTemplate("TEMPLATE_ID", templateOptions);
} catch (RequestException | LocalOperationException e) {
// handle exception here
}
}
}
Template löschen
Um ein Template zu löschen, verwenden Sie die Methode deleteTemplate und übergeben die Template ID
als Parameter.
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.Response;
import java.util.HashMap;
public class Main {
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
try {
Response response = transloadit.deleteTemplate("TEMPLATE_ID");
} catch (RequestException | LocalOperationException e) {
// handle exception here
}
}
}
Templates auflisten
Um eine Liste aller Templates in Ihrem Konto abzurufen, verwenden Sie die Methode listTemplates.
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.ListResponse;
public class Main {
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
try {
ListResponse response = transloadit.listTemplates();
System.out.println(response.size()); // number of assemblies on the list.
System.out.println(response.getItems()); // returns an iterable json array
} catch (RequestException | LocalOperationException e) {
// handle exception here
}
}
}
Template abrufen
Um ein bestimmtes Template abzurufen, verwenden Sie die Methode getTemplate und übergeben die
Template ID als Parameter.
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.Response;
public class Main {
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
try {
AssemblyResponse response = transloadit.getTemplate("TEMPLATE_ID");
System.out.println(response.json());
} catch (RequestException | LocalOperationException e) {
// handle exception here
}
}
}
Rechnung abrufen
Um die Abrechnungsdetails Ihres Kontos für einen bestimmten Monat abzurufen, verwenden Sie die
Methode getBill und übergeben Monat und Jahr als Parameter.
import com.transloadit.sdk.Transloadit;
import com.transloadit.sdk.exceptions.LocalOperationException;
import com.transloadit.sdk.exceptions.RequestException;
import com.transloadit.sdk.response.Response;
public class Main {
public static void main(String[] args) {
Transloadit transloadit = new Transloadit("YOUR_TRANSLOADIT_KEY", "YOUR_TRANSLOADIT_SECRET");
try {
ListResponse response = transloadit.getBill(3, 2017);
System.out.println(response.json());
} catch (RequestException | LocalOperationException e) {
// handle exception here
}
}
}
Beispiel
Vollständig funktionsfähige Beispiele finden Sie unter /examples.
Dokumentation
Die vollständige API-Dokumentation finden Sie in der Javadoc.