diff --git a/openai-client/generator/build.gradle.kts b/openai-client/generator/build.gradle.kts index 40aa5ffae..e5b07caa1 100644 --- a/openai-client/generator/build.gradle.kts +++ b/openai-client/generator/build.gradle.kts @@ -13,6 +13,12 @@ tasks.test { useJUnitPlatform() } +task("downloadOpenAIAPI", JavaExec::class) { + group = "GenerateTasks" + mainClass = "ai.xef.openai.generator.DownloadOpenAIAPI" + classpath = sourceSets["main"].runtimeClasspath +} + task("openaiClientGenerate", JavaExec::class) { group = "GenerateTasks" mainClass = "org.openapitools.codegen.OpenAPIGenerator" diff --git a/openai-client/generator/config/openai-api-commit b/openai-client/generator/config/openai-api-commit new file mode 100644 index 000000000..1360b8ffa --- /dev/null +++ b/openai-client/generator/config/openai-api-commit @@ -0,0 +1 @@ +5c1857ea865e74e45e3b12064e0cc2396ef64be1 \ No newline at end of file diff --git a/openai-client/generator/src/main/java/ai/xef/openai/generator/DownloadOpenAIAPI.java b/openai-client/generator/src/main/java/ai/xef/openai/generator/DownloadOpenAIAPI.java new file mode 100644 index 000000000..ef8cdb5b2 --- /dev/null +++ b/openai-client/generator/src/main/java/ai/xef/openai/generator/DownloadOpenAIAPI.java @@ -0,0 +1,38 @@ +package ai.xef.openai.generator; + +import java.io.*; +import java.net.URL; +import java.nio.channels.Channels; +import java.nio.channels.FileChannel; +import java.nio.channels.ReadableByteChannel; + +public class DownloadOpenAIAPI { + public static void main(String[] args) { + try { + String commit = readCommit(); + downloadAPI(commit); + } catch (IOException e) { + throw new RuntimeException(e); + } + } + + private static String readCommit() throws IOException { + StringBuilder resultStringBuilder = new StringBuilder(); + try (BufferedReader br = new BufferedReader(new FileReader("config/openai-api-commit"))) { + String line; + while ((line = br.readLine()) != null) { + resultStringBuilder.append(line).append("\n"); + } + } + return resultStringBuilder.toString().trim(); + } + + private static void downloadAPI(String commit) throws IOException { + URL url = new URL("https://raw.githubusercontent.com/openai/openai-openapi/%s/openapi.yaml".formatted(commit)); + ReadableByteChannel readableByteChannel = Channels.newChannel(url.openStream()); + try (FileOutputStream fileOutputStream = new FileOutputStream("config/openai-api.yaml")) { + FileChannel fileChannel = fileOutputStream.getChannel(); + fileChannel.transferFrom(readableByteChannel, 0, Long.MAX_VALUE); + } + } +}