/
responses.go
146 lines (115 loc) · 2.89 KB
/
responses.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
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0
package http
import (
"fmt"
"net/http"
"time"
"github.com/absmach/magistrala"
"github.com/absmach/magistrala/twins"
)
var (
_ magistrala.Response = (*twinRes)(nil)
_ magistrala.Response = (*viewTwinRes)(nil)
_ magistrala.Response = (*viewStateRes)(nil)
_ magistrala.Response = (*twinsPageRes)(nil)
_ magistrala.Response = (*statesPageRes)(nil)
_ magistrala.Response = (*removeRes)(nil)
)
type twinRes struct {
id string
created bool
}
func (res twinRes) Code() int {
if res.created {
return http.StatusCreated
}
return http.StatusOK
}
func (res twinRes) Headers() map[string]string {
if res.created {
return map[string]string{
"Location": fmt.Sprintf("/twins/%s", res.id),
}
}
return map[string]string{}
}
func (res twinRes) Empty() bool {
return true
}
type viewTwinRes struct {
Owner string `json:"owner,omitempty"`
ID string `json:"id"`
Name string `json:"name,omitempty"`
Revision int `json:"revision"`
Created time.Time `json:"created"`
Updated time.Time `json:"updated"`
Definitions []twins.Definition `json:"definitions,omitempty"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}
func (res viewTwinRes) Code() int {
return http.StatusOK
}
func (res viewTwinRes) Headers() map[string]string {
return map[string]string{}
}
func (res viewTwinRes) Empty() bool {
return false
}
type viewStateRes struct {
TwinID string `json:"twin_id"`
ID int64 `json:"id"`
Definition int `json:"definition"`
Created time.Time `json:"created"`
Payload map[string]interface{} `json:"payload"`
}
func (res viewStateRes) Code() int {
return http.StatusOK
}
func (res viewStateRes) Headers() map[string]string {
return map[string]string{}
}
func (res viewStateRes) Empty() bool {
return false
}
type pageRes struct {
Total uint64 `json:"total"`
Offset uint64 `json:"offset"`
Limit uint64 `json:"limit"`
}
type twinsPageRes struct {
pageRes
Twins []viewTwinRes `json:"twins"`
}
func (res twinsPageRes) Code() int {
return http.StatusOK
}
func (res twinsPageRes) Headers() map[string]string {
return map[string]string{}
}
func (res twinsPageRes) Empty() bool {
return false
}
type statesPageRes struct {
pageRes
States []viewStateRes `json:"states"`
}
func (res statesPageRes) Code() int {
return http.StatusOK
}
func (res statesPageRes) Headers() map[string]string {
return map[string]string{}
}
func (res statesPageRes) Empty() bool {
return false
}
type removeRes struct{}
func (res removeRes) Code() int {
return http.StatusNoContent
}
func (res removeRes) Headers() map[string]string {
return map[string]string{}
}
func (res removeRes) Empty() bool {
return true
}