refactor(api): simplify response structures for GetDocuments and GetD… (#199)

* refactor(api): simplify response structures for GetDocuments and GetDocument endpoints

Closes #198

---------

Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
This commit is contained in:
Icereed 2025-02-07 16:47:40 +01:00 committed by GitHub
parent 712ed53c1c
commit abeab949a1
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 55 additions and 76 deletions

View file

@ -9,7 +9,6 @@ import (
"net/http/httptest" "net/http/httptest"
"os" "os"
"testing" "testing"
"time"
"github.com/stretchr/testify/assert" "github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -180,31 +179,7 @@ func TestGetDocumentsByTags(t *testing.T) {
// Mock data for documents // Mock data for documents
documentsResponse := GetDocumentsApiResponse{ documentsResponse := GetDocumentsApiResponse{
Results: []struct { Results: []GetDocumentApiResponseResult{
ID int `json:"id"`
Correspondent int `json:"correspondent"`
DocumentType interface{} `json:"document_type"`
StoragePath interface{} `json:"storage_path"`
Title string `json:"title"`
Content string `json:"content"`
Tags []int `json:"tags"`
Created time.Time `json:"created"`
CreatedDate string `json:"created_date"`
Modified time.Time `json:"modified"`
Added time.Time `json:"added"`
ArchiveSerialNumber interface{} `json:"archive_serial_number"`
OriginalFileName string `json:"original_file_name"`
ArchivedFileName string `json:"archived_file_name"`
Owner int `json:"owner"`
UserCanChange bool `json:"user_can_change"`
Notes []interface{} `json:"notes"`
SearchHit struct {
Score float64 `json:"score"`
Highlights string `json:"highlights"`
NoteHighlights string `json:"note_highlights"`
Rank int `json:"rank"`
} `json:"__search_hit__"`
}{
{ {
ID: 1, ID: 1,
Title: "Document 1", Title: "Document 1",

104
types.go
View file

@ -1,59 +1,63 @@
package main package main
import ( // GetDocumentsApiResponse is the response payload for /documents endpoint.
"time" // But we are only interested in a subset of the fields.
)
type GetDocumentsApiResponse struct { type GetDocumentsApiResponse struct {
Count int `json:"count"` Count int `json:"count"`
Next interface{} `json:"next"` // Next interface{} `json:"next"`
Previous interface{} `json:"previous"` // Previous interface{} `json:"previous"`
All []int `json:"all"` All []int `json:"all"`
Results []struct { Results []GetDocumentApiResponseResult `json:"results"`
ID int `json:"id"`
Correspondent int `json:"correspondent"`
DocumentType interface{} `json:"document_type"`
StoragePath interface{} `json:"storage_path"`
Title string `json:"title"`
Content string `json:"content"`
Tags []int `json:"tags"`
Created time.Time `json:"created"`
CreatedDate string `json:"created_date"`
Modified time.Time `json:"modified"`
Added time.Time `json:"added"`
ArchiveSerialNumber interface{} `json:"archive_serial_number"`
OriginalFileName string `json:"original_file_name"`
ArchivedFileName string `json:"archived_file_name"`
Owner int `json:"owner"`
UserCanChange bool `json:"user_can_change"`
Notes []interface{} `json:"notes"`
SearchHit struct {
Score float64 `json:"score"`
Highlights string `json:"highlights"`
NoteHighlights string `json:"note_highlights"`
Rank int `json:"rank"`
} `json:"__search_hit__"`
} `json:"results"`
} }
// GetDocumentApiResponseResult is a part of the response payload for /documents endpoint.
// But we are only interested in a subset of the fields.
type GetDocumentApiResponseResult struct {
ID int `json:"id"`
Correspondent int `json:"correspondent"`
// DocumentType interface{} `json:"document_type"`
// StoragePath interface{} `json:"storage_path"`
Title string `json:"title"`
Content string `json:"content"`
Tags []int `json:"tags"`
// Created time.Time `json:"created"`
// CreatedDate string `json:"created_date"`
// Modified time.Time `json:"modified"`
// Added time.Time `json:"added"`
// ArchiveSerialNumber interface{} `json:"archive_serial_number"`
// OriginalFileName string `json:"original_file_name"`
// ArchivedFileName string `json:"archived_file_name"`
// Owner int `json:"owner"`
// UserCanChange bool `json:"user_can_change"`
Notes []interface{} `json:"notes"`
// SearchHit struct {
// Score float64 `json:"score"`
// Highlights string `json:"highlights"`
// NoteHighlights string `json:"note_highlights"`
// Rank int `json:"rank"`
// } `json:"__search_hit__"`
}
// GetDocumentApiResponse is the response payload for /documents/{id} endpoint.
// But we are only interested in a subset of the fields.
type GetDocumentApiResponse struct { type GetDocumentApiResponse struct {
ID int `json:"id"` ID int `json:"id"`
Correspondent int `json:"correspondent"` Correspondent int `json:"correspondent"`
DocumentType interface{} `json:"document_type"` // DocumentType interface{} `json:"document_type"`
StoragePath interface{} `json:"storage_path"` // StoragePath interface{} `json:"storage_path"`
Title string `json:"title"` Title string `json:"title"`
Content string `json:"content"` Content string `json:"content"`
Tags []int `json:"tags"` Tags []int `json:"tags"`
Created time.Time `json:"created"` // Created time.Time `json:"created"`
CreatedDate string `json:"created_date"` // CreatedDate string `json:"created_date"`
Modified time.Time `json:"modified"` // Modified time.Time `json:"modified"`
Added time.Time `json:"added"` // Added time.Time `json:"added"`
ArchiveSerialNumber interface{} `json:"archive_serial_number"` // ArchiveSerialNumber interface{} `json:"archive_serial_number"`
OriginalFileName string `json:"original_file_name"` // OriginalFileName string `json:"original_file_name"`
ArchivedFileName string `json:"archived_file_name"` // ArchivedFileName string `json:"archived_file_name"`
Owner int `json:"owner"` // Owner int `json:"owner"`
UserCanChange bool `json:"user_can_change"` // UserCanChange bool `json:"user_can_change"`
Notes []interface{} `json:"notes"` Notes []interface{} `json:"notes"`
} }
// Document is a stripped down version of the document object from paperless-ngx. // Document is a stripped down version of the document object from paperless-ngx.