GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing github.com — chart-api/examples/java/ChartAiExample.java at main · chartquery/chart-api Expand file tree/ Copy pathChartAiExample.javaMore file actions 49 lines (44 loc) · 2.12 KB / Copy pathChartAiExample.javaFile metadata and controls49 lines (44 loc) · 2.12 KB 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 // AI Chart API – Java example (Java 15+) // Usage: CHARTQUERY_API_KEY=your-key java ChartAiExample.java import java.net.URI; import java.net.URLEncoder; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.time.Duration; import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; public class ChartAiExample { static final HttpClient CLIENT = HttpClient.newHttpClient(); static final String KEY = System.getenv("CHARTQUERY_API_KEY"); static String enc(String v) { return URLEncoder.encode(v, StandardCharsets.UTF_8); } static HttpResponse<byte[]> call(String method, String url, String body) throws Exception { HttpRequest.Builder b = HttpRequest.newBuilder().uri(URI.create(url)).header("x-api-key", KEY) .timeout(Duration.ofSeconds(120)); b = body == null ? b.method(method, HttpRequest.BodyPublishers.noBody()) : b.header("Content-Type", "application/json").method(method, HttpRequest.BodyPublishers.ofString(body)); HttpResponse<byte[]> r = CLIENT.send(b.build(), HttpResponse.BodyHandlers.ofByteArray()); if (r.statusCode() >= 400) { System.err.println("HTTP " + r.statusCode() + ": " + new String(r.body(), StandardCharsets.UTF_8)); System.exit(1); } return r; } static String field(String json, String name) { Matcher m = Pattern.compile("\"" + name + "\"\\s*:\\s*\"([^\"]*)\"").matcher(json); return m.find() ? m.group(1) : ""; } public static void main(String[] args) throws Exception { String query = "prompt=" + enc("Bar chart of monthly sales from January to June, indigo bars, clean style") + "&" + "format=" + enc("png"); HttpResponse<byte[]> response = call("GET", "https://api.chartquery.com/v1/chart/ai" + (query.isEmpty() ? "" : "?" + query), null); Files.write(Path.of("ai-chart.png"), response.body()); System.out.println("Saved ai-chart.png"); } } |
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |