|
1
|
package anki |
|
2
|
|
|
3
|
import ( |
|
4
|
"bytes" |
|
5
|
"encoding/json" |
|
6
|
"fmt" |
|
7
|
"net/http" |
|
8
|
) |
|
9
|
|
|
10
|
const ( |
|
11
|
ankiURL = "http://localhost:8765" |
|
12
|
ankiConnectVersion = 6 |
|
13
|
) |
|
14
|
|
|
15
|
type AnkiResponse[T any] struct { |
|
16
|
Result T `json:"result,omitempty"` |
|
17
|
Error string `json:"error,omitempty"` |
|
18
|
} |
|
19
|
|
|
20
|
func (r AnkiResponse[T]) CheckErrors() error { |
|
21
|
if r.Error != "" { |
|
22
|
//nolint:err113 // there's no way to parse those errors so i just return them as they come |
|
23
|
return fmt.Errorf("%s", r.Error) |
|
24
|
} |
|
25
|
return nil |
|
26
|
} |
|
27
|
|
|
28
|
type AnkiRequest[T any] struct { |
|
29
|
Action string `json:"action"` |
|
30
|
Version int `json:"version"` |
|
31
|
Params T `json:"params"` |
|
32
|
} |
|
33
|
|
|
34
|
type paramsDefault struct{} |
|
35
|
|
|
36
|
func request[R any, P any](action string, params P) (AnkiResponse[R], error) { |
|
37
|
bodyReq, err := json.Marshal(AnkiRequest[P]{ |
|
38
|
Action: action, |
|
39
|
Version: ankiConnectVersion, |
|
40
|
Params: params, |
|
41
|
}) |
|
42
|
if err != nil { |
|
43
|
return AnkiResponse[R]{}, err |
|
44
|
} |
|
45
|
|
|
46
|
//nolint:noctx // there's no need for time out or anything like that at the moment |
|
47
|
req, err := http.NewRequest(http.MethodGet, ankiURL, bytes.NewBuffer(bodyReq)) |
|
48
|
if err != nil { |
|
49
|
return AnkiResponse[R]{}, err |
|
50
|
} |
|
51
|
|
|
52
|
req.Header.Set("Content-type", "application/json") |
|
53
|
|
|
54
|
client := &http.Client{} |
|
55
|
resp, err := client.Do(req) |
|
56
|
if err != nil { |
|
57
|
return AnkiResponse[R]{}, err |
|
58
|
} |
|
59
|
|
|
60
|
defer resp.Body.Close() |
|
61
|
|
|
62
|
var ankiResp AnkiResponse[R] |
|
63
|
if err := json.NewDecoder(resp.Body).Decode(&ankiResp); err != nil { |
|
64
|
return AnkiResponse[R]{}, err |
|
65
|
} |
|
66
|
|
|
67
|
return ankiResp, ankiResp.CheckErrors() |
|
68
|
} |