summaryrefslogtreecommitdiffstats
path: root/internal/client/files.go
blob: c4ca3728616e917f43da715fd4fdb1e15507043b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package client

import (
	"encoding/json"
	"fmt"
	"git.db.org.ai/dborg/internal/models"
	"net/url"
)

func (c *Client) SearchOpenDirectoryFiles(params *models.OpenDirectorySearchParams) (*models.OpenDirectorySearchResponse, error) {
	path := fmt.Sprintf("/files/%s", url.PathEscape(params.URL))

	queryParams := url.Values{}
	if params.Filename != "" {
		queryParams.Add("filename", params.Filename)
	}
	if params.Extension != "" {
		queryParams.Add("extension", params.Extension)
	}
	if params.Exclude != "" {
		queryParams.Add("exclude", params.Exclude)
	}
	if params.Size > 0 {
		queryParams.Add("size", fmt.Sprintf("%d", params.Size))
	}
	if params.From > 0 {
		queryParams.Add("from", fmt.Sprintf("%d", params.From))
	}

	data, err := c.Get(path, queryParams)
	if err != nil {
		return nil, err
	}

	var response models.OpenDirectorySearchResponse
	if err := json.Unmarshal(data, &response); err != nil {
		return nil, fmt.Errorf("failed to parse open directory search response: %w", err)
	}

	return &response, nil
}