Skip to content

Commit

Permalink
Adds a custom task for downloading the API
Browse files Browse the repository at this point in the history
  • Loading branch information
fedefernandez committed Nov 13, 2023
1 parent bd5d7da commit 06e925f
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
6 changes: 6 additions & 0 deletions openai-client/generator/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
1 change: 1 addition & 0 deletions openai-client/generator/config/openai-api-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
5c1857ea865e74e45e3b12064e0cc2396ef64be1
Original file line number Diff line number Diff line change
@@ -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);
}
}
}

0 comments on commit 06e925f

Please sign in to comment.