oscerd commented on code in PR #19567:
URL: https://github.com/apache/camel/pull/19567#discussion_r2431424842
##########
components/camel-ai/camel-docling/src/main/java/org/apache/camel/component/docling/DoclingServeClient.java:
##########
@@ -267,6 +279,282 @@ private void applyAuthentication(HttpPost httpPost) {
}
}
+ /**
+ * Apply authentication headers to the HTTP GET request based on the
configured authentication scheme.
+ *
+ * @param httpGet The HTTP GET request to add authentication to
+ */
+ private void applyAuthenticationGet(HttpGet httpGet) {
+ if (authenticationScheme == null || authenticationScheme ==
AuthenticationScheme.NONE) {
+ return;
+ }
+
+ if (authenticationToken == null || authenticationToken.isEmpty()) {
+ LOG.warn("Authentication scheme is set to {} but no authentication
token provided", authenticationScheme);
+ return;
+ }
+
+ switch (authenticationScheme) {
+ case BEARER:
+ httpGet.setHeader("Authorization", "Bearer " +
authenticationToken);
+ LOG.debug("Applied Bearer token authentication");
+ break;
+ case API_KEY:
+ httpGet.setHeader(apiKeyHeader, authenticationToken);
+ LOG.debug("Applied API Key authentication with header: {}",
apiKeyHeader);
+ break;
+ default:
+ LOG.warn("Unknown authentication scheme: {}",
authenticationScheme);
+ }
+ }
+
+ /**
+ * Convert a document using the docling-serve async API and return the
task ID.
+ *
+ * @param inputSource File path or URL to the document
+ * @param outputFormat Output format (md, json, html, text)
+ * @return Task ID for the async conversion
+ * @throws IOException If the API call fails
+ */
+ public String convertDocumentAsync(String inputSource, String
outputFormat) throws IOException {
+ LOG.debug("Starting async document conversion using docling-serve API:
{}", inputSource);
+
+ String asyncEndpoint = convertEndpoint.replace("/v1/convert/source",
DEFAULT_ASYNC_CONVERT_ENDPOINT);
+
+ // Check if input is a URL or file path
+ Map<String, Object> requestBody = buildRequestBody(inputSource,
outputFormat);
+
+ String jsonRequest = objectMapper.writeValueAsString(requestBody);
+ LOG.debug("Async request body: {}", jsonRequest);
+
+ HttpPost httpPost = new HttpPost(baseUrl + asyncEndpoint);
+ httpPost.setEntity(new StringEntity(jsonRequest,
ContentType.APPLICATION_JSON));
+ httpPost.setHeader("Accept", "application/json");
+ applyAuthentication(httpPost);
+
+ try (CloseableHttpResponse response = httpClient.execute(httpPost)) {
+ int statusCode = response.getCode();
+ String responseBody;
+ try {
+ responseBody = EntityUtils.toString(response.getEntity());
+ } catch (org.apache.hc.core5.http.ParseException e) {
+ throw new IOException("Failed to parse response from
docling-serve API", e);
+ }
+
+ if (statusCode >= 200 && statusCode < 300) {
+ // Extract task ID from response
+ JsonNode rootNode = objectMapper.readTree(responseBody);
+ if (rootNode.has("task_id")) {
+ return rootNode.get("task_id").asText();
+ } else if (rootNode.has("id")) {
+ return rootNode.get("id").asText();
+ } else {
+ throw new IOException("No task ID found in async
conversion response: " + responseBody);
+ }
+ } else {
+ throw new IOException(
+ "Docling-serve async API request failed with status "
+ statusCode + ": " + responseBody);
+ }
+ }
+ }
+
+ /**
+ * Check the status of an async conversion task.
+ *
+ * @param taskId The task ID returned from convertDocumentAsync
+ * @return ConversionStatus object with current status
+ * @throws IOException If the API call fails
+ */
+ public ConversionStatus checkConversionStatus(String taskId) throws
IOException {
+ LOG.debug("Checking status for task: {}", taskId);
+
+ String statusEndpoint = "/v1/status/poll/" + taskId;
+ HttpGet httpGet = new HttpGet(baseUrl + statusEndpoint);
+ httpGet.setHeader("Accept", "application/json");
+ applyAuthenticationGet(httpGet);
+
+ try (CloseableHttpResponse response = httpClient.execute(httpGet)) {
+ int statusCode = response.getCode();
+ String responseBody;
+ try {
+ responseBody = EntityUtils.toString(response.getEntity());
+ } catch (org.apache.hc.core5.http.ParseException e) {
+ throw new IOException("Failed to parse response from
docling-serve API", e);
+ }
+
+ if (statusCode >= 200 && statusCode < 300) {
+ JsonNode rootNode = objectMapper.readTree(responseBody);
+ return parseConversionStatus(taskId, rootNode);
+ } else {
+ throw new IOException(
+ "Failed to check task status. Status code: " +
statusCode + ", Response: " + responseBody);
+ }
+ }
+ }
+
+ /**
+ * Convert a document asynchronously and wait for completion by polling.
+ *
+ * @param inputSource File path or URL to the document
+ * @param outputFormat Output format (md, json, html, text)
+ * @return Converted document content
+ * @throws IOException If the API call fails or timeout occurs
+ */
+ public String convertDocumentAsyncAndWait(String inputSource, String
outputFormat) throws IOException {
+ String taskId = convertDocumentAsync(inputSource, outputFormat);
+ LOG.debug("Started async conversion with task ID: {}", taskId);
+
+ long startTime = System.currentTimeMillis();
+ long deadline = startTime + asyncTimeout;
+
+ while (System.currentTimeMillis() < deadline) {
Review Comment:
This could probably done a bit better, but as first iteration it makes
sense. We submit the document and check the task status on each
asyncPollInterval
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]