-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwrapper.go
157 lines (141 loc) · 4.06 KB
/
wrapper.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package qrzlog
import (
"context"
"errors"
"github.com/antihax/optional"
"regexp"
"strconv"
"strings"
)
const agent = "k0swe-go-1.0"
type FetchResponse struct {
Adif string
Count uint64
Result string
}
func Fetch(ctx context.Context, key *string) (*FetchResponse, error) {
client := newClient()
opts := RootPostOpts{}
apiResp, _, err := client.DefaultApi.RootPost(ctx, *key, "FETCH", &opts)
if err != nil {
return nil, err
}
count, _ := strconv.ParseUint(apiResp.COUNT, 10, 64)
adif := strings.Replace(apiResp.DATA, "ADIF=", "", -1)
adif = strings.ReplaceAll(adif, "<", "<")
adif = strings.ReplaceAll(adif, ">", ">")
r := FetchResponse{
Result: apiResp.RESULT,
Count: count,
Adif: adif,
}
if apiResp.RESULT == "FAIL" && apiResp.REASON != "" {
// having a logbook with 0 QSOs will always result in a FAIL
err = errors.New(apiResp.REASON)
}
return &r, err
}
type StatusResponse struct {
Action string
BookId string
BookName string
Callsign string
Confirmed uint64
Count uint64
DxccCount uint64
EndDate string
Owner string
Result string
StartDate string
}
func Status(ctx context.Context, key *string) (*StatusResponse, error) {
client := newClient()
opts := RootPostOpts{}
apiResp, _, err := client.DefaultApi.RootPost(ctx, *key, "STATUS", &opts)
if err != nil {
return nil, err
}
count, _ := strconv.ParseUint(apiResp.COUNT, 10, 64)
r := StatusResponse{
Result: apiResp.RESULT,
Count: count,
}
var confirmedStr, dxccCountStr string
findField(&apiResp.DATA, regexp.MustCompile(`(^|&)CONFIRMED=([\d]*)`), &confirmedStr)
r.Confirmed, _ = strconv.ParseUint(confirmedStr, 10, 64)
findField(&apiResp.DATA, regexp.MustCompile(`(^|&)DXCC_COUNT=(\d*)`), &dxccCountStr)
r.DxccCount, _ = strconv.ParseUint(dxccCountStr, 10, 64)
findField(&apiResp.DATA, regexp.MustCompile(`(^|&)ACTION=(\w*)`), &r.Action)
findField(&apiResp.DATA, regexp.MustCompile(`(^|&)BOOKID=(\d*)`), &r.BookId)
findField(&apiResp.DATA, regexp.MustCompile(`(^|&)CALLSIGN=([\w-/]*)`), &r.Callsign)
findField(&apiResp.DATA, regexp.MustCompile(`(^|&)END_DATE=([\d-]*)`), &r.EndDate)
findField(&apiResp.DATA, regexp.MustCompile(`(^|&)OWNER=([\w-/]*)`), &r.Owner)
findField(&apiResp.DATA, regexp.MustCompile(`(^|&)START_DATE=([\d-]*)`), &r.StartDate)
// Put this at the end because it's the least strict regex
findField(&apiResp.DATA, regexp.MustCompile(`(^|&)BOOK_NAME=(.*)`), &r.BookName)
if apiResp.RESULT == "FAIL" && apiResp.REASON != "" {
err = errors.New(apiResp.REASON)
}
return &r, err
}
type InsertResponse struct {
Count uint64
Result string
LogId string
}
func Insert(ctx context.Context, key *string, adif string, replace bool) (*InsertResponse, error) {
client := newClient()
opts := RootPostOpts{
ADIF: optional.NewString(adif),
}
if replace {
opts.OPTION = optional.NewString("REPLACE")
}
apiResp, _, err := client.DefaultApi.RootPost(ctx, *key, "INSERT", &opts)
if err != nil {
return nil, err
}
count, _ := strconv.ParseUint(apiResp.COUNT, 10, 64)
r := InsertResponse{
Result: apiResp.RESULT,
LogId: apiResp.LOGID,
Count: count,
}
if apiResp.RESULT == "FAIL" && apiResp.REASON != "" {
err = errors.New(apiResp.REASON)
}
return &r, err
}
type DeleteResponse struct {
Count uint64
Result string
// The log IDs which were not deleted; Result will be PARTIAL
LogIds []string
}
func Delete(ctx context.Context, key *string, ids []string) (*DeleteResponse, error) {
client := newClient()
idsSlice := strings.Join(ids, ",")
opts := RootPostOpts{
LOGIDS: optional.NewString(idsSlice),
}
apiResp, _, err := client.DefaultApi.RootPost(ctx, *key, "DELETE", &opts)
if err != nil {
return nil, err
}
count, _ := strconv.ParseUint(apiResp.COUNT, 10, 64)
notDeleted := strings.Split(apiResp.LOGIDS, ",")
r := DeleteResponse{
Result: apiResp.RESULT,
LogIds: notDeleted,
Count: count,
}
if apiResp.RESULT == "FAIL" && apiResp.REASON != "" {
err = errors.New(apiResp.REASON)
}
return &r, err
}
func newClient() *APIClient {
config := NewConfiguration()
config.UserAgent = agent
return NewAPIClient(config)
}