5 Commits
v24 ... v29

Author SHA1 Message Date
Andrew Stein
e99b3d24a3 Refactored error handling, abstracted request types to avoid needing to declare HTTPMethod for each request 2023-02-25 18:21:13 -05:00
Andrew Stein
ca3962dd05 Styling 2023-02-25 02:34:23 -05:00
Andrew Stein
f4e28baadd Styling 2023-02-25 02:32:09 -05:00
Andrew Stein
8bb441a097 Styling 2023-02-25 02:17:43 -05:00
Andrew Stein
aef486a14d Workflow. I was a fool. 2023-02-25 02:11:08 -05:00
39 changed files with 479 additions and 294 deletions

View File

@@ -1,4 +1,4 @@
name: Java CI with Maven
name: Dev Build
on:
push:
@@ -32,7 +32,7 @@ jobs:
run: git tag v${{github.run_number}}
- name: Push new tag
run: git push origin v${{ steps.version.outputs.version }}
run: git push origin v${{github.run_number}} # I was a fool.
- name: Create Release
if: always()

View File

@@ -1,19 +1,13 @@
# elevenlabs-api ![example workflow](https://github.com/AndrewCPU/elevenlabs-api/actions/workflows/main.yml/badge.svg)
## An unofficial ElevenLabs AI Voice Generation Java API
*Disclaimer: I am not affiliated with ElevenLabs and am providing a programmatic way of accessing the public ElevenLabs Web API.*
# elevenlabs-api ![Build](https://github.com/AndrewCPU/elevenlabs-api/actions/workflows/build.yml/badge.svg)
### An unofficial ElevenLabs AI Voice Generation Java API
## Links to ElevenLabs
### **ElevenLabs Website**: https://elevenlabs.io
### **ElevenLabs API Documentation**: https://api.elevenlabs.io/docs
- - -
## Getting Started
So you wanna make custom voices, huh? Well you've come to the right place.
### Installation
**Maven**
To install `elevenlabs-api` with Maven, use:
To add `elevenlabs-api` to your Maven project, use:
```xml
<dependencies>
...
@@ -35,7 +29,7 @@ Compiled JARs are available via the [Releases tab](https://github.com/AndrewCPU/
*Todo*
### Setting up your API Key
To access your ElevenLabs API key, head to the [official website](https://elevenlabs.io/), you can view your xi-api-key using the 'Profile' tab on the website.
To access your ElevenLabs API key, head to the [official website](https://elevenlabs.io/), you can view your `xi-api-key` using the 'Profile' tab on the website.
To set up your ElevenLabs API key, you must register it with the ElevenLabsAPI Java API like below:
```java
ElevenLabsAPI.getInstance().setAPIKey("YOUR_API_KEY_HERE");
@@ -45,6 +39,53 @@ ElevenLabsAPI.getInstance().setAPIKey("YOUR_API_KEY_HERE");
Once you've injected your API Key, you can safely assume that you will not receive a `ElevenLabsAPINotInitiatedException`.
- - -
<!-- TOC -->
### Table of Contents
* [Getting Started](#getting-started)
* [Installation](#installation)
* [Setting up your API Key](#setting-up-your-api-key)
* [Voices](#voices)
* [Accessing your List of Available Voices](#accessing-your-list-of-available-voices)
* [Accessing the Default Voice Settings](#accessing-the-default-voice-settings)
* [Getting a Voice by ID](#getting-a-voice-by-id)
* [Deleting a voice](#deleting-a-voice)
* [Retrieving an Updated VoiceSettings for a Voice](#retrieving-an-updated-voicesettings-for-a-voice)
* [Updating the VoiceSettings for a Voice](#updating-the-voicesettings-for-a-voice)
* [Editing a Voice](#editing-a-voice)
* [Creating a Voice](#creating-a-voice)
* [Generating Audio](#generating-audio)
* [Samples](#samples)
* [Accessing Voice Samples](#accessing-voice-samples)
* [Downloading a Sample](#downloading-a-sample)
* [Deleting a Sample](#deleting-a-sample)
* [History](#history)
* [Getting Generation History](#getting-generation-history)
* [Getting a History Item](#getting-a-history-item)
* [Downloading History](#downloading-history)
* [Deleting a HistoryItem](#deleting-a-historyitem)
* [Requesting the Voice for a HistoryItem](#requesting-the-voice-for-a-historyitem)
* [Downloading a HistoryItem Audio](#downloading-a-historyitem-audio)
* [User Management](#user-management)
* [Getting your Subscription](#getting-your-subscription)
* [Getting your User](#getting-your-user)
* [Exceptions](#exceptions)
* [*ElevenLabsAPINotInitiatedException*](#elevenlabsapinotinitiatedexception)
* [*ElevenLabsValidationException*](#elevenlabsvalidationexception)
* [Built in Types](#built-in-types)
* [`Voice` Related Types](#voice-related-types)
* [`User` Related Types](#user-related-types)
* [Misc](#misc)
* [Links to ElevenLabs](#links-to-elevenlabs)
* [**ElevenLabs Website**: https://elevenlabs.io](#elevenlabs-website--httpselevenlabsio)
* [**ElevenLabs API Documentation**: https://api.elevenlabs.io/docs](#elevenlabs-api-documentation--httpsapielevenlabsiodocs)
<!-- TOC -->
## Links to ElevenLabs
### **ElevenLabs Website**: https://elevenlabs.io
### **ElevenLabs API Documentation**: https://api.elevenlabs.io/docs
- - -
## Voices
### Accessing your List of Available Voices
To retrieve your list of accessible Voices, you can statically utilize `Voice#getVoices()`. This will return both ElevenLab's pregenerated `Voice` models, as well as any personal `Voices` you have generated.
@@ -214,7 +255,6 @@ A `Subscription` contains all the relevant data to manage your API usage (charac
```java
Subscription subscription = Subscription.get();
```
- - -
### Getting your User
This endpoint will return the `User` associated with a given API key.
@@ -305,5 +345,8 @@ If you like what you see, give it a star! :)
- - -
## Special Thanks
A special thanks to ElevenLabs for making an awesome tool. Thanks folks 🥂
#### Unit Testing
*Todo*
Thanks to ElevenLabs for making an awesome tool 🥂

View File

@@ -13,6 +13,9 @@ import net.andrewcpu.elevenlabs.api.requests.samples.GetAudioSampleRequest;
import net.andrewcpu.elevenlabs.api.requests.user.GetSubscriptionInfoRequest;
import net.andrewcpu.elevenlabs.api.requests.user.GetUserRequest;
import net.andrewcpu.elevenlabs.api.requests.voices.*;
import net.andrewcpu.elevenlabs.api.requests.voices.settings.GetDefaultVoiceSettingsRequest;
import net.andrewcpu.elevenlabs.api.requests.voices.settings.GetVoiceSettingsRequest;
import net.andrewcpu.elevenlabs.api.requests.voices.settings.UpdateVoiceSettingsRequest;
import net.andrewcpu.elevenlabs.elements.user.Subscription;
import net.andrewcpu.elevenlabs.elements.user.User;
import net.andrewcpu.elevenlabs.elements.voice.History;
@@ -21,6 +24,7 @@ import net.andrewcpu.elevenlabs.elements.voice.Voice;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.enums.ContentType;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsAPINotInitiatedException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsValidationException;
import java.io.File;
@@ -38,8 +42,9 @@ import static net.andrewcpu.elevenlabs.util.MultipartUtil.addFormField;
@SuppressWarnings({"unchecked", "UnusedReturnValue"})
public class ElevenLabsAPI {
//this class needs help
private static ElevenLabsAPI instance;
private static final String baseURL = "https://api.elevenlabs.io/";
private static final String baseURL = "https://api.elevenlabs.io/v1/";
private static boolean debugMode = false;
public static boolean isDebugMode() {
@@ -57,6 +62,8 @@ public class ElevenLabsAPI {
return instance;
}
private boolean instantiated;
private String apiKey;
@@ -69,110 +76,110 @@ public class ElevenLabsAPI {
this.apiKey = apiKey;
}
public Subscription getSubscription() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public Subscription getSubscription() throws ElevenLabsException {
return (Subscription) getResult(new GetSubscriptionInfoRequest());
}
public User getUser() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public User getUser() throws ElevenLabsException {
return (User) getResult(new GetUserRequest());
}
public File getTextToSpeech(String text, Voice voice, VoiceSettings settings, File outputFile) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (File)getResult(new GetTextToSpeechRequest(voice, settings, text, outputFile));
public File getTextToSpeech(String text, Voice voice, VoiceSettings settings, File outputFile) throws ElevenLabsException {
return (File)getResult(new GenerateTextToSpeechRequest(voice, settings, text, outputFile));
}
public List<Voice> getVoices() throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public List<Voice> getVoices() throws ElevenLabsException {
return (List<Voice>) getResult(new GetVoicesRequest());
}
public Voice getVoice(String voiceId, boolean withSettings) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public Voice getVoice(String voiceId, boolean withSettings) throws ElevenLabsException {
return (Voice)getResult(new GetVoiceRequest(voiceId, withSettings));
}
public Voice getVoice(String voiceId) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public Voice getVoice(String voiceId) throws ElevenLabsException {
return getVoice(voiceId, true);
}
public VoiceSettings getVoiceSettings(String voiceId) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public VoiceSettings getVoiceSettings(String voiceId) throws ElevenLabsException {
return (VoiceSettings) getResult(new GetVoiceSettingsRequest(voiceId));
}
public VoiceSettings getVoiceSettings(Voice voice) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public VoiceSettings getVoiceSettings(Voice voice) throws ElevenLabsException {
return getVoiceSettings(voice.getVoiceId());
}
public String deleteVoice(Voice voice) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String deleteVoice(Voice voice) throws ElevenLabsException {
return deleteVoice(voice.getVoiceId());
}
public String deleteVoice(String voiceId) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String deleteVoice(String voiceId) throws ElevenLabsException {
return (String)getResult(new DeleteVoiceRequest(voiceId));
}
public String editVoice(Voice voice, VoiceSettings voiceSettings) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String editVoice(Voice voice, VoiceSettings voiceSettings) throws ElevenLabsException {
return editVoice(voice.getVoiceId(), voiceSettings);
}
public String editVoice(String voiceId, VoiceSettings settings) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String editVoice(String voiceId, VoiceSettings settings) throws ElevenLabsException {
return (String)getResult(new UpdateVoiceSettingsRequest(voiceId, settings));
}
public String deleteSample(String voiceId, String sampleId) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String deleteSample(String voiceId, String sampleId) throws ElevenLabsException {
return (String)getResult(new DeleteSampleRequest(voiceId, sampleId));
}
public String deleteSample(Voice voice, String sampleId) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String deleteSample(Voice voice, String sampleId) throws ElevenLabsException {
return deleteSample(voice.getVoiceId(), sampleId);
}
public String deleteSample(String voiceId, Sample sample) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String deleteSample(String voiceId, Sample sample) throws ElevenLabsException {
return deleteSample(voiceId, sample.getSampleId());
}
public String deleteSample(Voice voice, Sample sample) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String deleteSample(Voice voice, Sample sample) throws ElevenLabsException {
return deleteSample(voice.getVoiceId(), sample.getSampleId());
}
public File getSampleAudio(String voiceId, String sampleId, File file) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public File getSampleAudio(String voiceId, String sampleId, File file) throws ElevenLabsException {
return (File)getResult(new GetAudioSampleRequest(voiceId, sampleId, file));
}
public File getSampleAudio(Voice voice, Sample sample, File file) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public File getSampleAudio(Voice voice, Sample sample, File file) throws ElevenLabsException {
return getSampleAudio(voice.getVoiceId(), sample.getSampleId(), file);
}
public History getHistory() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public History getHistory() throws ElevenLabsException {
return (History)getResult(new GetHistoryRequest());
}
public File downloadHistory(List<String> historyIds, File outputFile) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public File downloadHistory(List<String> historyIds, File outputFile) throws ElevenLabsException {
return (File)getResult(new DownloadHistoryRequest(historyIds, outputFile));
}
public File getHistoryItemAudio(History.HistoryItem historyItem, File outputFile) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public File getHistoryItemAudio(History.HistoryItem historyItem, File outputFile) throws ElevenLabsException {
return (File)getResult(new GetHistoryAudioRequest(historyItem.getHistoryItemId(), outputFile));
}
public String deleteHistoryItem(History.HistoryItem historyItem) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String deleteHistoryItem(History.HistoryItem historyItem) throws ElevenLabsException {
return (String)getResult(new DeleteHistoryItemRequest(historyItem.getHistoryItemId()));
}
public VoiceSettings getDefaultVoiceSettings() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public VoiceSettings getDefaultVoiceSettings() throws ElevenLabsException {
return (VoiceSettings)getResult(new GetDefaultVoiceSettingsRequest());
}
public String createVoice(String name, Map<String, String> labels, List<File> files) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public String createVoice(String name, Map<String, String> labels, List<File> files) throws ElevenLabsException {
CreateVoiceRequest request = new CreateVoiceRequest(name, files, labels);
return (String)(getResult(request));
}
@SuppressWarnings("UnusedReturnValue")
public String editVoice(String voiceId, String name, Map<String, String> labels, List<File> files) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public String editVoice(String voiceId, String name, Map<String, String> labels, List<File> files) throws ElevenLabsException {
EditVoiceRequest editVoiceRequest = new EditVoiceRequest(voiceId,name,files,labels);
return (String)(getResult(editVoiceRequest));
}
private void checkOrThrow(ElevenLabsResponse<?> response) throws ElevenLabsValidationException {
private void checkOrThrow(ElevenLabsResponse<?> response) throws ElevenLabsException {
if(response == null){
throw new ElevenLabsValidationException("An error has occurred.");
}
@@ -181,43 +188,45 @@ public class ElevenLabsAPI {
}
}
private Object getResult(ElevenLabsRequest<?> request) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
private Object getResult(ElevenLabsRequest<?> request) throws ElevenLabsException {
ElevenLabsResponse<?> response = sendRequest(request);
checkOrThrow(response);
return response.getResult();
}
private ElevenLabsResponse<?> sendRequest(ElevenLabsRequest<?> request) throws IOException, ElevenLabsAPINotInitiatedException {
private ElevenLabsResponse<?> sendRequest(ElevenLabsRequest<?> request) throws ElevenLabsAPINotInitiatedException {
if(!instantiated){
throw new ElevenLabsAPINotInitiatedException();
}
String formattedEndpoint = request.getFormattedEndpoint();
String boundary = "---------------------------" + System.currentTimeMillis();
URL url = new URL(baseURL + formattedEndpoint);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
setupConnection(request, boundary, connection);
if(request.getContentType() == ContentType.JSON){
handleJSON(request, connection);
}
else if(request.getContentType() == ContentType.MULTIPART){
handleMultipart(request, connection, boundary);
}
URL url = null;
int responseCode = 999;
try {
responseCode = connection.getResponseCode();
} catch (IOException e) {
e.printStackTrace();
}
InputStream successStream = null,
errorStream = null;
if (responseCode >= 200 && responseCode < 300) {
successStream = connection.getInputStream();
} else {
errorStream = connection.getErrorStream();
errorStream = null;
try {
url = new URL(baseURL + formattedEndpoint);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
setupConnection(request, boundary, connection);
if(request.getContentType() == ContentType.JSON){
handleJSON(request, connection);
}
else if(request.getContentType() == ContentType.MULTIPART){
handleMultipart(request, connection, boundary);
}
responseCode = connection.getResponseCode();
if (responseCode >= 200 && responseCode < 300) {
successStream = connection.getInputStream();
} else {
errorStream = connection.getErrorStream();
}
} catch (IOException e) {
throw new RuntimeException(e);
}
return new ElevenLabsResponse<>(responseCode, errorStream, successStream, request);
}

View File

@@ -11,13 +11,13 @@ import java.io.File;
import java.util.List;
public abstract class ElevenLabsRequest<T> {
protected List<String> parameters;
protected ContentType contentType = ContentType.JSON;
protected ResponseType responseType = ResponseType.JSON;
protected final HTTPMethod method;
protected List<String> parameters;
protected ResultTransformer<T> resultTransformer;
protected JSONObject body;
protected ResponseType responseType = ResponseType.JSON;
protected File outputFilePath;
protected ContentType contentType = ContentType.JSON;
protected MultipartForm multipartForm;
public ElevenLabsRequest(List<String> parameters, JSONObject body, HTTPMethod method, ResultTransformer<T> resultTransformer) {
@@ -52,6 +52,12 @@ public abstract class ElevenLabsRequest<T> {
this.resultTransformer = resultTransformer;
}
public ElevenLabsRequest(JSONObject body, HTTPMethod method, ResultTransformer<T> resultTransformer) {
this.resultTransformer = resultTransformer;
this.method = method;
this.body = body;
}
public abstract String getEndpoint();
@@ -82,8 +88,10 @@ public abstract class ElevenLabsRequest<T> {
if(endpoint.startsWith("/")){
endpoint = endpoint.substring(1);
}
if(parameters != null && parameters.size() > 0){
return endpoint.formatted(parameters.toArray());
if(parameters != null && !parameters.isEmpty()){
for(int i = 0; i<parameters.size(); i++){
endpoint = endpoint.replaceFirst("\\{.*?}", parameters.get(i));
}
}
return endpoint;
}

View File

@@ -0,0 +1,39 @@
package net.andrewcpu.elevenlabs.api.requests;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformer;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import org.json.simple.JSONObject;
import java.util.List;
public abstract class ElevenLabsDeleteRequest<T> extends ElevenLabsRequest<T> {
public ElevenLabsDeleteRequest(List<String> parameters, JSONObject body, ResultTransformer<T> resultTransformer) {
super(parameters, body, HTTPMethod.DELETE, resultTransformer);
}
public ElevenLabsDeleteRequest(JSONObject body, ResultTransformer<T> resultTransformer) {
super(body, HTTPMethod.DELETE, resultTransformer);
}
public ElevenLabsDeleteRequest(JSONObject body) {
super(body, HTTPMethod.DELETE);
}
public ElevenLabsDeleteRequest(List<String> parameters) {
super(parameters, HTTPMethod.DELETE);
}
public ElevenLabsDeleteRequest(List<String> parameters, ResultTransformer<T> resultTransformer) {
super(parameters, HTTPMethod.DELETE, resultTransformer);
}
public ElevenLabsDeleteRequest() {
super(HTTPMethod.DELETE);
}
public ElevenLabsDeleteRequest(ResultTransformer<T> resultTransformer) {
super(HTTPMethod.DELETE, resultTransformer);
}
}

View File

@@ -0,0 +1,37 @@
package net.andrewcpu.elevenlabs.api.requests;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformer;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import org.json.simple.JSONObject;
import java.util.List;
public abstract class ElevenLabsGetRequest<T> extends ElevenLabsRequest<T> {
public ElevenLabsGetRequest(List<String> parameters, JSONObject body, ResultTransformer<T> resultTransformer) {
super(parameters, body, HTTPMethod.GET, resultTransformer);
}
public ElevenLabsGetRequest(JSONObject body, ResultTransformer<T> resultTransformer) {
super(body, HTTPMethod.GET, resultTransformer);
}
public ElevenLabsGetRequest(JSONObject body) {
super(body, HTTPMethod.GET);
}
public ElevenLabsGetRequest(List<String> parameters) {
super(parameters, HTTPMethod.GET);
}
public ElevenLabsGetRequest(List<String> parameters, ResultTransformer<T> resultTransformer) {
super(parameters, HTTPMethod.GET, resultTransformer);
}
public ElevenLabsGetRequest() {
super(HTTPMethod.GET);
}
public ElevenLabsGetRequest( ResultTransformer<T> resultTransformer) {
super(HTTPMethod.GET, resultTransformer);
}
}

View File

@@ -0,0 +1,38 @@
package net.andrewcpu.elevenlabs.api.requests;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformer;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import org.json.simple.JSONObject;
import java.util.List;
public abstract class ElevenLabsPostRequest<T> extends ElevenLabsRequest<T> {
public ElevenLabsPostRequest(List<String> parameters, JSONObject body, ResultTransformer<T> resultTransformer) {
super(parameters, body, HTTPMethod.POST, resultTransformer);
}
public ElevenLabsPostRequest(JSONObject body) {
super(body, HTTPMethod.POST);
}
public ElevenLabsPostRequest(JSONObject body, ResultTransformer<T> resultTransformer) {
super(body, HTTPMethod.POST, resultTransformer);
}
public ElevenLabsPostRequest(List<String> parameters) {
super(parameters, HTTPMethod.POST);
}
public ElevenLabsPostRequest(List<String> parameters, ResultTransformer<T> resultTransformer) {
super(parameters, HTTPMethod.POST, resultTransformer);
}
public ElevenLabsPostRequest() {
super(HTTPMethod.POST);
}
public ElevenLabsPostRequest(ResultTransformer<T> resultTransformer) {
super(HTTPMethod.POST, resultTransformer);
}
}

View File

@@ -1,20 +1,19 @@
package net.andrewcpu.elevenlabs.api.requests.history;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.StringPingPongTransformer;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsDeleteRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.enums.ResponseType;
import java.util.List;
public class DeleteHistoryItemRequest extends ElevenLabsRequest<String> {
public class DeleteHistoryItemRequest extends ElevenLabsDeleteRequest<String> {
public DeleteHistoryItemRequest(String historyId) {
super(List.of(historyId), null, HTTPMethod.DELETE, new StringPingPongTransformer());
super(List.of(historyId), RequestTransformer.STRING_RESULT_TRANSFORMER);
responseType = (ResponseType.STRING);
}
@Override
public String getEndpoint() {
return "v1/history/%s";
return "history/{historyItemId}";
}
}

View File

@@ -1,8 +1,7 @@
package net.andrewcpu.elevenlabs.api.requests.history;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsPostRequest;
import net.andrewcpu.elevenlabs.api.transformers.FilePingPongTransformer;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import net.andrewcpu.elevenlabs.enums.ResponseType;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -11,7 +10,7 @@ import java.io.File;
import java.util.List;
@SuppressWarnings("unchecked")
public class DownloadHistoryRequest extends ElevenLabsRequest<File> {
public class DownloadHistoryRequest extends ElevenLabsPostRequest<File> {
private static JSONObject getBody(List<String> historyIds) {
JSONObject object = new JSONObject();
JSONArray array = new JSONArray();
@@ -20,13 +19,13 @@ public class DownloadHistoryRequest extends ElevenLabsRequest<File> {
return object;
}
public DownloadHistoryRequest(List<String> historyIds, File outputFile) {
super(null, getBody(historyIds), HTTPMethod.POST, new FilePingPongTransformer(outputFile));
super(null, getBody(historyIds), new FilePingPongTransformer(outputFile));
responseType = (ResponseType.FILE_STREAM);
outputFilePath = (outputFile);
}
@Override
public String getEndpoint() {
return "v1/history/download";
return "history/download";
}
}

View File

@@ -1,22 +1,21 @@
package net.andrewcpu.elevenlabs.api.requests.history;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.transformers.FilePingPongTransformer;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import net.andrewcpu.elevenlabs.enums.ResponseType;
import java.io.File;
import java.util.List;
public class GetHistoryAudioRequest extends ElevenLabsRequest<File> {
public class GetHistoryAudioRequest extends ElevenLabsGetRequest<File> {
public GetHistoryAudioRequest(String historyId, File outputFile) {
super(List.of(historyId), null, HTTPMethod.GET, new FilePingPongTransformer(outputFile));
super(List.of(historyId), new FilePingPongTransformer(outputFile));
responseType = (ResponseType.FILE_STREAM);
outputFilePath = (outputFile);
}
@Override
public String getEndpoint() {
return "v1/history/%s/audio";
return "history/{historyItemId}/audio";
}
}

View File

@@ -1,17 +1,16 @@
package net.andrewcpu.elevenlabs.api.requests.history;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.obj.HistoryTransformer;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.elements.voice.History;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
public class GetHistoryRequest extends ElevenLabsRequest<History> {
public class GetHistoryRequest extends ElevenLabsGetRequest<History> {
public GetHistoryRequest() {
super(HTTPMethod.GET, new HistoryTransformer());
super(RequestTransformer.HISTORY_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "v1/history";
return "history";
}
}

View File

@@ -1,20 +1,19 @@
package net.andrewcpu.elevenlabs.api.requests.samples;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.StringPingPongTransformer;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsDeleteRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.enums.ResponseType;
import java.util.List;
public class DeleteSampleRequest extends ElevenLabsRequest<String> {
public class DeleteSampleRequest extends ElevenLabsDeleteRequest<String> {
public DeleteSampleRequest(String voiceId, String sampleId) {
super(List.of(voiceId, sampleId), null, HTTPMethod.DELETE, new StringPingPongTransformer());
super(List.of(voiceId, sampleId), RequestTransformer.STRING_RESULT_TRANSFORMER);
responseType = (ResponseType.STRING);
}
@Override
public String getEndpoint() {
return "v1/voices/%s/samples/%s";
return "voices/{voiceId}/samples/{sampleId}";
}
}

View File

@@ -1,22 +1,21 @@
package net.andrewcpu.elevenlabs.api.requests.samples;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.transformers.FilePingPongTransformer;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import net.andrewcpu.elevenlabs.enums.ResponseType;
import java.io.File;
import java.util.List;
public class GetAudioSampleRequest extends ElevenLabsRequest<File> {
public class GetAudioSampleRequest extends ElevenLabsGetRequest<File> {
public GetAudioSampleRequest(String voiceId, String sampleId, File outputFile) {
super(List.of(voiceId, sampleId), null, HTTPMethod.GET, new FilePingPongTransformer(outputFile));
super(List.of(voiceId, sampleId), new FilePingPongTransformer(outputFile));
responseType = (ResponseType.FILE_STREAM);
outputFilePath = (outputFile);
}
@Override
public String getEndpoint() {
return "v1/voices/%s/samples/%s/audio";
return "voices/{voiceId}/samples/{sampleId}/audio";
}
}

View File

@@ -1,17 +1,16 @@
package net.andrewcpu.elevenlabs.api.requests.user;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.obj.SubscriptionTransformer;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.elements.user.Subscription;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
public class GetSubscriptionInfoRequest extends ElevenLabsRequest<Subscription> {
public class GetSubscriptionInfoRequest extends ElevenLabsGetRequest<Subscription> {
public GetSubscriptionInfoRequest() {
super(HTTPMethod.GET, new SubscriptionTransformer());
super(RequestTransformer.SUBSCRIPTION_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "v1/user/subscription";
return "user/subscription";
}
}

View File

@@ -1,17 +1,16 @@
package net.andrewcpu.elevenlabs.api.requests.user;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.obj.UserTransformer;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.elements.user.User;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
public class GetUserRequest extends ElevenLabsRequest<User> {
public class GetUserRequest extends ElevenLabsGetRequest<User> {
public GetUserRequest() {
super(HTTPMethod.GET, new UserTransformer());
super(RequestTransformer.USER_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "v1/user";
return "user";
}
}

View File

@@ -1,13 +1,13 @@
package net.andrewcpu.elevenlabs.api.requests.voices;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.multipart.MultipartFile;
import net.andrewcpu.elevenlabs.api.multipart.MultipartForm;
import net.andrewcpu.elevenlabs.api.multipart.MultipartFormContent;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsPostRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformer;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.enums.ContentType;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import org.json.simple.JSONObject;
import java.io.File;
@@ -17,22 +17,21 @@ import java.util.List;
import java.util.Map;
@SuppressWarnings("unchecked")
public class CreateVoiceRequest extends ElevenLabsRequest<String> {
private static final ResultTransformer<String> transformer = new ResultTransformerAdapter<>(){
@Override
public String transform(JSONObject object) {
return object.get("voice_id").toString();
}
};
public class CreateVoiceRequest extends ElevenLabsPostRequest<String> {
public CreateVoiceRequest(String name, List<File> files, Map<String, String> labels) {
super(HTTPMethod.POST, transformer);
super(RequestTransformer.VOICE_ID_TRANSFORMER);
buildBody(name, files, labels);
}
public CreateVoiceRequest(List<String> parameters, String name, List<File> files, Map<String, String> labels) {
super(parameters, HTTPMethod.POST, transformer);
super(parameters, RequestTransformer.VOICE_ID_TRANSFORMER);
buildBody(name, files, labels);
}
@Override
public String getEndpoint() {
return "voices/add";
}
private void buildBody(String name, List<File> files, Map<String, String> labels){
this.contentType = ContentType.MULTIPART;
this.multipartForm = new MultipartForm();
@@ -52,8 +51,5 @@ public class CreateVoiceRequest extends ElevenLabsRequest<String> {
this.multipartForm.push(new MultipartFile("files", file));
}
}
@Override
public String getEndpoint() {
return "v1/voices/add";
}
}

View File

@@ -1,18 +1,17 @@
package net.andrewcpu.elevenlabs.api.requests.voices;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.StringPingPongTransformer;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsDeleteRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import java.util.List;
public class DeleteVoiceRequest extends ElevenLabsRequest<String> {
public class DeleteVoiceRequest extends ElevenLabsDeleteRequest<String> {
public DeleteVoiceRequest(String voiceId) {
super(List.of(voiceId), HTTPMethod.DELETE, new StringPingPongTransformer());
super(List.of(voiceId), RequestTransformer.STRING_RESULT_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "/v1/voices/%s";
return "voices/{voiceId}";
}
}

View File

@@ -11,6 +11,6 @@ public class EditVoiceRequest extends CreateVoiceRequest{
@Override
public String getEndpoint() {
return "v1/voices/%s/edit";
return "voices/{voiceId}/edit";
}
}

View File

@@ -12,7 +12,7 @@ import java.io.File;
import java.util.List;
@SuppressWarnings("unchecked")
public class GetTextToSpeechRequest extends ElevenLabsRequest<File> {
public class GenerateTextToSpeechRequest extends ElevenLabsRequest<File> {
private static JSONObject buildBody(VoiceSettings settings, String text){
JSONObject object = new JSONObject();
@@ -20,7 +20,7 @@ public class GetTextToSpeechRequest extends ElevenLabsRequest<File> {
object.put("voice_settings", settings.toJSON());
return object;
}
public GetTextToSpeechRequest(Voice voice, VoiceSettings settings, String text, File outputFile){
public GenerateTextToSpeechRequest(Voice voice, VoiceSettings settings, String text, File outputFile){
super(List.of(voice.getVoiceId()), buildBody(settings, text), HTTPMethod.POST, new FilePingPongTransformer(outputFile));
responseType = (ResponseType.FILE_STREAM);
outputFilePath = (outputFile);
@@ -28,6 +28,6 @@ public class GetTextToSpeechRequest extends ElevenLabsRequest<File> {
@Override
public String getEndpoint() {
return "v1/text-to-speech/%s";
return "text-to-speech/{voiceId}";
}
}

View File

@@ -1,17 +0,0 @@
package net.andrewcpu.elevenlabs.api.requests.voices;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.obj.VoiceSettingTransformer;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
public class GetDefaultVoiceSettingsRequest extends ElevenLabsRequest<VoiceSettings> {
public GetDefaultVoiceSettingsRequest() {
super(HTTPMethod.GET, new VoiceSettingTransformer());
}
@Override
public String getEndpoint() {
return "v1/voices/settings/default";
}
}

View File

@@ -1,19 +1,18 @@
package net.andrewcpu.elevenlabs.api.requests.voices;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.obj.VoiceTransformer;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.elements.voice.Voice;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import java.util.List;
public class GetVoiceRequest extends ElevenLabsRequest<Voice> {
public class GetVoiceRequest extends ElevenLabsGetRequest<Voice> {
public GetVoiceRequest(String voiceId, boolean withSettings) {
super(List.of(voiceId, String.valueOf(withSettings)), HTTPMethod.GET, new VoiceTransformer());
super(List.of(voiceId, String.valueOf(withSettings)), RequestTransformer.VOICE_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "v1/voices/%s?with_settings=%s";
return "voices/{voiceId}?with_settings={withSettings}";
}
}

View File

@@ -1,19 +0,0 @@
package net.andrewcpu.elevenlabs.api.requests.voices;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.obj.VoiceSettingTransformer;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import java.util.List;
public class GetVoiceSettingsRequest extends ElevenLabsRequest<VoiceSettings> {
public GetVoiceSettingsRequest(String voiceId) {
super(List.of(voiceId), HTTPMethod.GET, new VoiceSettingTransformer());
}
@Override
public String getEndpoint() {
return "v1/voices/%s/settings";
}
}

View File

@@ -1,19 +1,18 @@
package net.andrewcpu.elevenlabs.api.requests.voices;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.obj.VoiceListTransformer;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.elements.voice.Voice;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import java.util.List;
public class GetVoicesRequest extends ElevenLabsRequest<List<Voice>> {
public class GetVoicesRequest extends ElevenLabsGetRequest<List<Voice>> {
public GetVoicesRequest() {
super(HTTPMethod.GET, new VoiceListTransformer());
super(RequestTransformer.VOICE_LIST_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "v1/voices";
return "voices";
}
}

View File

@@ -1,20 +0,0 @@
package net.andrewcpu.elevenlabs.api.requests.voices;
import net.andrewcpu.elevenlabs.api.transformers.StringPingPongTransformer;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import java.util.List;
public class UpdateVoiceSettingsRequest extends ElevenLabsRequest<String> {
public UpdateVoiceSettingsRequest(String voiceId, VoiceSettings settings){
super(List.of(voiceId), settings.toJSON(), HTTPMethod.POST, new StringPingPongTransformer());
}
@Override
public String getEndpoint() {
return "v1/voices/%s/settings/edit";
}
}

View File

@@ -0,0 +1,16 @@
package net.andrewcpu.elevenlabs.api.requests.voices.settings;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
public class GetDefaultVoiceSettingsRequest extends ElevenLabsGetRequest<VoiceSettings> {
public GetDefaultVoiceSettingsRequest() {
super(RequestTransformer.VOICE_SETTINGS_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "voices/settings/default";
}
}

View File

@@ -0,0 +1,18 @@
package net.andrewcpu.elevenlabs.api.requests.voices.settings;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import java.util.List;
public class GetVoiceSettingsRequest extends ElevenLabsGetRequest<VoiceSettings> {
public GetVoiceSettingsRequest(String voiceId) {
super(List.of(voiceId), RequestTransformer.VOICE_SETTINGS_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "voices/{voiceId}/settings";
}
}

View File

@@ -0,0 +1,19 @@
package net.andrewcpu.elevenlabs.api.requests.voices.settings;
import net.andrewcpu.elevenlabs.api.requests.ElevenLabsPostRequest;
import net.andrewcpu.elevenlabs.api.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import java.util.List;
public class UpdateVoiceSettingsRequest extends ElevenLabsPostRequest<String> {
public UpdateVoiceSettingsRequest(String voiceId, VoiceSettings settings){
super(List.of(voiceId), settings.toJSON(), RequestTransformer.STRING_RESULT_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "voices/{voiceId}/settings/edit";
}
}

View File

@@ -0,0 +1,21 @@
package net.andrewcpu.elevenlabs.api.transformers;
import net.andrewcpu.elevenlabs.api.transformers.obj.*;
import net.andrewcpu.elevenlabs.elements.user.Subscription;
import net.andrewcpu.elevenlabs.elements.user.User;
import net.andrewcpu.elevenlabs.elements.voice.History;
import net.andrewcpu.elevenlabs.elements.voice.Voice;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import java.util.List;
public class RequestTransformer {
public static final ResultTransformer<Voice> VOICE_TRANSFORMER = new VoiceTransformer();
public static final ResultTransformer<VoiceSettings> VOICE_SETTINGS_TRANSFORMER = new VoiceSettingTransformer();
public static final ResultTransformer<History> HISTORY_TRANSFORMER = new HistoryTransformer();
public static final ResultTransformer<User> USER_TRANSFORMER = new UserTransformer();
public static final ResultTransformer<Subscription> SUBSCRIPTION_TRANSFORMER = new SubscriptionTransformer();
public static final ResultTransformer<List<Voice>> VOICE_LIST_TRANSFORMER = new VoiceListTransformer();
public static final ResultTransformer<String> STRING_RESULT_TRANSFORMER = new StringPingPongTransformer();
public static final ResultTransformer<String> VOICE_ID_TRANSFORMER = new VoiceIDTransformer();
}

View File

@@ -0,0 +1,11 @@
package net.andrewcpu.elevenlabs.api.transformers.obj;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformerAdapter;
import org.json.simple.JSONObject;
public class VoiceIDTransformer extends ResultTransformerAdapter<String> {
@Override
public String transform(JSONObject object) {
return object.get("voice_id").toString();
}
}

View File

@@ -2,11 +2,10 @@ package net.andrewcpu.elevenlabs.elements;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.elements.voice.Voice;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsAPINotInitiatedException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsValidationException;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -65,12 +64,12 @@ public class VoiceBuilder {
return this;
}
public Voice edit() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public Voice edit() throws ElevenLabsException {
ElevenLabsAPI.getInstance().editVoice(voiceId, name, labels, files);
return ElevenLabsAPI.getInstance().getVoice(voiceId, true);
}
public Voice create() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public Voice create() throws ElevenLabsException {
if(files.isEmpty()){
throw new ElevenLabsValidationException("Cannot build a voice without any files.");
}

View File

@@ -2,12 +2,10 @@ package net.andrewcpu.elevenlabs.elements.user;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.enums.Status;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsAPINotInitiatedException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsValidationException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
@@ -26,11 +24,50 @@ public class Subscription {
private final Status status;
private final NextInvoice nextInvoice;
public static Subscription get() throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public static Subscription fromJSON(JSONObject object) {
String tier = (String) object.get("tier");
int characterCount = ((Long) object.get("character_count")).intValue();
int characterLimit = ((Long) object.get("character_limit")).intValue();
boolean canExtendCharacterLimit = (Boolean) object.get("can_extend_character_limit");
boolean allowedToExtendCharacterLimit = (Boolean) object.get("allowed_to_extend_character_limit");
long nextCharacterCountResetUnix = (Long) object.get("next_character_count_reset_unix");
int voiceLimit = ((Long) object.get("voice_limit")).intValue();
boolean canExtendVoiceLimit = (Boolean) object.get("can_extend_voice_limit");
boolean canUseInstantVoiceCloning = (Boolean) object.get("can_use_instant_voice_cloning");
List<AvailableModel> availableModels = new ArrayList<>();
JSONArray availableModelsJson = (JSONArray) object.get("available_models");
for (Object model : availableModelsJson) {
JSONObject modelJson = (JSONObject) model;
String modelId = (String) modelJson.get("model_id");
String displayName = (String) modelJson.get("display_name");
List<SupportedLanguage> supportedLanguages = new ArrayList<>();
JSONArray supportedLanguagesJson = (JSONArray) modelJson.get("supported_languages");
for (Object language : supportedLanguagesJson) {
JSONObject languageJson = (JSONObject) language;
String isoCode = (String) languageJson.get("iso_code");
String languageDisplayName = (String) languageJson.get("display_name");
supportedLanguages.add(new SupportedLanguage(isoCode, languageDisplayName));
}
availableModels.add(new AvailableModel(modelId, displayName, supportedLanguages));
}
String status = (String) object.get("status");
NextInvoice invoice = null;
if(object.containsKey("next_invoice")){
JSONObject nextInvoiceJson = (JSONObject) object.get("next_invoice");
invoice = new NextInvoice(
((Long) nextInvoiceJson.get("amount_due_cents")).intValue(),
(Long) nextInvoiceJson.get("next_payment_attempt_unix")
);
}
return new Subscription(tier, characterCount, characterLimit, canExtendCharacterLimit, allowedToExtendCharacterLimit, nextCharacterCountResetUnix, voiceLimit, canExtendVoiceLimit, canUseInstantVoiceCloning, availableModels, status, invoice);
}
public static Subscription get() throws ElevenLabsException {
return ElevenLabsAPI.getInstance().getSubscription();
}
public Subscription(String tier, int characterCount, int characterLimit, boolean canExtendCharacterLimit, boolean allowedToExtendCharacterLimit,
private Subscription(String tier, int characterCount, int characterLimit, boolean canExtendCharacterLimit, boolean allowedToExtendCharacterLimit,
long nextCharacterCountResetUnix, int voiceLimit, boolean canExtendVoiceLimit, boolean canUseInstantVoiceCloning, List<AvailableModel> availableModels,
String status, NextInvoice nextInvoice) {
this.tier = tier;
@@ -95,44 +132,6 @@ public class Subscription {
return nextInvoice;
}
public static Subscription fromJSON(JSONObject object) {
String tier = (String) object.get("tier");
int characterCount = ((Long) object.get("character_count")).intValue();
int characterLimit = ((Long) object.get("character_limit")).intValue();
boolean canExtendCharacterLimit = (Boolean) object.get("can_extend_character_limit");
boolean allowedToExtendCharacterLimit = (Boolean) object.get("allowed_to_extend_character_limit");
long nextCharacterCountResetUnix = (Long) object.get("next_character_count_reset_unix");
int voiceLimit = ((Long) object.get("voice_limit")).intValue();
boolean canExtendVoiceLimit = (Boolean) object.get("can_extend_voice_limit");
boolean canUseInstantVoiceCloning = (Boolean) object.get("can_use_instant_voice_cloning");
List<AvailableModel> availableModels = new ArrayList<>();
JSONArray availableModelsJson = (JSONArray) object.get("available_models");
for (Object model : availableModelsJson) {
JSONObject modelJson = (JSONObject) model;
String modelId = (String) modelJson.get("model_id");
String displayName = (String) modelJson.get("display_name");
List<SupportedLanguage> supportedLanguages = new ArrayList<>();
JSONArray supportedLanguagesJson = (JSONArray) modelJson.get("supported_languages");
for (Object language : supportedLanguagesJson) {
JSONObject languageJson = (JSONObject) language;
String isoCode = (String) languageJson.get("iso_code");
String languageDisplayName = (String) languageJson.get("display_name");
supportedLanguages.add(new SupportedLanguage(isoCode, languageDisplayName));
}
availableModels.add(new AvailableModel(modelId, displayName, supportedLanguages));
}
String status = (String) object.get("status");
NextInvoice invoice = null;
if(object.containsKey("next_invoice")){
JSONObject nextInvoiceJson = (JSONObject) object.get("next_invoice");
invoice = new NextInvoice(
((Long) nextInvoiceJson.get("amount_due_cents")).intValue(),
(Long) nextInvoiceJson.get("next_payment_attempt_unix")
);
}
return new Subscription(tier, characterCount, characterLimit, canExtendCharacterLimit, allowedToExtendCharacterLimit, nextCharacterCountResetUnix, voiceLimit, canExtendVoiceLimit, canUseInstantVoiceCloning, availableModels, status, invoice);
}
@Override
public String toString() {

View File

@@ -2,8 +2,7 @@ package net.andrewcpu.elevenlabs.elements.user;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.elements.voice.History;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsAPINotInitiatedException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsValidationException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import org.json.simple.JSONObject;
import java.io.IOException;
@@ -13,7 +12,7 @@ public class User {
private final boolean isNewUser;
private final String apiKey;
public static User get() throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public static User get() throws ElevenLabsException, IOException {
return ElevenLabsAPI.getInstance().getUser();
}
@@ -35,7 +34,7 @@ public class User {
return apiKey;
}
public History getHistory() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public History getHistory() throws IOException, ElevenLabsException {
return ElevenLabsAPI.getInstance().getHistory();
}
public static User fromJSON(JSONObject object) {

View File

@@ -2,13 +2,11 @@ package net.andrewcpu.elevenlabs.elements.voice;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.enums.State;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsAPINotInitiatedException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsValidationException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Date;
@@ -38,7 +36,7 @@ public record History(List<HistoryItem> history) {
return history;
}
public static History get() throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public static History get() throws ElevenLabsException {
return ElevenLabsAPI.getInstance().getHistory();
}
@@ -52,11 +50,11 @@ public record History(List<HistoryItem> history) {
return null;
}
public File downloadHistory(String[] ids, File file) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public File downloadHistory(String[] ids, File file) throws ElevenLabsException {
return ElevenLabsAPI.getInstance().downloadHistory(Arrays.stream(ids).toList(), file);
}
public File downloadHistory(List<HistoryItem> historyItems, File file) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public File downloadHistory(List<HistoryItem> historyItems, File file) throws ElevenLabsException {
return ElevenLabsAPI.getInstance().downloadHistory(historyItems.stream().map(HistoryItem::getHistoryItemId).collect(Collectors.toList()), file);
}
@@ -80,7 +78,7 @@ public record History(List<HistoryItem> history) {
private Voice voice;
private final History history;
public HistoryItem(String historyItemId, String voiceId, String voiceName, String text, long dateUnix, int characterCountChangeFrom, int characterCountChangeTo, String contentType, String state, History history) {
HistoryItem(String historyItemId, String voiceId, String voiceName, String text, long dateUnix, int characterCountChangeFrom, int characterCountChangeTo, String contentType, String state, History history) {
this.historyItemId = historyItemId;
this.voiceId = voiceId;
this.voiceName = voiceName;
@@ -98,7 +96,7 @@ public record History(List<HistoryItem> history) {
if (voice == null) {
try {
voice = ElevenLabsAPI.getInstance().getVoice(voiceId);
} catch (ElevenLabsValidationException | IOException | ElevenLabsAPINotInitiatedException e) {
} catch (ElevenLabsException e) {
throw new RuntimeException(e);
}
}
@@ -145,13 +143,13 @@ public record History(List<HistoryItem> history) {
return state;
}
public String delete() throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public String delete() throws ElevenLabsException {
String output = ElevenLabsAPI.getInstance().deleteHistoryItem(this);
history.history.remove(this);
return output;
}
public File downloadAudio(File outputFile) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public File downloadAudio(File outputFile) throws ElevenLabsException {
return ElevenLabsAPI.getInstance().getHistoryItemAudio(this, outputFile);
}

View File

@@ -1,12 +1,10 @@
package net.andrewcpu.elevenlabs.elements.voice;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsAPINotInitiatedException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsValidationException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import org.json.simple.JSONObject;
import java.io.File;
import java.io.IOException;
public class Sample {
private final String sampleId;
@@ -16,7 +14,7 @@ public class Sample {
private final String hash;
private Voice voice;
public static Sample fromJSON(JSONObject object) {
static Sample fromJSON(JSONObject object) {
String sampleId = (String) object.get("sample_id");
String fileName = (String) object.get("file_name");
String mimeType = (String) object.get("mime_type");
@@ -27,7 +25,7 @@ public class Sample {
}
public Sample(String sampleId, String fileName, String mimeType, long sizeBytes, String hash) {
private Sample(String sampleId, String fileName, String mimeType, long sizeBytes, String hash) {
this.sampleId = sampleId;
this.fileName = fileName;
this.mimeType = mimeType;
@@ -64,11 +62,11 @@ public class Sample {
return hash;
}
public String delete() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String delete() throws ElevenLabsException {
return ElevenLabsAPI.getInstance().deleteSample(voice,this);
}
public File downloadAudio(File outputFile) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public File downloadAudio(File outputFile) throws ElevenLabsException {
return ElevenLabsAPI.getInstance().getSampleAudio(voice, this, outputFile);
}

View File

@@ -2,7 +2,7 @@ package net.andrewcpu.elevenlabs.elements.voice;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.elements.VoiceBuilder;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsAPINotInitiatedException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsValidationException;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
@@ -69,17 +69,17 @@ public class Voice {
public static List<Voice> getVoices() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public static List<Voice> getVoices() throws ElevenLabsException {
return ElevenLabsAPI.getInstance().getVoices();
}
public static Voice get(String voiceId) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public static Voice get(String voiceId) throws ElevenLabsException {
Voice voice = ElevenLabsAPI.getInstance().getVoice(voiceId);
voice.hasSettings = true;
return voice;
}
public static Voice get(String voiceId, boolean withSettings) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public static Voice get(String voiceId, boolean withSettings) throws ElevenLabsException {
Voice voice = ElevenLabsAPI.getInstance().getVoice(voiceId, withSettings);
voice.hasSettings = withSettings;
return voice;
@@ -129,11 +129,11 @@ public class Voice {
return voiceSettings;
}
public String delete() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String delete() throws IOException, ElevenLabsException {
return ElevenLabsAPI.getInstance().deleteVoice(getVoiceId());
}
public void fetchSettings() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public void fetchSettings() throws IOException, ElevenLabsException {
this.voiceSettings = ElevenLabsAPI.getInstance().getVoiceSettings(getVoiceId());
hasSettings = true;
}
@@ -142,7 +142,7 @@ public class Voice {
return VoiceBuilder.fromVoice(this);
}
public String updateVoiceSettings(VoiceSettings settings) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public String updateVoiceSettings(VoiceSettings settings) throws ElevenLabsException {
String response = ElevenLabsAPI.getInstance().editVoice(this, settings);
if(response != null){
this.voiceSettings = settings;
@@ -150,13 +150,13 @@ public class Voice {
return response;
}
return null;
}
}//todo figure out what this endpoint returns. String is not description, but docs don't have the info
public File generate(String text, VoiceSettings voiceSettings, File output) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public File generate(String text, VoiceSettings voiceSettings, File output) throws ElevenLabsException {
return ElevenLabsAPI.getInstance().getTextToSpeech(text, this, voiceSettings,output);
}
public File generate(String text, File output) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
public File generate(String text, File output) throws ElevenLabsException, IOException {
if(!hasSettings){
throw new ElevenLabsValidationException("Cannot use default voice settings for " + voiceId + " because this object does not have VoiceSettings");
}

View File

@@ -1,12 +1,9 @@
package net.andrewcpu.elevenlabs.elements.voice;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsAPINotInitiatedException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsValidationException;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import org.json.simple.JSONObject;
import java.io.IOException;
@SuppressWarnings("unchecked")
public class VoiceSettings {
private double stability;
@@ -15,7 +12,7 @@ public class VoiceSettings {
public static VoiceSettings fromJSON(JSONObject object){
return new VoiceSettings((double)object.get("stability"), (double)object.get("similarity"));
}
public static VoiceSettings getDefaultVoiceSettings() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
public static VoiceSettings getDefaultVoiceSettings() throws ElevenLabsException {
return ElevenLabsAPI.getInstance().getDefaultVoiceSettings();
}

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.exceptions;
public class ElevenLabsAPINotInitiatedException extends Exception{
public class ElevenLabsAPINotInitiatedException extends ElevenLabsException{
public ElevenLabsAPINotInitiatedException() {
super("ElevenLabs API has not been initiated.");
}

View File

@@ -0,0 +1,7 @@
package net.andrewcpu.elevenlabs.exceptions;
public abstract class ElevenLabsException extends Exception{
public ElevenLabsException(String message) {
super(message);
}
}

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.exceptions;
public class ElevenLabsValidationException extends Exception{
public class ElevenLabsValidationException extends ElevenLabsException{
public ElevenLabsValidationException(String message) {
super(message);
}