This repository has been archived by the owner on Jan 17, 2024. It is now read-only.
/
correspondent.go
76 lines (65 loc) · 2.42 KB
/
correspondent.go
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package paperless
import (
"encoding/json"
"fmt"
log "github.com/sirupsen/logrus"
)
// Correspondent is a struct representation of Paperless' /api/correspondents/<id> JSON response.
type Correspondent struct {
ID int `json:"id"`
Slug string `json:"slug"`
Name string `json:"name"`
Match string `json:"match"`
MatchingAlgorithm MatchingAlgorithm `json:"matching_algorithm"`
IsInsensitive bool `json:"is_insensitive"`
}
// How should we represent a Correspondent object when trying to stringify it? This returns the struct as a string.
func (c Correspondent) String() string {
return fmt.Sprintf("ID: %v, Slug: %v, Name: %v, Match: %v, Matching Algorithm: %v, Is Insensitive: %v",
c.ID, c.Slug, c.Name, c.Match, c.MatchingAlgorithm, c.IsInsensitive)
}
// CorrespondentList is a slice of https://godoc.org/github.com/stgarf/paperless-cli/paperless/#Correspondent structs.
type CorrespondentList []Correspondent
// GetCorrespondent returns a https://godoc.org/github.com/stgarf/paperless-cli/paperless/#CorrespondentList matching the search string.
func (p Paperless) GetCorrespondent(s string, caseSensitive bool) (CorrespondentList, error) {
// A place to store the results
var c Correspondent
var cl CorrespondentList
// Make the request
if caseSensitive {
p.Root += "/correspondents/?name__contains=" + s
} else {
p.Root += "/correspondents/?name__icontains=" + s
}
u := fmt.Sprint(p)
results, err := p.MakeGetRequest(u)
if err != nil {
log.Errorf("An error occurred making request: %v", err.Error())
}
// Append results so far to CorrespondentList cl
for _, corr := range results {
json.Unmarshal([]byte(corr.Raw), &c)
json.Unmarshal([]byte(corr.Raw), &c)
cl = append(cl, c)
}
return cl, nil
}
// GetCorrespondents returns a https://godoc.org/github.com/stgarf/paperless-cli/paperless/#CorrespondentList.
func (p Paperless) GetCorrespondents() (CorrespondentList, error) {
// A place to store the results
var c Correspondent
var cl CorrespondentList
// Make the request
p.Root += "/correspondents"
u := fmt.Sprint(p)
results, err := p.MakeGetRequest(u)
if err != nil {
log.Errorf("An error occurred making request: %v", err.Error())
}
// Append results so far to CorrespondentList cl
for _, corr := range results {
json.Unmarshal([]byte(corr.Raw), &c)
cl = append(cl, c)
}
return cl, nil
}