mirror of
https://github.com/icereed/paperless-gpt.git
synced 2025-03-13 13:18:02 -05:00
Fix tags API pagination
This commit is contained in:
parent
31f0e81465
commit
0dd1d0b5ad
2 changed files with 36 additions and 28 deletions
61
main.go
61
main.go
|
@ -165,40 +165,47 @@ func createLLM() (llms.Model, error) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func getAllTags(ctx context.Context, baseURL, apiToken string) (map[string]int, error) {
|
func getAllTags(ctx context.Context, baseURL, apiToken string) (map[string]int, error) {
|
||||||
|
tagIDMapping := make(map[string]int)
|
||||||
url := fmt.Sprintf("%s/api/tags/", baseURL)
|
url := fmt.Sprintf("%s/api/tags/", baseURL)
|
||||||
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
req.Header.Set("Authorization", fmt.Sprintf("Token %s", apiToken))
|
|
||||||
|
|
||||||
client := &http.Client{}
|
client := &http.Client{}
|
||||||
resp, err := client.Do(req)
|
|
||||||
if err != nil {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
defer resp.Body.Close()
|
|
||||||
|
|
||||||
if resp.StatusCode != http.StatusOK {
|
for url != "" {
|
||||||
bodyBytes, _ := io.ReadAll(resp.Body)
|
req, err := http.NewRequestWithContext(ctx, "GET", url, nil)
|
||||||
return nil, fmt.Errorf("Error fetching tags: %d, %s", resp.StatusCode, string(bodyBytes))
|
if err != nil {
|
||||||
}
|
return nil, err
|
||||||
|
}
|
||||||
|
req.Header.Set("Authorization", fmt.Sprintf("Token %s", apiToken))
|
||||||
|
|
||||||
var tagsResponse struct {
|
resp, err := client.Do(req)
|
||||||
Results []struct {
|
if err != nil {
|
||||||
ID int `json:"id"`
|
return nil, err
|
||||||
Name string `json:"name"`
|
}
|
||||||
} `json:"results"`
|
defer resp.Body.Close()
|
||||||
}
|
|
||||||
|
|
||||||
err = json.NewDecoder(resp.Body).Decode(&tagsResponse)
|
if resp.StatusCode != http.StatusOK {
|
||||||
if err != nil {
|
bodyBytes, _ := io.ReadAll(resp.Body)
|
||||||
return nil, err
|
return nil, fmt.Errorf("Error fetching tags: %d, %s", resp.StatusCode, string(bodyBytes))
|
||||||
}
|
}
|
||||||
|
|
||||||
tagIDMapping := make(map[string]int)
|
var tagsResponse struct {
|
||||||
for _, tag := range tagsResponse.Results {
|
Results []struct {
|
||||||
tagIDMapping[tag.Name] = tag.ID
|
ID int `json:"id"`
|
||||||
|
Name string `json:"name"`
|
||||||
|
} `json:"results"`
|
||||||
|
Next string `json:"next"`
|
||||||
|
}
|
||||||
|
|
||||||
|
err = json.NewDecoder(resp.Body).Decode(&tagsResponse)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tag := range tagsResponse.Results {
|
||||||
|
tagIDMapping[tag.Name] = tag.ID
|
||||||
|
}
|
||||||
|
|
||||||
|
url = tagsResponse.Next
|
||||||
}
|
}
|
||||||
|
|
||||||
return tagIDMapping, nil
|
return tagIDMapping, nil
|
||||||
|
|
|
@ -17,6 +17,7 @@ const SuggestionCard: React.FC<SuggestionCardProps> = ({
|
||||||
onTagAddition,
|
onTagAddition,
|
||||||
onTagDeletion,
|
onTagDeletion,
|
||||||
}) => {
|
}) => {
|
||||||
|
const sortedAvailableTags = availableTags.sort((a, b) => a.name.localeCompare(b.name));
|
||||||
const document = suggestion.original_document;
|
const document = suggestion.original_document;
|
||||||
return (
|
return (
|
||||||
<div className="bg-white dark:bg-gray-800 shadow-lg shadow-blue-500/50 rounded-md p-4 relative flex flex-col justify-between h-full">
|
<div className="bg-white dark:bg-gray-800 shadow-lg shadow-blue-500/50 rounded-md p-4 relative flex flex-col justify-between h-full">
|
||||||
|
@ -64,7 +65,7 @@ const SuggestionCard: React.FC<SuggestionCardProps> = ({
|
||||||
value: index.toString(),
|
value: index.toString(),
|
||||||
})) || []
|
})) || []
|
||||||
}
|
}
|
||||||
suggestions={availableTags.map((tag) => ({
|
suggestions={sortedAvailableTags.map((tag) => ({
|
||||||
id: tag.id,
|
id: tag.id,
|
||||||
name: tag.name,
|
name: tag.name,
|
||||||
label: tag.name,
|
label: tag.name,
|
||||||
|
|
Loading…
Reference in a new issue