9 Commits

Author SHA1 Message Date
Andrew Stein
78dba6753d Added Maven deploy info to pom 2023-02-25 20:41:31 -05:00
Andrew Stein
49e2e716eb Updated exception handling, refactored classes to further reduce duplicate code. Adjusted endpoint handling. Removed logic from ElevenLabsAPI and moved it into components. 2023-02-25 20:36:59 -05:00
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
Andrew Stein
12375897b0 Workflow. I am making this work ;( 2023-02-25 02:09:36 -05:00
Andrew Stein
9c3b295de7 Workflow. I am making this work. 2023-02-25 02:05:08 -05:00
73 changed files with 977 additions and 696 deletions

View File

@@ -1,4 +1,4 @@
name: Java CI with Maven
name: Dev Build
on:
push:
@@ -27,25 +27,12 @@ jobs:
id: version
run: echo ::set-output name=version::$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
- name: Fetch tags from remote
run: git fetch --tags
- name: Get current tag
id: current_tag
run: echo ::set-output name=tag::$(git describe --abbrev=0 --tags 2>/dev/null || echo '')
- name: Create new tag
id: makeatag
run: |
if [ `$(git tag v${{ steps.version.outputs.version }} | wc -l)` -le 2 ]; then
echo ::set-output name=successful::true
else
echo ::set-output name=successful::false
fi
run: git tag v${{github.run_number}}
- name: Push new tag
if: ${{steps.makeatag.successful}}
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()
@@ -54,7 +41,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.current_tag.outputs.tag || 'v$(steps.version.outputs.version)' }}
tag_name: v${{github.run_number}} # Defeated by logic I stand.
release_name: "Release v${{ steps.version.outputs.version }} Build ${{ github.run_number }}"
draft: false
prerelease: false

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.
@@ -253,11 +293,11 @@ History(List<HistoryItem> history)
```java
History.HistoryItem(String historyItemId, String voiceId, String voiceName, String text,
long dateUnix, int characterCountChangeFrom, int characterCountChangeTo,
String contentType, String state)
String contentType, GenerationState state)
```
```java
public enum State {
public enum GenerationState {
CREATED,
DELETED,
PROCESSING;
@@ -271,8 +311,8 @@ User(Subscription subscription, boolean isNewUser, String xiApiKey)
```java
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)
boolean canExtendVoiceLimit, boolean canUseInstantVoiceCloning, List<AvailableModel> availableModels,
AccountStatus status, NextInvoice nextInvoice)
```
```java
AvailableModel(String modelId, String displayName, List<SupportedLanguage> supportedLanguages)
@@ -284,7 +324,7 @@ SupportedLanguage(String isoCode, String displayName)
NextInvoice(int amountDueCents, long nextPaymentAttemptUnix)
```
```java
public enum Status {
public enum AccountStatus {
TRIALING,
ACTIVE,
INCOMPLETE,
@@ -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

@@ -24,6 +24,14 @@
<tag>HEAD</tag>
</scm>
<distributionManagement>
<repository>
<id>github</id>
<name>GitHub AndrewCPU Apache Maven Packages</name>
<url>https://maven.pkg.github.com/AndrewCPU/elevenlabs-api</url>
</repository>
</distributionManagement>
<groupId>net.andrewcpu</groupId>
<artifactId>elevenlabs-api</artifactId>
<version>1.0-SNAPSHOT</version>

View File

@@ -1,52 +1,37 @@
package net.andrewcpu.elevenlabs;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.ElevenLabsResponse;
import net.andrewcpu.elevenlabs.api.multipart.MultipartFile;
import net.andrewcpu.elevenlabs.api.multipart.MultipartFormContent;
import net.andrewcpu.elevenlabs.api.requests.history.DeleteHistoryItemRequest;
import net.andrewcpu.elevenlabs.api.requests.history.DownloadHistoryRequest;
import net.andrewcpu.elevenlabs.api.requests.history.GetHistoryAudioRequest;
import net.andrewcpu.elevenlabs.api.requests.history.GetHistoryRequest;
import net.andrewcpu.elevenlabs.api.requests.samples.DeleteSampleRequest;
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.elements.user.Subscription;
import net.andrewcpu.elevenlabs.elements.user.User;
import net.andrewcpu.elevenlabs.elements.voice.History;
import net.andrewcpu.elevenlabs.elements.voice.Sample;
import net.andrewcpu.elevenlabs.elements.voice.Voice;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.api.net.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.net.ElevenLabsResponse;
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 net.andrewcpu.elevenlabs.util.DebugLogger;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.ProtocolException;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
import static net.andrewcpu.elevenlabs.util.MultipartUtil.addFilePart;
import static net.andrewcpu.elevenlabs.util.MultipartUtil.addFormField;
import static net.andrewcpu.elevenlabs.util.MultipartUtil.*;
@SuppressWarnings({"unchecked", "UnusedReturnValue"})
public class ElevenLabsAPI {
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() {
return debugMode;
}
public static void setDebugMode(boolean enabled){
public static <T> T getRequestResult(ElevenLabsRequest<T> request) throws ElevenLabsException {
return ElevenLabsAPI.getInstance().getResult(request);
}
public static void setDebugMode(boolean enabled) {
debugMode = enabled;
}
@@ -69,164 +54,67 @@ public class ElevenLabsAPI {
this.apiKey = apiKey;
}
public Subscription getSubscription() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (Subscription) getResult(new GetSubscriptionInfoRequest());
}
public User getUser() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
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 List<Voice> getVoices() throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
return (List<Voice>) getResult(new GetVoicesRequest());
}
public Voice getVoice(String voiceId, boolean withSettings) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
return (Voice)getResult(new GetVoiceRequest(voiceId, withSettings));
}
public Voice getVoice(String voiceId) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
return getVoice(voiceId, true);
}
public VoiceSettings getVoiceSettings(String voiceId) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (VoiceSettings) getResult(new GetVoiceSettingsRequest(voiceId));
}
public VoiceSettings getVoiceSettings(Voice voice) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return getVoiceSettings(voice.getVoiceId());
}
public String deleteVoice(Voice voice) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return deleteVoice(voice.getVoiceId());
}
public String deleteVoice(String voiceId) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (String)getResult(new DeleteVoiceRequest(voiceId));
}
public String editVoice(Voice voice, VoiceSettings voiceSettings) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return editVoice(voice.getVoiceId(), voiceSettings);
}
public String editVoice(String voiceId, VoiceSettings settings) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (String)getResult(new UpdateVoiceSettingsRequest(voiceId, settings));
}
public String deleteSample(String voiceId, String sampleId) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (String)getResult(new DeleteSampleRequest(voiceId, sampleId));
}
public String deleteSample(Voice voice, String sampleId) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return deleteSample(voice.getVoiceId(), sampleId);
}
public String deleteSample(String voiceId, Sample sample) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return deleteSample(voiceId, sample.getSampleId());
}
public String deleteSample(Voice voice, Sample sample) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return deleteSample(voice.getVoiceId(), sample.getSampleId());
}
public File getSampleAudio(String voiceId, String sampleId, File file) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (File)getResult(new GetAudioSampleRequest(voiceId, sampleId, file));
}
public File getSampleAudio(Voice voice, Sample sample, File file) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return getSampleAudio(voice.getVoiceId(), sample.getSampleId(), file);
}
public History getHistory() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (History)getResult(new GetHistoryRequest());
}
public File downloadHistory(List<String> historyIds, File outputFile) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (File)getResult(new DownloadHistoryRequest(historyIds, outputFile));
}
public File getHistoryItemAudio(History.HistoryItem historyItem, File outputFile) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (File)getResult(new GetHistoryAudioRequest(historyItem.getHistoryItemId(), outputFile));
}
public String deleteHistoryItem(History.HistoryItem historyItem) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (String)getResult(new DeleteHistoryItemRequest(historyItem.getHistoryItemId()));
}
public VoiceSettings getDefaultVoiceSettings() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return (VoiceSettings)getResult(new GetDefaultVoiceSettingsRequest());
}
public String createVoice(String name, Map<String, String> labels, List<File> files) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
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 {
EditVoiceRequest editVoiceRequest = new EditVoiceRequest(voiceId,name,files,labels);
return (String)(getResult(editVoiceRequest));
}
private void checkOrThrow(ElevenLabsResponse<?> response) throws ElevenLabsValidationException {
if(response == null){
private void checkOrThrow(ElevenLabsResponse<?> response) throws ElevenLabsException {
if (response == null) {
throw new ElevenLabsValidationException("An error has occurred.");
}
if(!response.isSuccessful()){
if (!response.isSuccessful()) {
throw response.getException();
}
}
private Object getResult(ElevenLabsRequest<?> request) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
ElevenLabsResponse<?> response = sendRequest(request);
public <T> T getResult(ElevenLabsRequest<T> request) throws ElevenLabsException {
ElevenLabsResponse<T> response = sendRequest(request);
checkOrThrow(response);
return response.getResult();
}
private ElevenLabsResponse<?> sendRequest(ElevenLabsRequest<?> request) throws IOException, ElevenLabsAPINotInitiatedException {
if(!instantiated){
private <T> ElevenLabsResponse<T> sendRequest(ElevenLabsRequest<T> request) throws ElevenLabsException {
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);
URL url;
int responseCode;
InputStream successStream = null, errorStream = null;
if(request.getContentType() == ContentType.JSON){
handleJSON(request, connection);
}
else if(request.getContentType() == ContentType.MULTIPART){
handleMultipart(request, connection, boundary);
}
int responseCode = 999;
try {
String boundary = "---------------------------" + System.currentTimeMillis();
DebugLogger.log(ElevenLabsAPI.class,"Request to " + baseURL + ", " + formattedEndpoint);
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);
connection.getOutputStream().flush();
connection.getOutputStream().close();
}
responseCode = connection.getResponseCode();
if (responseCode >= 200 && responseCode < 300) {
successStream = connection.getInputStream();
} else {
errorStream = connection.getErrorStream();
}
} catch (IOException e) {
e.printStackTrace();
}
InputStream successStream = null,
errorStream = null;
if (responseCode >= 200 && responseCode < 300) {
successStream = connection.getInputStream();
} else {
errorStream = connection.getErrorStream();
throw new ElevenLabsException(e);
}
return new ElevenLabsResponse<>(responseCode, errorStream, successStream, request);
}
private void setupConnection(ElevenLabsRequest<?> request, String boundary, HttpURLConnection connection) throws ProtocolException {
connection.setConnectTimeout(60000);
connection.setReadTimeout(60000);
System.out.println(request.getMethod().name());
connection.setRequestMethod(request.getMethod().name());
String contType = request.getContentType().getType();
if(request.getContentType() == ContentType.MULTIPART){
if (request.getContentType() == ContentType.MULTIPART) {
contType += "; boundary=" + boundary;
}
connection.setRequestProperty("xi-api-key", apiKey);
@@ -241,17 +129,8 @@ public class ElevenLabsAPI {
}
private static void handleMultipart(ElevenLabsRequest<?> request, HttpURLConnection connection, String boundary) throws IOException {
for(MultipartFormContent item : request.getMultipartForm().getItems()){
if(item instanceof MultipartFile multipartFile){
addFilePart(multipartFile.getName(),multipartFile.getFilename(), multipartFile.getFile(), boundary, connection);
}
else{
addFormField(item.getName(), item.getValue(), boundary, connection);
}
}
writeFormValues(request, connection, boundary);
String footer = "--" + boundary + "--\r\n";
connection.getOutputStream().write(footer.getBytes(StandardCharsets.UTF_8));
connection.getOutputStream().flush();
connection.getOutputStream().close();
}
}

View File

@@ -0,0 +1,31 @@
package net.andrewcpu.elevenlabs.api;
import net.andrewcpu.elevenlabs.api.net.requests.history.DeleteHistoryItemRequest;
import net.andrewcpu.elevenlabs.api.net.requests.history.DownloadHistoryRequest;
import net.andrewcpu.elevenlabs.api.net.requests.history.GetHistoryAudioRequest;
import net.andrewcpu.elevenlabs.api.net.requests.history.GetHistoryRequest;
import net.andrewcpu.elevenlabs.elements.voice.History;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import java.io.File;
import java.util.List;
import static net.andrewcpu.elevenlabs.ElevenLabsAPI.getRequestResult;
public class HistoryAPI {
public static History getHistory() throws ElevenLabsException {
return getRequestResult(new GetHistoryRequest());
}
public static File downloadHistory(List<String> historyIds, File outputFile) throws ElevenLabsException {
return getRequestResult(new DownloadHistoryRequest(historyIds, outputFile));
}
public static File getHistoryItemAudio(History.HistoryItem historyItem, File outputFile) throws ElevenLabsException {
return getRequestResult(new GetHistoryAudioRequest(historyItem.getHistoryItemId(), outputFile));
}
public static String deleteHistoryItem(History.HistoryItem historyItem) throws ElevenLabsException {
return getRequestResult(new DeleteHistoryItemRequest(historyItem.getHistoryItemId()));
}
}

View File

@@ -0,0 +1,36 @@
package net.andrewcpu.elevenlabs.api;
import net.andrewcpu.elevenlabs.api.net.requests.samples.DeleteSampleRequest;
import net.andrewcpu.elevenlabs.api.net.requests.samples.GetAudioSampleRequest;
import net.andrewcpu.elevenlabs.elements.voice.Sample;
import net.andrewcpu.elevenlabs.elements.voice.Voice;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import org.json.simple.JSONObject;
import java.io.File;
import static net.andrewcpu.elevenlabs.ElevenLabsAPI.getRequestResult;
public class SampleAPI {
public static JSONObject deleteSample(String voiceId, String sampleId) throws ElevenLabsException {
return getRequestResult(new DeleteSampleRequest(voiceId, sampleId));
}
public static JSONObject deleteSample(Voice voice, String sampleId) throws ElevenLabsException {
return deleteSample(voice.getVoiceId(), sampleId);
}
public static JSONObject deleteSample(String voiceId, Sample sample) throws ElevenLabsException {
return deleteSample(voiceId, sample.getSampleId());
}
public static JSONObject deleteSample(Voice voice, Sample sample) throws ElevenLabsException {
return deleteSample(voice.getVoiceId(), sample.getSampleId());
}
public static File getSampleAudio(String voiceId, String sampleId, File file) throws ElevenLabsException {
return getRequestResult(new GetAudioSampleRequest(voiceId, sampleId, file));
}
public static File getSampleAudio(Voice voice, Sample sample, File file) throws ElevenLabsException {
return getSampleAudio(voice.getVoiceId(), sample.getSampleId(), file);
}
}

View File

@@ -0,0 +1,20 @@
package net.andrewcpu.elevenlabs.api;
import net.andrewcpu.elevenlabs.api.net.requests.user.GetSubscriptionInfoRequest;
import net.andrewcpu.elevenlabs.api.net.requests.user.GetUserRequest;
import net.andrewcpu.elevenlabs.elements.user.Subscription;
import net.andrewcpu.elevenlabs.elements.user.User;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import static net.andrewcpu.elevenlabs.ElevenLabsAPI.getRequestResult;
public class UserAPI {
public static Subscription getSubscription() throws ElevenLabsException {
return getRequestResult(new GetSubscriptionInfoRequest());
}
public static User getUser() throws ElevenLabsException {
return getRequestResult(new GetUserRequest());
}
}

View File

@@ -0,0 +1,78 @@
package net.andrewcpu.elevenlabs.api;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.api.net.requests.voices.*;
import net.andrewcpu.elevenlabs.api.net.requests.voices.settings.GetDefaultVoiceSettingsRequest;
import net.andrewcpu.elevenlabs.api.net.requests.voices.settings.GetVoiceSettingsRequest;
import net.andrewcpu.elevenlabs.api.net.requests.voices.settings.UpdateVoiceSettingsRequest;
import net.andrewcpu.elevenlabs.elements.voice.Voice;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import org.json.simple.JSONObject;
import java.io.File;
import java.util.List;
import java.util.Map;
import static net.andrewcpu.elevenlabs.ElevenLabsAPI.getRequestResult;
public class VoiceAPI {
public static VoiceSettings getDefaultVoiceSettings() throws ElevenLabsException {
return getRequestResult(new GetDefaultVoiceSettingsRequest());
}
public static File getTextToSpeech(String text, Voice voice, VoiceSettings settings, File outputFile) throws ElevenLabsException {
return getRequestResult(new GenerateTextToSpeechRequest(voice, settings, text, outputFile));
}
public static List<Voice> getVoices() throws ElevenLabsException {
return getRequestResult(new GetVoicesRequest());
}
public static Voice getVoice(String voiceId, boolean withSettings) throws ElevenLabsException {
return getRequestResult(new GetVoiceRequest(voiceId, withSettings));
}
public static Voice getVoice(String voiceId) throws ElevenLabsException {
return getVoice(voiceId, true);
}
public static VoiceSettings getVoiceSettings(String voiceId) throws ElevenLabsException {
return getRequestResult(new GetVoiceSettingsRequest(voiceId));
}
public static VoiceSettings getVoiceSettings(Voice voice) throws ElevenLabsException {
return getVoiceSettings(voice.getVoiceId());
}
public static boolean deleteVoice(Voice voice) throws ElevenLabsException {
return deleteVoice(voice.getVoiceId());
}
public static boolean deleteVoice(String voiceId) throws ElevenLabsException {
JSONObject result = getRequestResult(new DeleteVoiceRequest(voiceId));
if(result.containsKey("status") && result.get("status").equals("ok")){
return true;
}
return false;
}
public static JSONObject editVoice(Voice voice, VoiceSettings voiceSettings) throws ElevenLabsException {
return editVoice(voice.getVoiceId(), voiceSettings);
}
public static JSONObject editVoice(String voiceId, VoiceSettings settings) throws ElevenLabsException {
return getRequestResult(new UpdateVoiceSettingsRequest(voiceId, settings));
}
public static String createVoice(String name, Map<String, String> labels, List<File> files) throws ElevenLabsException {
CreateVoiceRequest request = new CreateVoiceRequest(name, files, labels);
return getRequestResult(request);
}
public static JSONObject editVoice(String voiceId, String name, Map<String, String> labels, List<File> files) throws ElevenLabsException {
EditVoiceRequest editVoiceRequest = new EditVoiceRequest(voiceId, name, files, labels);
return getRequestResult(editVoiceRequest);
}
}

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.api;
package net.andrewcpu.elevenlabs.api.net;
import net.andrewcpu.elevenlabs.api.multipart.MultipartForm;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformer;
import net.andrewcpu.elevenlabs.api.net.multipart.MultipartForm;
import net.andrewcpu.elevenlabs.api.net.transformers.ResultTransformer;
import net.andrewcpu.elevenlabs.enums.ContentType;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import net.andrewcpu.elevenlabs.enums.ResponseType;
@@ -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

@@ -1,4 +1,4 @@
package net.andrewcpu.elevenlabs.api;
package net.andrewcpu.elevenlabs.api.net;
import net.andrewcpu.elevenlabs.enums.ResponseType;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsExceptionBuilder;
@@ -15,12 +15,12 @@ import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
public class ElevenLabsResponse<T> {
private final int responseCode;
private final InputStream errorStream;
private final InputStream successStream;
private final ElevenLabsRequest<T> request;
private JSONObject successful;
private JSONObject error;
private final InputStream successStream;
private JSONObject successful;
private final ElevenLabsRequest<T> request;
private final int responseCode;
private T resultingObject;
public ElevenLabsResponse(int responseCode, InputStream errorStream, InputStream successStream, ElevenLabsRequest<T> request) {

View File

@@ -1,4 +1,4 @@
package net.andrewcpu.elevenlabs.api.multipart;
package net.andrewcpu.elevenlabs.api.net.multipart;
import java.io.File;

View File

@@ -1,4 +1,4 @@
package net.andrewcpu.elevenlabs.api.multipart;
package net.andrewcpu.elevenlabs.api.net.multipart;
import java.util.ArrayList;
import java.util.Arrays;

View File

@@ -1,4 +1,4 @@
package net.andrewcpu.elevenlabs.api.multipart;
package net.andrewcpu.elevenlabs.api.net.multipart;
import java.io.File;

View File

@@ -0,0 +1,39 @@
package net.andrewcpu.elevenlabs.api.net.requests;
import net.andrewcpu.elevenlabs.api.net.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.net.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.net.requests;
import net.andrewcpu.elevenlabs.api.net.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.net.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.net.requests;
import net.andrewcpu.elevenlabs.api.net.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.net.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

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

View File

@@ -1,8 +1,7 @@
package net.andrewcpu.elevenlabs.api.requests.history;
package net.andrewcpu.elevenlabs.api.net.requests.history;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.FilePingPongTransformer;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import net.andrewcpu.elevenlabs.api.net.requests.ElevenLabsPostRequest;
import net.andrewcpu.elevenlabs.api.net.transformers.FilePingPongTransformer;
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

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

View File

@@ -0,0 +1,16 @@
package net.andrewcpu.elevenlabs.api.net.requests.history;
import net.andrewcpu.elevenlabs.api.net.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.net.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.elements.voice.History;
public class GetHistoryRequest extends ElevenLabsGetRequest<History> {
public GetHistoryRequest() {
super(RequestTransformer.HISTORY_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "history";
}
}

View File

@@ -0,0 +1,20 @@
package net.andrewcpu.elevenlabs.api.net.requests.samples;
import net.andrewcpu.elevenlabs.api.net.requests.ElevenLabsDeleteRequest;
import net.andrewcpu.elevenlabs.api.net.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.enums.ResponseType;
import org.json.simple.JSONObject;
import java.util.List;
public class DeleteSampleRequest extends ElevenLabsDeleteRequest<JSONObject> {
public DeleteSampleRequest(String voiceId, String sampleId) {
super(List.of(voiceId, sampleId), RequestTransformer.IN_OUT_TRANSFORMER);
responseType = (ResponseType.JSON);
}
@Override
public String getEndpoint() {
return "voices/{voiceId}/samples/{sampleId}";
}
}

View File

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

View File

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

View File

@@ -0,0 +1,16 @@
package net.andrewcpu.elevenlabs.api.net.requests.user;
import net.andrewcpu.elevenlabs.api.net.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.net.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.elements.user.User;
public class GetUserRequest extends ElevenLabsGetRequest<User> {
public GetUserRequest() {
super(RequestTransformer.USER_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "user";
}
}

View File

@@ -0,0 +1,43 @@
package net.andrewcpu.elevenlabs.api.net.requests.voices;
import net.andrewcpu.elevenlabs.api.net.multipart.MultipartFile;
import net.andrewcpu.elevenlabs.api.net.multipart.MultipartForm;
import net.andrewcpu.elevenlabs.api.net.multipart.MultipartFormContent;
import net.andrewcpu.elevenlabs.api.net.requests.ElevenLabsPostRequest;
import net.andrewcpu.elevenlabs.api.net.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.enums.ContentType;
import org.json.simple.JSONObject;
import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
@SuppressWarnings("unchecked")
public class CreateVoiceRequest extends ElevenLabsPostRequest<String> {
public CreateVoiceRequest(String name, List<File> files, Map<String, String> labels) {
super(RequestTransformer.VOICE_ID_TRANSFORMER);
this.contentType = ContentType.MULTIPART;
this.multipartForm = new MultipartForm();
buildBody(name, files, labels);
}
@Override
public String getEndpoint() {
return "voices/add";
}
private void buildBody(String name, List<File> files, Map<String, String> labels) {
JSONObject val = new JSONObject();
if (labels != null && labels.size() != 0) {
labels.keySet().forEach(key -> val.put(key, labels.get(key)));
}
MultipartFormContent nameMultipart = new MultipartFormContent("name", URLEncoder.encode(name, StandardCharsets.UTF_8));
MultipartFormContent labelsMultipart = new MultipartFormContent("labels", val.toJSONString());
this.multipartForm.push(nameMultipart, labelsMultipart);
this.multipartForm.push(files.stream()
.map(file -> new MultipartFile("files", file))
.toArray(MultipartFile[]::new));
}
}

View File

@@ -0,0 +1,18 @@
package net.andrewcpu.elevenlabs.api.net.requests.voices;
import net.andrewcpu.elevenlabs.api.net.requests.ElevenLabsDeleteRequest;
import net.andrewcpu.elevenlabs.api.net.transformers.RequestTransformer;
import org.json.simple.JSONObject;
import java.util.List;
public class DeleteVoiceRequest extends ElevenLabsDeleteRequest<JSONObject> {
public DeleteVoiceRequest(String voiceId) {
super(List.of(voiceId), RequestTransformer.IN_OUT_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "voices/{voiceId}";
}
}

View File

@@ -0,0 +1,45 @@
package net.andrewcpu.elevenlabs.api.net.requests.voices;
import net.andrewcpu.elevenlabs.api.net.multipart.MultipartFile;
import net.andrewcpu.elevenlabs.api.net.multipart.MultipartForm;
import net.andrewcpu.elevenlabs.api.net.multipart.MultipartFormContent;
import net.andrewcpu.elevenlabs.api.net.requests.ElevenLabsPostRequest;
import net.andrewcpu.elevenlabs.api.net.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.enums.ContentType;
import net.andrewcpu.elevenlabs.enums.ResponseType;
import org.json.simple.JSONObject;
import java.io.File;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;
public class EditVoiceRequest extends ElevenLabsPostRequest<JSONObject> {
public EditVoiceRequest(String voiceId, String name, List<File> files, Map<String, String> labels) {
super(List.of(voiceId), RequestTransformer.IN_OUT_TRANSFORMER);
this.contentType = ContentType.MULTIPART;
this.multipartForm = new MultipartForm();
buildBody(name, files, labels);
}
@Override
public String getEndpoint() {
return "voices/{voiceId}/edit";
}
private void buildBody(String name, List<File> files, Map<String, String> labels) {
JSONObject val = new JSONObject();
if (labels != null && labels.size() != 0) {
labels.keySet().forEach(key -> val.put(key, labels.get(key)));
}
MultipartFormContent nameMultipart = new MultipartFormContent("name", URLEncoder.encode(name, StandardCharsets.UTF_8));
MultipartFormContent labelsMultipart = new MultipartFormContent("labels", val.toJSONString());
this.multipartForm.push(nameMultipart, labelsMultipart);
this.multipartForm.push(files.stream()
.map(file -> new MultipartFile("files", file))
.toArray(MultipartFile[]::new));
}
}

View File

@@ -1,7 +1,7 @@
package net.andrewcpu.elevenlabs.api.requests.voices;
package net.andrewcpu.elevenlabs.api.net.requests.voices;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.transformers.FilePingPongTransformer;
import net.andrewcpu.elevenlabs.api.net.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.net.transformers.FilePingPongTransformer;
import net.andrewcpu.elevenlabs.elements.voice.Voice;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
@@ -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

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

View File

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

View File

@@ -0,0 +1,16 @@
package net.andrewcpu.elevenlabs.api.net.requests.voices.settings;
import net.andrewcpu.elevenlabs.api.net.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.net.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.net.requests.voices.settings;
import net.andrewcpu.elevenlabs.api.net.requests.ElevenLabsGetRequest;
import net.andrewcpu.elevenlabs.api.net.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,20 @@
package net.andrewcpu.elevenlabs.api.net.requests.voices.settings;
import net.andrewcpu.elevenlabs.api.net.requests.ElevenLabsPostRequest;
import net.andrewcpu.elevenlabs.api.net.transformers.RequestTransformer;
import net.andrewcpu.elevenlabs.api.net.transformers.ResultTransformer;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import org.json.simple.JSONObject;
import java.util.List;
public class UpdateVoiceSettingsRequest extends ElevenLabsPostRequest<JSONObject> {
public UpdateVoiceSettingsRequest(String voiceId, VoiceSettings settings){
super(List.of(voiceId), settings.toJSON(), RequestTransformer.IN_OUT_TRANSFORMER);
}
@Override
public String getEndpoint() {
return "voices/{voiceId}/settings/edit";
}
}

View File

@@ -1,4 +1,4 @@
package net.andrewcpu.elevenlabs.api.transformers;
package net.andrewcpu.elevenlabs.api.net.transformers;
import net.andrewcpu.elevenlabs.util.DebugLogger;

View File

@@ -0,0 +1,29 @@
package net.andrewcpu.elevenlabs.api.net.transformers;
import net.andrewcpu.elevenlabs.api.net.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 org.json.simple.JSONObject;
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();
public static final ResultTransformer<JSONObject> IN_OUT_TRANSFORMER = new ResultTransformerAdapter<JSONObject>(){
@Override
public JSONObject transform(JSONObject object) {
return object;
}
};
}

View File

@@ -1,4 +1,4 @@
package net.andrewcpu.elevenlabs.api.transformers;
package net.andrewcpu.elevenlabs.api.net.transformers;
import org.json.simple.JSONObject;

View File

@@ -1,4 +1,4 @@
package net.andrewcpu.elevenlabs.api.transformers;
package net.andrewcpu.elevenlabs.api.net.transformers;
import org.json.simple.JSONObject;

View File

@@ -1,4 +1,4 @@
package net.andrewcpu.elevenlabs.api.transformers;
package net.andrewcpu.elevenlabs.api.net.transformers;
import net.andrewcpu.elevenlabs.util.DebugLogger;

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.api.transformers.obj;
package net.andrewcpu.elevenlabs.api.net.transformers.obj;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.api.net.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.elements.voice.History;
import org.json.simple.JSONObject;

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.api.transformers.obj;
package net.andrewcpu.elevenlabs.api.net.transformers.obj;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.api.net.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.elements.user.Subscription;
import org.json.simple.JSONObject;

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.api.transformers.obj;
package net.andrewcpu.elevenlabs.api.net.transformers.obj;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.api.net.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.elements.user.User;
import org.json.simple.JSONObject;

View File

@@ -0,0 +1,12 @@
package net.andrewcpu.elevenlabs.api.net.transformers.obj;
import net.andrewcpu.elevenlabs.api.net.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

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.api.transformers.obj;
package net.andrewcpu.elevenlabs.api.net.transformers.obj;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.api.net.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.elements.voice.Voice;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.api.transformers.obj;
package net.andrewcpu.elevenlabs.api.net.transformers.obj;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.api.net.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.elements.voice.VoiceSettings;
import org.json.simple.JSONObject;

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.api.transformers.obj;
package net.andrewcpu.elevenlabs.api.net.transformers.obj;
import net.andrewcpu.elevenlabs.api.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.api.net.transformers.ResultTransformerAdapter;
import net.andrewcpu.elevenlabs.elements.voice.Voice;
import org.json.simple.JSONObject;

View File

@@ -1,20 +0,0 @@
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.enums.ResponseType;
import java.util.List;
public class DeleteHistoryItemRequest extends ElevenLabsRequest<String> {
public DeleteHistoryItemRequest(String historyId) {
super(List.of(historyId), null, HTTPMethod.DELETE, new StringPingPongTransformer());
responseType = (ResponseType.STRING);
}
@Override
public String getEndpoint() {
return "v1/history/%s";
}
}

View File

@@ -1,22 +0,0 @@
package net.andrewcpu.elevenlabs.api.requests.history;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
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 GetHistoryAudioRequest(String historyId, File outputFile) {
super(List.of(historyId), null, HTTPMethod.GET, new FilePingPongTransformer(outputFile));
responseType = (ResponseType.FILE_STREAM);
outputFilePath = (outputFile);
}
@Override
public String getEndpoint() {
return "v1/history/%s/audio";
}
}

View File

@@ -1,17 +0,0 @@
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.elements.voice.History;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
public class GetHistoryRequest extends ElevenLabsRequest<History> {
public GetHistoryRequest() {
super(HTTPMethod.GET, new HistoryTransformer());
}
@Override
public String getEndpoint() {
return "v1/history";
}
}

View File

@@ -1,20 +0,0 @@
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.enums.ResponseType;
import java.util.List;
public class DeleteSampleRequest extends ElevenLabsRequest<String> {
public DeleteSampleRequest(String voiceId, String sampleId) {
super(List.of(voiceId, sampleId), null, HTTPMethod.DELETE, new StringPingPongTransformer());
responseType = (ResponseType.STRING);
}
@Override
public String getEndpoint() {
return "v1/voices/%s/samples/%s";
}
}

View File

@@ -1,22 +0,0 @@
package net.andrewcpu.elevenlabs.api.requests.samples;
import net.andrewcpu.elevenlabs.api.ElevenLabsRequest;
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 GetAudioSampleRequest(String voiceId, String sampleId, File outputFile) {
super(List.of(voiceId, sampleId), null, HTTPMethod.GET, new FilePingPongTransformer(outputFile));
responseType = (ResponseType.FILE_STREAM);
outputFilePath = (outputFile);
}
@Override
public String getEndpoint() {
return "v1/voices/%s/samples/%s/audio";
}
}

View File

@@ -1,17 +0,0 @@
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.elements.user.Subscription;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
public class GetSubscriptionInfoRequest extends ElevenLabsRequest<Subscription> {
public GetSubscriptionInfoRequest() {
super(HTTPMethod.GET, new SubscriptionTransformer());
}
@Override
public String getEndpoint() {
return "v1/user/subscription";
}
}

View File

@@ -1,17 +0,0 @@
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.elements.user.User;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
public class GetUserRequest extends ElevenLabsRequest<User> {
public GetUserRequest() {
super(HTTPMethod.GET, new UserTransformer());
}
@Override
public String getEndpoint() {
return "v1/user";
}
}

View File

@@ -1,59 +0,0 @@
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.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;
import java.net.URLEncoder;
import java.nio.charset.StandardCharsets;
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 CreateVoiceRequest(String name, List<File> files, Map<String, String> labels) {
super(HTTPMethod.POST, transformer);
buildBody(name, files, labels);
}
public CreateVoiceRequest(List<String> parameters, String name, List<File> files, Map<String, String> labels) {
super(parameters, HTTPMethod.POST, transformer);
buildBody(name, files, labels);
}
private void buildBody(String name, List<File> files, Map<String, String> labels){
this.contentType = ContentType.MULTIPART;
this.multipartForm = new MultipartForm();
JSONObject val = new JSONObject();
if(labels != null && labels.size() != 0){
for(String key : labels.keySet()){
val.put(key, labels.get(key));
}
}
MultipartFormContent nameMultipart = new MultipartFormContent("name", URLEncoder.encode(name, StandardCharsets.UTF_8));
MultipartFormContent labelsMultipart = new MultipartFormContent("labels", val.toJSONString());
this.multipartForm.push(nameMultipart, labelsMultipart);
for(File file : files){
this.multipartForm.push(new MultipartFile("files", file));
}
}
@Override
public String getEndpoint() {
return "v1/voices/add";
}
}

View File

@@ -1,18 +0,0 @@
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 java.util.List;
public class DeleteVoiceRequest extends ElevenLabsRequest<String> {
public DeleteVoiceRequest(String voiceId) {
super(List.of(voiceId), HTTPMethod.DELETE, new StringPingPongTransformer());
}
@Override
public String getEndpoint() {
return "/v1/voices/%s";
}
}

View File

@@ -1,16 +0,0 @@
package net.andrewcpu.elevenlabs.api.requests.voices;
import java.io.File;
import java.util.List;
import java.util.Map;
public class EditVoiceRequest extends CreateVoiceRequest{
public EditVoiceRequest(String voiceId, String name, List<File> files, Map<String, String> labels) {
super(List.of(voiceId), name, files, labels);
}
@Override
public String getEndpoint() {
return "v1/voices/%s/edit";
}
}

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 +0,0 @@
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.elements.voice.Voice;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import java.util.List;
public class GetVoiceRequest extends ElevenLabsRequest<Voice> {
public GetVoiceRequest(String voiceId, boolean withSettings) {
super(List.of(voiceId, String.valueOf(withSettings)), HTTPMethod.GET, new VoiceTransformer());
}
@Override
public String getEndpoint() {
return "v1/voices/%s?with_settings=%s";
}
}

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 +0,0 @@
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.elements.voice.Voice;
import net.andrewcpu.elevenlabs.enums.HTTPMethod;
import java.util.List;
public class GetVoicesRequest extends ElevenLabsRequest<List<Voice>> {
public GetVoicesRequest() {
super(HTTPMethod.GET, new VoiceListTransformer());
}
@Override
public String getEndpoint() {
return "v1/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

@@ -1,12 +1,12 @@
package net.andrewcpu.elevenlabs.elements;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.api.VoiceAPI;
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,16 +65,16 @@ public class VoiceBuilder {
return this;
}
public Voice edit() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
ElevenLabsAPI.getInstance().editVoice(voiceId, name, labels, files);
return ElevenLabsAPI.getInstance().getVoice(voiceId, true);
public Voice edit() throws ElevenLabsException {
VoiceAPI.editVoice(voiceId, name, labels, files);
return VoiceAPI.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.");
}
String voiceId = ElevenLabsAPI.getInstance().createVoice(name, labels, files);
return ElevenLabsAPI.getInstance().getVoice(voiceId, true);
String voiceId = VoiceAPI.createVoice(name, labels, files);
return VoiceAPI.getVoice(voiceId, true);
}
}

View File

@@ -1,13 +1,11 @@
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.api.UserAPI;
import net.andrewcpu.elevenlabs.enums.AccountStatus;
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;
@@ -23,14 +21,53 @@ public class Subscription {
private final boolean canExtendVoiceLimit;
private final boolean canUseInstantVoiceCloning;
private final List<AvailableModel> availableModels;
private final Status status;
private final AccountStatus status;
private final NextInvoice nextInvoice;
public static Subscription get() throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().getSubscription();
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 Subscription(String tier, int characterCount, int characterLimit, boolean canExtendCharacterLimit, boolean allowedToExtendCharacterLimit,
public static Subscription get() throws ElevenLabsException {
return UserAPI.getSubscription();
}
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;
@@ -43,7 +80,7 @@ public class Subscription {
this.canExtendVoiceLimit = canExtendVoiceLimit;
this.canUseInstantVoiceCloning = canUseInstantVoiceCloning;
this.availableModels = availableModels;
this.status = Status.valueOf(status.toUpperCase());
this.status = AccountStatus.valueOf(status.toUpperCase());
this.nextInvoice = nextInvoice;
}
@@ -87,7 +124,7 @@ public class Subscription {
return availableModels;
}
public Status getStatus() {
public AccountStatus getStatus() {
return status;
}
@@ -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

@@ -1,20 +1,20 @@
package net.andrewcpu.elevenlabs.elements.user;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.api.HistoryAPI;
import net.andrewcpu.elevenlabs.api.UserAPI;
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;
public class User {
private final Subscription subscription;
private final boolean isNewUser;
private final String apiKey;
public static User get() throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().getUser();
public static User get() throws ElevenLabsException {
return UserAPI.getUser();
}
public User(Subscription subscription, boolean isNewUser, String xiApiKey) {
@@ -35,8 +35,8 @@ public class User {
return apiKey;
}
public History getHistory() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().getHistory();
public History getHistory() throws ElevenLabsException {
return HistoryAPI.getHistory();
}
public static User fromJSON(JSONObject object) {
JSONObject subscriptionJson = (JSONObject) object.get("subscription");

View File

@@ -1,14 +1,13 @@
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.api.HistoryAPI;
import net.andrewcpu.elevenlabs.api.VoiceAPI;
import net.andrewcpu.elevenlabs.enums.GenerationState;
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,8 +37,8 @@ public record History(List<HistoryItem> history) {
return history;
}
public static History get() throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().getHistory();
public static History get() throws ElevenLabsException {
return HistoryAPI.getHistory();
}
@@ -52,12 +51,12 @@ public record History(List<HistoryItem> history) {
return null;
}
public File downloadHistory(String[] ids, File file) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().downloadHistory(Arrays.stream(ids).toList(), file);
public File downloadHistory(String[] ids, File file) throws ElevenLabsException {
return HistoryAPI.downloadHistory(Arrays.stream(ids).toList(), file);
}
public File downloadHistory(List<HistoryItem> historyItems, File file) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().downloadHistory(historyItems.stream().map(HistoryItem::getHistoryItemId).collect(Collectors.toList()), file);
public File downloadHistory(List<HistoryItem> historyItems, File file) throws ElevenLabsException {
return HistoryAPI.downloadHistory(historyItems.stream().map(HistoryItem::getHistoryItemId).collect(Collectors.toList()), file);
}
@Override
@@ -76,11 +75,11 @@ public record History(List<HistoryItem> history) {
private final int characterCountChangeFrom;
private final int characterCountChangeTo;
private final String contentType;
private final State state;
private final GenerationState state;
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;
@@ -89,7 +88,7 @@ public record History(List<HistoryItem> history) {
this.characterCountChangeFrom = characterCountChangeFrom;
this.characterCountChangeTo = characterCountChangeTo;
this.contentType = contentType;
this.state = State.valueOf(state.toUpperCase());
this.state = GenerationState.valueOf(state.toUpperCase());
this.history = history;
this.voice = null;
}
@@ -97,8 +96,8 @@ public record History(List<HistoryItem> history) {
public Voice getVoice() {
if (voice == null) {
try {
voice = ElevenLabsAPI.getInstance().getVoice(voiceId);
} catch (ElevenLabsValidationException | IOException | ElevenLabsAPINotInitiatedException e) {
voice = VoiceAPI.getVoice(voiceId);
} catch (ElevenLabsException e) {
throw new RuntimeException(e);
}
}
@@ -141,18 +140,18 @@ public record History(List<HistoryItem> history) {
return contentType;
}
public State getState() {
public GenerationState getState() {
return state;
}
public String delete() throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
String output = ElevenLabsAPI.getInstance().deleteHistoryItem(this);
public String delete() throws ElevenLabsException {
String output = HistoryAPI.deleteHistoryItem(this);
history.history.remove(this);
return output;
}
public File downloadAudio(File outputFile) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().getHistoryItemAudio(this, outputFile);
public File downloadAudio(File outputFile) throws ElevenLabsException {
return HistoryAPI.getHistoryItemAudio(this, outputFile);
}
@Override

View File

@@ -1,12 +1,11 @@
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.api.SampleAPI;
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 +15,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 +26,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,12 +63,18 @@ public class Sample {
return hash;
}
public String delete() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().deleteSample(voice,this);
public boolean delete() throws ElevenLabsException {
JSONObject result = SampleAPI.deleteSample(voice,this);
if(result.containsKey("status")){
if(result.get("status").equals("ok")){
return true;
}
}
return false;
}
public File downloadAudio(File outputFile) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().getSampleAudio(voice, this, outputFile);
public File downloadAudio(File outputFile) throws ElevenLabsException {
return SampleAPI.getSampleAudio(voice, this, outputFile);
}
@Override
@@ -80,7 +85,7 @@ public class Sample {
", mimeType='" + mimeType + '\'' +
", sizeBytes=" + sizeBytes +
", hash='" + hash + '\'' +
", voice=" + voice +
", voice=" + voice.getVoiceId() +
'}';
}
}

View File

@@ -1,14 +1,14 @@
package net.andrewcpu.elevenlabs.elements.voice;
import net.andrewcpu.elevenlabs.ElevenLabsAPI;
import net.andrewcpu.elevenlabs.api.VoiceAPI;
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;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
@@ -69,18 +69,18 @@ public class Voice {
public static List<Voice> getVoices() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().getVoices();
public static List<Voice> getVoices() throws ElevenLabsException {
return VoiceAPI.getVoices();
}
public static Voice get(String voiceId) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
Voice voice = ElevenLabsAPI.getInstance().getVoice(voiceId);
public static Voice get(String voiceId) throws ElevenLabsException {
Voice voice = VoiceAPI.getVoice(voiceId);
voice.hasSettings = true;
return voice;
}
public static Voice get(String voiceId, boolean withSettings) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
Voice voice = ElevenLabsAPI.getInstance().getVoice(voiceId, withSettings);
public static Voice get(String voiceId, boolean withSettings) throws ElevenLabsException {
Voice voice = VoiceAPI.getVoice(voiceId, withSettings);
voice.hasSettings = withSettings;
return voice;
}
@@ -129,40 +129,47 @@ public class Voice {
return voiceSettings;
}
public String delete() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().deleteVoice(getVoiceId());
public boolean delete() throws ElevenLabsException {
return VoiceAPI.deleteVoice(getVoiceId());
}
public void fetchSettings() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
this.voiceSettings = ElevenLabsAPI.getInstance().getVoiceSettings(getVoiceId());
public void fetchSettings() throws ElevenLabsException {
this.voiceSettings = VoiceAPI.getVoiceSettings(getVoiceId());
hasSettings = true;
}
public VoiceBuilder builder() {
public VoiceBuilder editor() {
return VoiceBuilder.fromVoice(this);
}
public String updateVoiceSettings(VoiceSettings settings) throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
String response = ElevenLabsAPI.getInstance().editVoice(this, settings);
public void updateVoiceSettings(VoiceSettings settings) throws ElevenLabsException {
JSONObject response = VoiceAPI.editVoice(this, settings);
if(response != null){
this.voiceSettings = settings;
hasSettings = true;
return response;
return;
}
return null;
}
public File generate(String text, VoiceSettings voiceSettings, File output) throws ElevenLabsValidationException, IOException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().getTextToSpeech(text, this, voiceSettings,output);
public File generate(String text, VoiceSettings voiceSettings, File output) throws ElevenLabsException {
return VoiceAPI.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 {
if(!hasSettings){
throw new ElevenLabsValidationException("Cannot use default voice settings for " + voiceId + " because this object does not have VoiceSettings");
}
return generate(text, voiceSettings, output);
}
public String getLabel(String label) {
return labels.get(label);
}
public boolean hasLabel(String label){
return labels.containsKey(label);
}
@Override
public String toString() {
return "Voice{" +

View File

@@ -1,22 +1,19 @@
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.api.VoiceAPI;
import net.andrewcpu.elevenlabs.exceptions.ElevenLabsException;
import org.json.simple.JSONObject;
import java.io.IOException;
@SuppressWarnings("unchecked")
public class VoiceSettings {
private double stability;
private double similarityBoost;
public static VoiceSettings fromJSON(JSONObject object){
return new VoiceSettings((double)object.get("stability"), (double)object.get("similarity"));
return new VoiceSettings((double)object.get("stability"), (double)object.get("similarity_boost"));
}
public static VoiceSettings getDefaultVoiceSettings() throws IOException, ElevenLabsValidationException, ElevenLabsAPINotInitiatedException {
return ElevenLabsAPI.getInstance().getDefaultVoiceSettings();
public static VoiceSettings getDefaultVoiceSettings() throws ElevenLabsException {
return VoiceAPI.getDefaultVoiceSettings();
}

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.enums;
public enum Status {
public enum AccountStatus {
TRIALING,
ACTIVE,
INCOMPLETE,

View File

@@ -1,6 +1,6 @@
package net.andrewcpu.elevenlabs.enums;
public enum State {
public enum GenerationState {
CREATED,
DELETED,
PROCESSING

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,13 @@
package net.andrewcpu.elevenlabs.exceptions;
import java.io.IOException;
public class ElevenLabsException extends Exception{
public ElevenLabsException(String message) {
super(message);
}
public ElevenLabsException(IOException ioException) {
super(ioException);
}
}

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);
}

View File

@@ -1,5 +1,9 @@
package net.andrewcpu.elevenlabs.util;
import net.andrewcpu.elevenlabs.api.net.ElevenLabsRequest;
import net.andrewcpu.elevenlabs.api.net.multipart.MultipartFile;
import net.andrewcpu.elevenlabs.api.net.multipart.MultipartFormContent;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -33,4 +37,14 @@ public class MultipartUtil {
connection.getOutputStream().write("\r\n".getBytes(StandardCharsets.UTF_8));
}
public static void writeFormValues(ElevenLabsRequest<?> request, HttpURLConnection connection, String boundary) throws IOException {
for (MultipartFormContent item : request.getMultipartForm().getItems()) {
if (item instanceof MultipartFile multipartFile) {
addFilePart(multipartFile.getName(), multipartFile.getFilename(), multipartFile.getFile(), boundary, connection);
} else {
addFormField(item.getName(), item.getValue(), boundary, connection);
}
}
}
}