From 028454e30692922bb7d4eeceee97365b3b4b53ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fede=20Fern=C3=A1ndez?= <720923+fedefernandez@users.noreply.github.com> Date: Fri, 10 Nov 2023 22:24:13 +0100 Subject: [PATCH] Adds a custom task for downloading the API --- openai-client/generator/build.gradle.kts | 6 +++ .../generator/config/openai-api-commit | 1 + .../openai/generator/DownloadOpenAIAPI.java | 38 +++++++++++++++++++ 3 files changed, 45 insertions(+) create mode 100644 openai-client/generator/config/openai-api-commit create mode 100644 openai-client/generator/src/main/java/ai/xef/openai/generator/DownloadOpenAIAPI.java 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); + } + } +}