|
| 1 | +// Copyright 2025 SIXT SE |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package tensorlake |
| 16 | + |
| 17 | +import ( |
| 18 | + "bytes" |
| 19 | + "context" |
| 20 | + "encoding/json" |
| 21 | + "fmt" |
| 22 | + "io" |
| 23 | + "log/slog" |
| 24 | + "net/http" |
| 25 | +) |
| 26 | + |
| 27 | +type ParseDocumentRequest struct { |
| 28 | + FileSource |
| 29 | + |
| 30 | + // ParsingOptions contains the properties of this object define |
| 31 | + // the configuration for the document parsing process. |
| 32 | + // |
| 33 | + // Tensorlake provides sane defaults that work well for most |
| 34 | + // documents, so this object is not required. However, every document |
| 35 | + // is different, and you may want to customize the parsing process to |
| 36 | + // better suit your needs. |
| 37 | + ParsingOptions *ParsingOptions `json:"parsing_options,omitempty"` |
| 38 | + |
| 39 | + // The properties of this object help to extend the output of the document |
| 40 | + // parsing process with additional information. |
| 41 | + // |
| 42 | + // This includes summarization of tables and figures, which can help to |
| 43 | + // provide a more comprehensive understanding of the document. |
| 44 | + // |
| 45 | + // This object is not required, and the API will use default settings if it |
| 46 | + // is not present. |
| 47 | + EnrichmentOptions *EnrichmentOptions `json:"enrichment_options,omitempty"` |
| 48 | + |
| 49 | + // StructuredExtractionOptions is the options for structured data extraction. |
| 50 | + // |
| 51 | + // The properties of this object define the configuration for structured |
| 52 | + // data extraction. |
| 53 | + // |
| 54 | + // If this object is present, the API will perform structured data |
| 55 | + // extraction on the document. |
| 56 | + StructuredExtractionOptions []StructuredExtractionOptions `json:"structured_extraction_options,omitempty"` |
| 57 | + |
| 58 | + // PageClassificationOptions is the options for page classification. |
| 59 | + // |
| 60 | + // The properties of this object define the configuration for page |
| 61 | + // classify. |
| 62 | + // |
| 63 | + // If this object is present, the API will perform page classify on |
| 64 | + // the document. |
| 65 | + PageClassificationOptions []PageClassConfig `json:"page_classifications,omitempty"` |
| 66 | + |
| 67 | + // PageRange is a comma-separated list of page numbers or |
| 68 | + // ranges to parse (e.g., '1,2,3-5'). Default: all pages. |
| 69 | + // Examples: "1-5,8,10" |
| 70 | + PageRange string `json:"page_range,omitempty"` |
| 71 | + |
| 72 | + // Additional metadata to identify the read request. The labels are |
| 73 | + // returned in the read response. |
| 74 | + Labels map[string]string `json:"labels,omitempty"` |
| 75 | + |
| 76 | + // MimeType is the MIME type of the file. This is used to determine how to process the file. |
| 77 | + MimeType MimeType `json:"mime_type,omitempty"` |
| 78 | +} |
| 79 | + |
| 80 | +// ParseDocumentResponse represents the response from the ParseDocument operation. |
| 81 | +// |
| 82 | +// ParseId is the unique identifier for the parse job. |
| 83 | +// CreatedAt is the creation date and time of the parse job. |
| 84 | +type ParseDocumentResponse struct { |
| 85 | + // ParseId is the unique identifier for the parse job. |
| 86 | + // This is the ID that can be used to track the status of the parse job. |
| 87 | + // Used in the GET /documents/v2/parse/{parse_id} endpoint to retrieve |
| 88 | + // the status and results of the parse job. |
| 89 | + ParseId string `json:"parse_id"` |
| 90 | + // CreatedAt is the creation date and time of the parse job. |
| 91 | + CreatedAt string `json:"created_at"` |
| 92 | +} |
| 93 | + |
| 94 | +// ParseDocument submits a document for comprehensive parsing (read, extract, and classify). |
| 95 | +func (c *Client) ParseDocument(ctx context.Context, in *ParseDocumentRequest) (*ParseDocumentResponse, error) { |
| 96 | + if !in.SourceProvided() { |
| 97 | + return nil, fmt.Errorf("exactly one of file_id, file_url, or raw_text must be provided") |
| 98 | + } |
| 99 | + |
| 100 | + body, _ := json.Marshal(in) // Impossible to fail? |
| 101 | + |
| 102 | + slog.Info("ParseDocument request", "request", string(body)) |
| 103 | + |
| 104 | + req, err := http.NewRequestWithContext(ctx, http.MethodPost, c.baseURL+"/parse", bytes.NewReader(body)) |
| 105 | + if err != nil { |
| 106 | + return nil, fmt.Errorf("failed to create request: %w", err) |
| 107 | + } |
| 108 | + |
| 109 | + return do(c, req, func(r io.Reader) (*ParseDocumentResponse, error) { |
| 110 | + var result ParseDocumentResponse |
| 111 | + if err := json.NewDecoder(r).Decode(&result); err != nil { |
| 112 | + return nil, fmt.Errorf("failed to decode response: %w", err) |
| 113 | + } |
| 114 | + return &result, nil |
| 115 | + }) |
| 116 | +} |
0 commit comments