Skip to content

Commit 14aae2d

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

File tree

17 files changed

+1296
-135
lines changed

17 files changed

+1296
-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: 247 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,268 @@
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;
11+
import tools.jackson.databind.annotation.JsonDeserialize;
12+
import tools.jackson.databind.annotation.JsonPOJOBuilder;
1013

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

14-
@JsonProperty("doctags_content") @Nullable String doctagsContent,
25+
/**
26+
* Retrieves the filename associated with the document.
27+
*
28+
* @return the filename of the document as a string
29+
*/
30+
String filename();
1531

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

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

20-
@JsonProperty("json_content") @Nullable Map<String, Object> jsonContent,
47+
/**
48+
* Retrieves the Markdown content associated with the document, if available.
49+
*
50+
* @return the Markdown content as a string, or null if no Markdown content is present
51+
*/
52+
@Nullable
53+
String markdownContent();
2154

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

24-
@JsonProperty("text_content") @Nullable String textContent
63+
/**
64+
* Creates a new {@code Builder} instance initialized with the current state of the {@code DocumentResponse}.
65+
*
66+
* @return a {@code Builder} instance populated with the values from this {@code DocumentResponse}
67+
*/
68+
default Builder toBuilder() {
69+
return new Builder(this);
70+
}
71+
72+
/**
73+
* Creates and returns a new instance of the {@code Builder} class, which can be used to
74+
* construct a {@code DocumentResponse} object in a step-by-step manner.
75+
*
76+
* @return a new {@code Builder} instance
77+
*/
78+
static Builder builder() {
79+
return new Builder();
80+
}
2581

26-
) {
82+
/**
83+
* Default implementation of the {@link DocumentResponse} interface.
84+
* This record represents the response containing document data in various formats.
85+
* It is an immutable data structure that consolidates information related to a document,
86+
* such as its filename, content in multiple formats, and metadata.
87+
*
88+
* Each instance ensures the provided JSON content is unmodifiable by copying
89+
* the input map if it is present, or initializing it to an empty map otherwise.
90+
*/
91+
record DefaultDocumentResponse(String doctagsContent,
92+
String filename,
93+
String htmlContent,
94+
Map<String, Object> jsonContent,
95+
String markdownContent,
96+
String textContent) implements DocumentResponse {
2797

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

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

0 commit comments

Comments
 (0)