Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion .github/workflows/pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,4 @@ jobs:
java-version: 17
- name: Run unit tests
run: |
cd ultraocr
mvn -B package --file pom.xml
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,12 @@ With the job or batch id, you can get the job result or batch status with:
BatchStatusResponse response = client.getBatchStatus("BATCH_ID"); // Batches
JobResultResponse jobResponse = client.getJobResult("JOB_ID", "JOB_ID"); // Simple jobs
JobResultResponse jobResponse2 = client.getJobResult("BATCH_ID", "JOB_ID"); // Jobs belonging to batches
List<BatchResultJob> batchResult = client.getBatchResult("BATCH_ID"); // Get batch jobs result as array
BatchResultStorageResponse storage = client.getBatchResultStorage("BATCH_ID", params); // Get batch jobs result in a file

// More details about job and batch
BatchInfoResponse batchInfo = client.getBatchInfo("BATCH_ID"); // Batches info (without jobs info)
JobInfoResponse jobInfo = client.getJobInfo("JOB_ID"); // Jobs info (single jobs only)
```

Alternatively, you can use a utily `waitForJobDone` or `waitForBatchDone`:
Expand Down
91 changes: 91 additions & 0 deletions src/main/java/com/nuveo/ultraocr/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.util.ArrayList;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

/**
* Client to help on UltraOCR usage. For more details about all arguments and
Expand Down Expand Up @@ -261,6 +262,15 @@ private HttpResponse<String> get(String url, Map<String, String> params)
return this.httpClient.send(request, BodyHandlers.ofString());
}

private String getBatchResult(String batchKsuid, Map<String, String> params)
throws IOException, InterruptedException, InvalidStatusCodeException {
String url = String.format("%s/ocr/batch/result/%s", this.baseUrl, batchKsuid);
HttpResponse<String> response = this.get(url, params);
validateStatus(Constants.STATUS_OK, response.statusCode());

return response.body();
}

/**
* Generate signed url to send the document.
*
Expand Down Expand Up @@ -816,4 +826,85 @@ public List<JobResultResponse> getJobs(String start, String end)

return jobs;
}

/**
* Get the job info with more details.
*
* @param jobKsuid the id of the job, given on job creation.
* @return the job with infos.
* @see JobInfoResponse
* @throws InvalidStatusCodeException if status code is not 200.
* @throws InterruptedException if http request fail.
* @throws IOException if http request fail.
*/
public JobInfoResponse getJobInfo(String jobKsuid)
throws IOException, InterruptedException, InvalidStatusCodeException {
String url = String.format("%s/ocr/job/info/%s", this.baseUrl, jobKsuid);
HttpResponse<String> response = this.get(url, new HashMap<>());
validateStatus(Constants.STATUS_OK, response.statusCode());

Gson gson = new Gson();
return gson.fromJson(response.body(), JobInfoResponse.class);
}

/**
* Get the infos of the batch with more details.
*
* @param batchKsuid the id of the batch, given on batch creation.
* @return the batch with infos.
* @see BatchInfoResponse
* @throws InvalidStatusCodeException if status code is not 200.
* @throws InterruptedException if http request fail.
* @throws IOException if http request fail.
*/
public BatchInfoResponse getBatchInfo(String batchKsuid)
throws IOException, InterruptedException, InvalidStatusCodeException {
String url = String.format("%s/ocr/batch/info/%s", this.baseUrl, batchKsuid);
HttpResponse<String> response = this.get(url, new HashMap<>());
validateStatus(Constants.STATUS_OK, response.statusCode());

Gson gson = new Gson();
return gson.fromJson(response.body(), BatchInfoResponse.class);
}

/**
* Get the batch jobs results as array.
*
* @param batchKsuid the id of the batch, given on batch creation.
* @return the batch jobs results.
* @see List<BatchResultJob>
* @throws InvalidStatusCodeException if status code is not 200.
* @throws InterruptedException if http request fail.
* @throws IOException if http request fail.
*/
public List<BatchResultJob> getBatchResult(String batchKsuid)
throws IOException, InterruptedException, InvalidStatusCodeException {
Map<String, String> params = new HashMap<>();
params.put(Constants.RETURN_ATTRIBUTE, Constants.RETURN_REQUEST);
String body = this.getBatchResult(batchKsuid, params);

Gson gson = new Gson();
return gson.fromJson(body, new TypeToken<List<BatchResultJob>>() {
}.getType());
}

/**
* Generate url to download a file containing the batch jobs results.
*
* @param batchKsuid the id of the batch, given on batch creation.
* @param params the query parameters based on UltraOCR Docs.
* @return the url to download result file.
* @see BatchResultStorageResponse
* @throws InvalidStatusCodeException if status code is not 200.
* @throws InterruptedException if http request fail.
* @throws IOException if http request fail.
*/
public BatchResultStorageResponse getBatchResultStorage(String batchKsuid, Map<String, String> params)
throws IOException, InterruptedException, InvalidStatusCodeException {
params.put(Constants.RETURN_ATTRIBUTE, Constants.RETURN_STORAGE);
String body = this.getBatchResult(batchKsuid, params);

Gson gson = new Gson();
return gson.fromJson(body, BatchResultStorageResponse.class);
}
}
3 changes: 3 additions & 0 deletions src/main/java/com/nuveo/ultraocr/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ public class Constants {
public static final String KEY_EXTRA_URL = "extra_document";
public static final String FLAG_TRUE = "true";
public static final String BASE64_ATTRIBUTE = "base64";
public static final String RETURN_ATTRIBUTE = "return";
public static final String RETURN_REQUEST = "request";
public static final String RETURN_STORAGE = "storage";

private Constants() {

Expand Down
121 changes: 121 additions & 0 deletions src/main/java/com/nuveo/ultraocr/responses/BatchInfoResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
package com.nuveo.ultraocr.responses;

import com.google.gson.annotations.SerializedName;

import java.util.List;

public class BatchInfoResponse {
@SerializedName("batch_id")
private String batchId;

@SerializedName("client_id")
private String clientId;

@SerializedName("company_id")
private String companyId;

@SerializedName("validation_id")
private String validationId;

@SerializedName("created_at")
private String createdAt;

@SerializedName("total_jobs")
private int totalJobs;

@SerializedName("total_processed")
private int totalProcessed;

private String service;
private String status;
private String error;
private String source;

public String getBatchId() {
return batchId;
}

public void setBatchId(String batchId) {
this.batchId = batchId;
}

public String getClientId() {
return clientId;
}

public void setClientId(String clientId) {
this.clientId = clientId;
}

public String getCompanyId() {
return companyId;
}

public void setCompanyId(String companyId) {
this.companyId = companyId;
}

public String getValidationId() {
return validationId;
}

public void setValidationId(String validationId) {
this.validationId = validationId;
}

public String getCreatedAt() {
return createdAt;
}

public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}

public int getTotalJobs() {
return totalJobs;
}

public void setTotalJobs(int totalJobs) {
this.totalJobs = totalJobs;
}

public int getTotalProcessed() {
return totalProcessed;
}

public void setTotalProcessed(int totalProcessed) {
this.totalProcessed = totalProcessed;
}

public String getService() {
return service;
}

public void setService(String service) {
this.service = service;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

public String getSource() {
return source;
}

public void setSource(String source) {
this.source = source;
}
}
104 changes: 104 additions & 0 deletions src/main/java/com/nuveo/ultraocr/responses/BatchResultJob.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package com.nuveo.ultraocr.responses;

import com.google.gson.annotations.SerializedName;

public class BatchResultJob {
@SerializedName("job_ksuid")
private String jobKsuid;

@SerializedName("created_at")
private String createdAt;

@SerializedName("validation_status")
private String validationStatus;

@SerializedName("client_data")
private Object clientData;

private String service;
private String status;
private String error;
private String filename;
private Object validation;
private Result result;

public String getJobKsuid() {
return jobKsuid;
}

public void setJobKsuid(String jobKsuid) {
this.jobKsuid = jobKsuid;
}

public String getCreatedAt() {
return createdAt;
}

public void setCreatedAt(String createdAt) {
this.createdAt = createdAt;
}

public String getValidationStatus() {
return validationStatus;
}

public void setValidationStatus(String validationStatus) {
this.validationStatus = validationStatus;
}

public Object getClientData() {
return clientData;
}

public void setClientData(Object clientData) {
this.clientData = clientData;
}

public String getService() {
return service;
}

public void setService(String service) {
this.service = service;
}

public String getStatus() {
return status;
}

public void setStatus(String status) {
this.status = status;
}

public String getError() {
return error;
}

public void setError(String error) {
this.error = error;
}

public String getFilename() {
return filename;
}

public void setFilename(String filename) {
this.filename = filename;
}

public Object getValidation() {
return validation;
}

public void setValidation(Object validation) {
this.validation = validation;
}

public Result getResult() {
return result;
}

public void setResult(Result result) {
this.result = result;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.nuveo.ultraocr.responses;

import com.google.gson.annotations.SerializedName;

import java.util.List;

public class BatchResultStorageResponse {
private String exp;
private String url;

public String getExp() {
return exp;
}

public void setExp(String exp) {
this.exp = exp;
}

public String getUrl() {
return url;
}

public void setUrl(String url) {
this.url = url;
}
}
Loading