-
Notifications
You must be signed in to change notification settings - Fork 0
/
kodicli.go
122 lines (98 loc) · 2.89 KB
/
kodicli.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
package kodicli
import (
"bytes"
"encoding/base64"
"encoding/json"
"errors"
"io/ioutil"
"net/http"
"net/url"
"strconv"
"time"
)
const jsonRPCVersion = "2.0"
// ErrUnableToParseResponse error is returned when RPC client is unable
// to parse response from Kodi
var ErrUnableToParseResponse = errors.New("Unable to parse Kodi response")
// ErrUnexpectedResponse error is returned when the response received
// does not contains expected content
var ErrUnexpectedResponse = errors.New("Unexpected response from Kodi")
// KodiRPCClient structure contains informations necessary to
// contact Kodi HTTP server
type KodiRPCClient struct {
endpoint string
auth string
nextID int
}
// RPCRequest structure represents RPC request object
// according to JSON-RPC 2.0 specifications
type RPCRequest struct {
JSONRPC string `json:"jsonrpc"`
Method string `json:"method"`
Args interface{} `json:"params,omitempty"`
ID int `json:"id"`
}
// RPCResponse structure contains RPC response object
// according to JSON-RPC 2.0 specifications
type RPCResponse struct {
JSONRPC string `json:"jsonrpc"`
ID int `json:"id"`
Error *RPCError `json:"error,omitempty"`
Result interface{} `json:"result,omitempty"`
}
// RPCError structure contains informations about an RPC error object
// according to JSON-RPC 2.0 specifications
type RPCError struct {
Message string `json:"message"`
Code int `json:"code"`
Data interface{} `json:"data,omitempty"`
}
func checkResultOk(result interface{}) bool {
asStr, ok := result.(string)
if !ok {
return false
}
return asStr == "OK"
}
// New initialize a new Kodi RPC Client
func New(host string, port int, user string, password string) (*KodiRPCClient, error) {
endpoint, err := url.Parse("http://" + host + ":" + strconv.Itoa(port) + "/jsonrpc")
if err != nil {
return nil, errors.New("Unable to create kodi URL : " + err.Error())
}
auth := user + ":" + password
auth = base64.StdEncoding.EncodeToString([]byte(auth))
return &KodiRPCClient{
endpoint: endpoint.String(),
auth: auth,
nextID: 0,
}, nil
}
func (k *KodiRPCClient) doRequest(rpc *RPCRequest) (*RPCResponse, error) {
asJSON, err := json.Marshal(rpc)
if err != nil {
return nil, err
}
req, err := http.NewRequest(http.MethodPost, k.endpoint, bytes.NewBuffer(asJSON))
if err != nil {
return nil, err
}
req.Header.Set("Authorization", "Basic "+k.auth)
req.Header.Set("User-Agent", "kodicli")
cli := http.Client{Timeout: time.Second * 20}
response, err := cli.Do(req)
if err != nil {
return nil, err
}
defer response.Body.Close()
body, err := ioutil.ReadAll(response.Body)
var result RPCResponse
err = json.Unmarshal(body, &result)
if err != nil {
return nil, ErrUnableToParseResponse
}
if result.Error != nil {
return nil, errors.New("Operation failed with message : " + result.Error.Message)
}
return &result, nil
}