Skip to content
This repository was archived by the owner on Apr 1, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.9-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.1-bin.zip
networkTimeout=10000
validateDistributionUrl=true
zipStoreBase=GRADLE_USER_HOME
Expand Down
2 changes: 1 addition & 1 deletion sample-app/src/main/java/sample/App.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ public static void main(String... args) throws IOException, InterruptedException
System.out.println("Delete transcript. " + transcript);

File file = new File("sample-app/src/main/resources/nZP7pb_t4oA.mp3");
UploadedFile uploadedFile = client.files().upload(Files.readAllBytes(file.toPath()));
UploadedFile uploadedFile = client.files().upload(file);
System.out.println("Uploaded file" + uploadedFile);

transcript = client.transcripts().submit(TranscriptParams.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,7 @@ public Transcript transcribe(
TranscriptOptionalParams transcriptParams,
RequestOptions requestOptions
) throws IOException {
UploadedFile uploadedFile = client.files().upload(Files.readAllBytes(file.toPath()), requestOptions);
UploadedFile uploadedFile = client.files().upload(file, requestOptions);
return transcribe(uploadedFile.getUploadUrl(), transcriptParams, requestOptions);
}

Expand Down
60 changes: 60 additions & 0 deletions src/main/java/com/assemblyai/api/core/FileStream.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.assemblyai.api.core;

import java.io.InputStream;
import java.util.Objects;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import org.jetbrains.annotations.Nullable;

/**
* Represents a file stream with associated metadata for file uploads.
*/
public class FileStream {
private final InputStream inputStream;
private final String fileName;
private final MediaType contentType;

/**
* Constructs a FileStream with the given input stream and optional metadata.
*
* @param inputStream The input stream of the file content. Must not be null.
* @param fileName The name of the file, or null if unknown.
* @param contentType The MIME type of the file content, or null if unknown.
* @throws NullPointerException if inputStream is null
*/
public FileStream(InputStream inputStream, @Nullable String fileName, @Nullable MediaType contentType) {
this.inputStream = Objects.requireNonNull(inputStream, "Input stream cannot be null");
this.fileName = fileName;
this.contentType = contentType;
}

public FileStream(InputStream inputStream) {
this(inputStream, null, null);
}

public InputStream getInputStream() {
return inputStream;
}

@Nullable
public String getFileName() {
return fileName;
}

@Nullable
public MediaType getContentType() {
return contentType;
}

/**
* Creates a RequestBody suitable for use with OkHttp client.
*
* @return A RequestBody instance representing this file stream.
*/
public RequestBody toRequestBody() {
return new InputStreamRequestBody(contentType, inputStream);
}
}
79 changes: 79 additions & 0 deletions src/main/java/com/assemblyai/api/core/InputStreamRequestBody.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/**
* This file was auto-generated by Fern from our API Definition.
*/
package com.assemblyai.api.core;

import java.io.IOException;
import java.io.InputStream;
import java.util.Objects;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import okhttp3.internal.Util;
import okio.BufferedSink;
import okio.Okio;
import okio.Source;
import org.jetbrains.annotations.Nullable;

/**
* A custom implementation of OkHttp's RequestBody that wraps an InputStream.
* This class allows streaming of data from an InputStream directly to an HTTP request body,
* which is useful for file uploads or sending large amounts of data without loading it all into memory.
*/
public class InputStreamRequestBody extends RequestBody {
private final InputStream inputStream;
private final MediaType contentType;

/**
* Constructs an InputStreamRequestBody with the specified content type and input stream.
*
* @param contentType the MediaType of the content, or null if not known
* @param inputStream the InputStream containing the data to be sent
* @throws NullPointerException if inputStream is null
*/
public InputStreamRequestBody(@Nullable MediaType contentType, InputStream inputStream) {
this.contentType = contentType;
this.inputStream = Objects.requireNonNull(inputStream, "inputStream == null");
}

/**
* Returns the content type of this request body.
*
* @return the MediaType of the content, or null if not specified
*/
@Nullable
@Override
public MediaType contentType() {
return contentType;
}

/**
* Returns the content length of this request body, if known.
* This method attempts to determine the length using the InputStream's available() method,
* which may not always accurately reflect the total length of the stream.
*
* @return the content length, or -1 if the length is unknown
* @throws IOException if an I/O error occurs
*/
@Override
public long contentLength() throws IOException {
return inputStream.available() == 0 ? -1 : inputStream.available();
}

/**
* Writes the content of the InputStream to the given BufferedSink.
* This method is responsible for transferring the data from the InputStream to the network request.
*
* @param sink the BufferedSink to write the content to
* @throws IOException if an I/O error occurs during writing
*/
@Override
public void writeTo(BufferedSink sink) throws IOException {
Source source = null;
try {
source = Okio.source(inputStream);
sink.writeAll(source);
} finally {
Util.closeQuietly(Objects.requireNonNull(source));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;

public class ExtendedFilesClient extends FilesClient {
public ExtendedFilesClient(ClientOptions clientOptions) {
Expand Down Expand Up @@ -38,6 +39,6 @@ public UploadedFile upload(Path filePath) throws IOException {
* Upload a media file to AssemblyAI's servers.
*/
public UploadedFile upload(Path filePath, RequestOptions requestOptions) throws IOException {
return upload(Files.readAllBytes(filePath), requestOptions);
return upload(Files.newInputStream(filePath, StandardOpenOption.READ), requestOptions);
}
}
25 changes: 21 additions & 4 deletions src/main/java/com/assemblyai/api/resources/files/FilesClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.assemblyai.api.core.AssemblyAIApiException;
import com.assemblyai.api.core.AssemblyAIException;
import com.assemblyai.api.core.ClientOptions;
import com.assemblyai.api.core.InputStreamRequestBody;
import com.assemblyai.api.core.ObjectMappers;
import com.assemblyai.api.core.RequestOptions;
import com.assemblyai.api.errors.BadRequestError;
Expand All @@ -18,9 +19,12 @@
import com.assemblyai.api.resources.files.types.UploadedFile;
import com.assemblyai.api.types.Error;
import com.fasterxml.jackson.core.JsonProcessingException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import okhttp3.Headers;
import okhttp3.HttpUrl;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
Expand All @@ -37,24 +41,23 @@ public FilesClient(ClientOptions clientOptions) {
/**
* Upload a media file to AssemblyAI's servers.
*/
public UploadedFile upload(byte[] request) {
public UploadedFile upload(InputStream request) {
return upload(request, null);
}

/**
* Upload a media file to AssemblyAI's servers.
*/
public UploadedFile upload(byte[] request, RequestOptions requestOptions) {
public UploadedFile upload(InputStream request, RequestOptions requestOptions) {
HttpUrl httpUrl = HttpUrl.parse(this.clientOptions.environment().getUrl())
.newBuilder()
.addPathSegments("v2/upload")
.build();
RequestBody body = RequestBody.create(request);
RequestBody body = new InputStreamRequestBody(MediaType.parse("application/octet-stream"), request);
Request okhttpRequest = new Request.Builder()
.url(httpUrl)
.method("POST", body)
.headers(Headers.of(clientOptions.headers(requestOptions)))
.addHeader("Content-Type", "application/octet-stream")
.build();
OkHttpClient client = clientOptions.httpClient();
if (requestOptions != null && requestOptions.getTimeout().isPresent()) {
Expand Down Expand Up @@ -99,4 +102,18 @@ public UploadedFile upload(byte[] request, RequestOptions requestOptions) {
throw new AssemblyAIException("Network error executing HTTP request", e);
}
}

/**
* Upload a media file to AssemblyAI's servers.
*/
public UploadedFile upload(byte[] request) {
return upload(new ByteArrayInputStream(request));
}

/**
* Upload a media file to AssemblyAI's servers.
*/
public UploadedFile upload(byte[] request, RequestOptions requestOptions) {
return upload(new ByteArrayInputStream(request), requestOptions);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;

@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonDeserialize(builder = UploadedFile.Builder.class)
Expand Down Expand Up @@ -65,7 +66,7 @@ public static UploadUrlStage builder() {
}

public interface UploadUrlStage {
_FinalStage uploadUrl(String uploadUrl);
_FinalStage uploadUrl(@NotNull String uploadUrl);

Builder from(UploadedFile other);
}
Expand Down Expand Up @@ -95,8 +96,8 @@ public Builder from(UploadedFile other) {
*/
@java.lang.Override
@JsonSetter("upload_url")
public _FinalStage uploadUrl(String uploadUrl) {
this.uploadUrl = uploadUrl;
public _FinalStage uploadUrl(@NotNull String uploadUrl) {
this.uploadUrl = Objects.requireNonNull(uploadUrl, "uploadUrl must not be null");
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import org.jetbrains.annotations.NotNull;

@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonDeserialize(builder = LemurTaskParams.Builder.class)
Expand Down Expand Up @@ -168,7 +169,7 @@ public static PromptStage builder() {
}

public interface PromptStage {
_FinalStage prompt(String prompt);
_FinalStage prompt(@NotNull String prompt);

Builder from(LemurTaskParams other);
}
Expand Down Expand Up @@ -240,8 +241,8 @@ public Builder from(LemurTaskParams other) {
*/
@java.lang.Override
@JsonSetter("prompt")
public _FinalStage prompt(String prompt) {
this.prompt = prompt;
public _FinalStage prompt(@NotNull String prompt) {
this.prompt = Objects.requireNonNull(prompt, "prompt must not be null");
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import org.jetbrains.annotations.NotNull;

@JsonInclude(JsonInclude.Include.NON_ABSENT)
@JsonDeserialize(builder = LemurActionItemsResponse.Builder.class)
Expand Down Expand Up @@ -91,17 +92,17 @@ public static ResponseStage builder() {
}

public interface ResponseStage {
RequestIdStage response(String response);
RequestIdStage response(@NotNull String response);

Builder from(LemurActionItemsResponse other);
}

public interface RequestIdStage {
UsageStage requestId(String requestId);
UsageStage requestId(@NotNull String requestId);
}

public interface UsageStage {
_FinalStage usage(LemurUsage usage);
_FinalStage usage(@NotNull LemurUsage usage);
}

public interface _FinalStage {
Expand Down Expand Up @@ -135,8 +136,8 @@ public Builder from(LemurActionItemsResponse other) {
*/
@java.lang.Override
@JsonSetter("response")
public RequestIdStage response(String response) {
this.response = response;
public RequestIdStage response(@NotNull String response) {
this.response = Objects.requireNonNull(response, "response must not be null");
return this;
}

Expand All @@ -146,8 +147,8 @@ public RequestIdStage response(String response) {
*/
@java.lang.Override
@JsonSetter("request_id")
public UsageStage requestId(String requestId) {
this.requestId = requestId;
public UsageStage requestId(@NotNull String requestId) {
this.requestId = Objects.requireNonNull(requestId, "requestId must not be null");
return this;
}

Expand All @@ -157,8 +158,8 @@ public UsageStage requestId(String requestId) {
*/
@java.lang.Override
@JsonSetter("usage")
public _FinalStage usage(LemurUsage usage) {
this.usage = usage;
public _FinalStage usage(@NotNull LemurUsage usage) {
this.usage = Objects.requireNonNull(usage, "usage must not be null");
return this;
}

Expand Down
Loading
Loading