Skip to content

Commit 945e7bd

Browse files
committed
feat: idea for api design
Signed-off-by: Eric Deandrea <[email protected]>
1 parent aee1e92 commit 945e7bd

File tree

17 files changed

+1305
-135
lines changed

17 files changed

+1305
-135
lines changed

api/build.gradle.kts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ dependencies {
66
implementation(platform(libs.jackson.bom))
77

88
implementation("com.fasterxml.jackson.core:jackson-annotations")
9+
implementation("tools.jackson.core:jackson-databind")
910
testImplementation(project(":docling-testcontainers"))
1011
}

api/src/main/java/ai/docling/api/DoclingApi.java

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,44 @@
99
*/
1010
public interface DoclingApi {
1111

12+
/**
13+
* Executes a health check for the API and retrieves the health status of the service.
14+
*
15+
* @return a {@link HealthCheckResponse} object containing the health status of the API.
16+
*/
1217
HealthCheckResponse health();
1318

19+
/**
20+
* Converts the provided document source(s) into a processed document based on the specified options.
21+
*
22+
* @param request the {@link ConvertDocumentRequest} containing the source(s), conversion options, and optional target.
23+
* @return a {@link ConvertDocumentResponse} containing the processed document data, processing details, and any errors.
24+
*/
1425
ConvertDocumentResponse convertSource(ConvertDocumentRequest request);
1526

27+
/**
28+
* Creates and returns a builder instance capable of constructing a duplicate or modified
29+
* version of the current API instance. The builder provides a customizable way to adjust
30+
* configuration or properties before constructing a new API instance.
31+
*
32+
* @return a {@link DoclingApiBuilder} initialized with the state of the current API instance.
33+
*/
34+
<T extends DoclingApi, B extends DoclingApiBuilder<T, B>> DoclingApiBuilder<T, B> toBuilder();
35+
36+
/**
37+
* A builder interface for constructing implementations of {@link DoclingApi}. This interface
38+
* supports a fluent API for setting configuration properties before building an instance.
39+
*
40+
* @param <T> the type of the {@link DoclingApi} implementation being built.
41+
* @param <B> the type of the concrete builder implementation.
42+
*/
43+
interface DoclingApiBuilder<T extends DoclingApi, B extends DoclingApiBuilder<T, B>> {
44+
/**
45+
* Builds and returns an instance of the specified type, representing the completed configuration
46+
* of the builder. The returned instance is typically an implementation of the Docling API.
47+
*
48+
* @return an instance of type {@code T} representing a configured Docling API client.
49+
*/
50+
T build();
51+
}
1652
}

api/src/main/java/ai/docling/api/convert/response/DocumentResponse.java

Lines changed: 248 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,269 @@
22

33
import java.util.HashMap;
44
import java.util.Map;
5+
import java.util.Optional;
56

67
import org.jspecify.annotations.Nullable;
78

89
import com.fasterxml.jackson.annotation.JsonInclude;
910
import com.fasterxml.jackson.annotation.JsonProperty;
1011

11-
@JsonInclude(JsonInclude.Include.NON_NULL)
12-
public record DocumentResponse(
12+
import tools.jackson.databind.annotation.JsonDeserialize;
13+
import tools.jackson.databind.annotation.JsonPOJOBuilder;
1314

14-
@JsonProperty("doctags_content") @Nullable String doctagsContent,
15+
@JsonInclude(JsonInclude.Include.NON_ABSENT)
16+
@JsonDeserialize(builder = DocumentResponse.Builder.class)
17+
public interface DocumentResponse {
18+
/**
19+
* Retrieves the content of the doc tags, if available.
20+
*
21+
* @return the content of the doc tags, or null if not present
22+
*/
23+
@Nullable
24+
String doctagsContent();
1525

16-
@JsonProperty("filename") String filename,
26+
/**
27+
* Retrieves the filename associated with the document.
28+
*
29+
* @return the filename of the document as a string
30+
*/
31+
String filename();
1732

18-
@JsonProperty("html_content") @Nullable String htmlContent,
33+
/**
34+
* Retrieves the HTML content associated with the document, if available.
35+
*
36+
* @return the HTML content as a string, or null if not present
37+
*/
38+
@Nullable
39+
String htmlContent();
1940

20-
@JsonProperty("json_content") @Nullable Map<String, Object> jsonContent,
41+
/**
42+
* Retrieves the JSON content associated with the document.
43+
*
44+
* @return a map representing the JSON content, or an empty map if no JSON content is present
45+
*/
46+
Map<String, Object> jsonContent();
2147

22-
@JsonProperty("md_content") @Nullable String markdownContent,
48+
/**
49+
* Retrieves the Markdown content associated with the document, if available.
50+
*
51+
* @return the Markdown content as a string, or null if no Markdown content is present
52+
*/
53+
@Nullable
54+
String markdownContent();
2355

24-
@JsonProperty("text_content") @Nullable String textContent
56+
/**
57+
* Retrieves the plain text content associated with the document, if available.
58+
*
59+
* @return the plain text content as a string, or null if no text content is present
60+
*/
61+
@Nullable
62+
String textContent();
2563

26-
) {
64+
/**
65+
* Creates a new {@code Builder} instance initialized with the current state of the {@code DocumentResponse}.
66+
*
67+
* @return a {@code Builder} instance populated with the values from this {@code DocumentResponse}
68+
*/
69+
default Builder toBuilder() {
70+
return new Builder(this);
71+
}
72+
73+
/**
74+
* Creates and returns a new instance of the {@code Builder} class, which can be used to
75+
* construct a {@code DocumentResponse} object in a step-by-step manner.
76+
*
77+
* @return a new {@code Builder} instance
78+
*/
79+
static Builder builder() {
80+
return new Builder();
81+
}
82+
83+
/**
84+
* Default implementation of the {@link DocumentResponse} interface.
85+
* This record represents the response containing document data in various formats.
86+
* It is an immutable data structure that consolidates information related to a document,
87+
* such as its filename, content in multiple formats, and metadata.
88+
*
89+
* Each instance ensures the provided JSON content is unmodifiable by copying
90+
* the input map if it is present, or initializing it to an empty map otherwise.
91+
*/
92+
record DefaultDocumentResponse(String doctagsContent,
93+
String filename,
94+
String htmlContent,
95+
Map<String, Object> jsonContent,
96+
String markdownContent,
97+
String textContent) implements DocumentResponse {
2798

28-
public DocumentResponse {
29-
if (jsonContent != null) {
30-
jsonContent = new HashMap<>(jsonContent);
99+
public DefaultDocumentResponse {
100+
jsonContent = Optional.ofNullable(jsonContent)
101+
.map(Map::copyOf)
102+
.orElseGet(Map::of);
103+
}
104+
105+
public DefaultDocumentResponse(Builder builder) {
106+
this(builder.doctagsContent,
107+
builder.filename,
108+
builder.htmlContent,
109+
builder.jsonContent,
110+
builder.markdownContent,
111+
builder.textContent);
31112
}
32113
}
33114

115+
/**
116+
* A builder class for constructing instances of {@code DocumentResponse}.
117+
*
118+
* This class provides a step-by-step approach to configure and create a
119+
* {@code DocumentResponse} object. Each method in this class sets a specific
120+
* property of the object being built. Once all the desired properties are set,
121+
* the {@code build} method is used to create the final {@code DocumentResponse}
122+
* instance.
123+
*
124+
* The builder supports customization of various document-related attributes,
125+
* including doc tags content, filename, HTML content, JSON content, Markdown
126+
* content, and plain text content.
127+
*
128+
* By default, the builder initializes attributes with an empty state or default
129+
* values. If a {@code DocumentResponse} instance is provided to the constructor,
130+
* the builder is pre-populated with the attributes from the given response.
131+
*
132+
* This class is intended for internal use and is protected to restrict its
133+
* accessibility outside the defining package or class hierarchy.
134+
*/
135+
@JsonPOJOBuilder(withPrefix = "")
136+
class Builder {
137+
protected String doctagsContent;
138+
protected String filename;
139+
protected String htmlContent;
140+
protected Map<String, Object> jsonContent = new HashMap<>();
141+
protected String markdownContent;
142+
protected String textContent;
143+
144+
/**
145+
* Constructs a new {@code Builder} instance.
146+
*
147+
* This constructor initializes a builder with default or empty states for all
148+
* attributes. It is protected to restrict direct instantiation outside of the
149+
* defining package or class hierarchy.
150+
*
151+
* The {@code Builder} class is primarily used to facilitate the creation of
152+
* {@code DocumentResponse} objects through a step-by-step configuration process.
153+
*/
154+
protected Builder() {
155+
156+
}
157+
158+
/**
159+
* Constructs a new {@code Builder} instance using the provided {@code DocumentResponse}.
160+
*
161+
* This constructor initializes the builder's fields with the data from the given
162+
* {@code DocumentResponse} object. It allows for the creation of a {@code Builder}
163+
* instance pre-populated with the state of an existing {@code DocumentResponse}.
164+
*
165+
* @param documentResponse the {@code DocumentResponse} instance whose data will
166+
* populate the fields of this builder
167+
*/
168+
protected Builder(DocumentResponse documentResponse) {
169+
this.doctagsContent = documentResponse.doctagsContent();
170+
this.filename = documentResponse.filename();
171+
this.htmlContent = documentResponse.htmlContent();
172+
this.jsonContent = documentResponse.jsonContent();
173+
this.markdownContent = documentResponse.markdownContent();
174+
this.textContent = documentResponse.textContent();
175+
}
176+
177+
/**
178+
* Sets the doctags content for the builder instance.
179+
*
180+
* @param doctagsContent the doctags content to be set
181+
* @return this Builder instance for method chaining
182+
*/
183+
@JsonProperty("doctags_content")
184+
public Builder doctagsContent(String doctagsContent) {
185+
this.doctagsContent = doctagsContent;
186+
return this;
187+
}
188+
189+
/**
190+
* Sets the filename for the builder instance.
191+
*
192+
* @param filename the filename to be set
193+
* @return this Builder instance for method chaining
194+
*/
195+
@JsonProperty("filename")
196+
public Builder filename(String filename) {
197+
this.filename = filename;
198+
return this;
199+
}
200+
201+
/**
202+
* Sets the HTML content for the builder instance.
203+
*
204+
* @param htmlContent the HTML content to be set
205+
* @return this Builder instance for method chaining
206+
*/
207+
@JsonProperty("html_content")
208+
public Builder htmlContent(String htmlContent) {
209+
this.htmlContent = htmlContent;
210+
return this;
211+
}
212+
213+
/**
214+
* Sets the JSON content for the builder instance.
215+
*
216+
* The JSON content is represented as a map of key-value pairs, where the keys
217+
* are {@code String} objects, and the values are {@code Object} instances.
218+
*
219+
* @param jsonContent the JSON content to be set, represented as a {@code Map<String, Object>}
220+
* @return this {@link Builder} instance for method chaining
221+
*/
222+
@JsonProperty("json_content")
223+
public Builder jsonContent(Map<String, Object> jsonContent) {
224+
this.jsonContent = jsonContent;
225+
return this;
226+
}
227+
228+
/**
229+
* Sets the Markdown content for this builder instance.
230+
*
231+
* The Markdown content represents the textual data formatted in Markdown syntax,
232+
* which can include headings, lists, links, and other Markdown elements.
233+
*
234+
* @param markdownContent the Markdown content to be set, represented as a {@code String}
235+
* @return this {@link Builder} instance for method chaining
236+
*/
237+
@JsonProperty("md_content")
238+
public Builder markdownContent(String markdownContent) {
239+
this.markdownContent = markdownContent;
240+
return this;
241+
}
242+
243+
/**
244+
* Sets the plain text content for this builder instance.
245+
*
246+
* The plain text content represents unformatted textual data that can be
247+
* used for display or processing purposes within the application.
248+
*
249+
* @param textContent the plain text content to be set, represented as a {@code String}
250+
* @return this {@link Builder} instance for method chaining
251+
*/
252+
@JsonProperty("text_content")
253+
public Builder textContent(String textContent) {
254+
this.textContent = textContent;
255+
return this;
256+
}
257+
258+
/**
259+
* Creates and returns a {@link DocumentResponse} instance based on the current state of this {@link Builder}.
260+
*
261+
* <p>The returned {@link DocumentResponse} will encapsulate the values configured in the builder,
262+
* and further modifications to the builder instance will not affect the created {@code DocumentResponse}.
263+
*
264+
* @return a new {@code DocumentResponse} instance constructed from the builder's state
265+
*/
266+
public DocumentResponse build() {
267+
return new DefaultDocumentResponse(this);
268+
}
269+
}
34270
}

0 commit comments

Comments
 (0)