-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathapi.go
121 lines (99 loc) · 2.91 KB
/
api.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
package webos
import (
"github.com/pkg/errors"
)
// MessageType is the type sent to and returned by the TV in the `type` field.
type MessageType string
const (
// ErrorMessageType is returned by the TV when an error has occurred.
ErrorMessageType MessageType = "error"
// RegisterMessageType is sent to the TV in a registration request.
RegisterMessageType MessageType = "register"
// RegisteredMessageType is returned by the TV in response to a registration request.
RegisteredMessageType MessageType = "registered"
// RequestMessageType is sent to the TV when issuing Commands.
RequestMessageType MessageType = "request"
// ResponseMessageType is returned by the TV in response to a request.
ResponseMessageType MessageType = "response"
)
// Message represents the JSON message format used in request and responses to
// and from the TV.
type Message struct {
Type MessageType `json:"type,omitempty"`
ID string `json:"id,omitempty"`
URI Command `json:"uri,omitempty"`
Payload Payload `json:"payload,omitempty"`
Error string `json:"error,omitempty"`
}
// Validate validates the Message.
// Only used for response (type: response || registered) types.
func (m Message) Validate() error {
switch m.Type {
case ErrorMessageType:
var err error
if _, ok := m.Payload["returnValue"]; ok {
err = m.Payload.Validate()
}
if err == nil {
return errors.New(m.Error)
}
return errors.Errorf("API error: %s, %s", m.Error, err)
case ResponseMessageType:
return m.Payload.Validate()
case RegisteredMessageType:
if m.Payload == nil {
return errors.New("empty payload")
}
return nil
default:
return errors.Errorf("unexpected API response type: %s", m.Type)
}
}
// Payload represents the Payload contained in the Message body.
type Payload map[string]interface{}
// Validate valides the Payload.
func (p Payload) Validate() error {
if p == nil {
return errors.New("empty payload")
}
returnValueI, ok := p["returnValue"]
if !ok {
return errors.New("`returnValue` is missing")
}
returnValue, ok := returnValueI.(bool)
if !ok {
return errors.New("`returnValue` is not of type bool")
}
if !returnValue {
if p["errorCode"] != nil {
return errors.Errorf("error %v: %v", p["errorCode"], p["errorText"])
}
return errors.New("`returnValue` is false and `errorCode` is nil")
}
return nil
}
// App represents an applications in the TVs responses.
type App struct {
ReturnValue bool
AppID string
WindowID string
ProcessID string
Running bool
Visible bool
}
// Service represents services in the TVs responses.
type Service struct {
Name string
Version float32
}
// ServiceList represents an array of Service types in the TVs responses.
type ServiceList struct {
Services []Service
}
// Volume represents the audio output volume in the TVs responses.
type Volume struct {
ReturnValue bool
Scenario string
Volume int32
Muted bool
}