-
Notifications
You must be signed in to change notification settings - Fork 1
/
spaceapi.go
160 lines (138 loc) · 4.27 KB
/
spaceapi.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
158
159
160
package spaceapi
import (
"encoding/json"
"errors"
)
import "net/http"
import "io/ioutil"
type Location struct {
Address string `json:"address,omitempty"`
Lat float32 `json:"lat,omitempty"`
Lon float32 `json:"lon,omitempty"`
}
type Icon struct {
Open string `json:"open,omitempty"`
Closed string `json:"closed,omitempty"`
}
const (
True = iota
False
Unknown
)
type SpaceState uint8
func (s SpaceState) MarshalJSON() ([]byte, error) {
switch s {
case True:
return []byte(`true`), nil
case False:
return []byte(`false`), nil
case Unknown:
return []byte(`null`), nil
}
return nil, errors.New("undefined value")
}
func (s *SpaceState) UnmarshalJSON(data []byte) error {
switch string(data) {
case `true`:
(*s) = True
case `false`:
(*s) = False
default:
(*s) = Unknown
}
return nil
}
type State struct {
Open SpaceState `json:"open"`
Lastchange int64 `json:"lastchange,omitempty"`
TriggerPerson string `json:"trigger_person,omitempty"`
Message string `json:"message,omitempty"`
Icon *Icon `json:"icon,omitempty"`
}
type GoogleContact struct {
Plus string `json:"plus,omitempty"`
}
type Contact struct {
Phone string `json:"phone,omitempty"`
Sip string `json:"sip,omitempty"`
Irc string `json:"irc,omitempty"`
Twitter string `json:"twitter,omitempty"`
Facebook string `json:"facebook,omitempty"`
Google *GoogleContact `json:"google,omitempty"`
Identica string `json:"identica,omitempty"`
Foursquare string `json:"foursquare,omitempty"`
Email string `json:"email,omitempty"`
Ml string `json:"ml,omitempty"`
Jabber string `json:"jabber,omitempty"`
IssueMail string `json:"issue_mail,omitempty"`
}
type Feed struct {
Type string `json:"type,omitempty"`
Url string `json:"url,omitempty"`
}
type Cache struct {
Schedule string `json:"schedule,omitempty"`
}
type SpaceFed struct {
Spacenet bool `json:"spacenet,omitempty"`
Spacesaml bool `json:"spacesaml,omitempty"`
Spacephone bool `json:"spacephone,omitempty"`
}
type RadioShow struct {
Name string `json:"name,omitempty"`
Url string `json:"url,omitempty"`
Type string `json:"type,omitempty"`
Start string `json:"start,omitempty"`
End string `json:"end,omitempty"`
}
type SpaceAPI struct {
Api string `json:"api,omitempty"`
Space string `json:"space,omitempty"`
Logo string `json:"logo,omitempty"`
Url string `json:"url,omitempty"`
Location *Location `json:"location,omitempty"`
State *State `json:"state,omitempty"`
Contact *Contact `json:"contact,omitempty"`
IssueReportChannels []string `json:"issue_report_channels,omitempty"`
Sensors map[string]interface{} `json:"sensors,omitempty"`
Feeds map[string]Feed `json:"feeds,omitempty"`
Cache *Cache `json:"cache,omitempty"`
SpaceFed *SpaceFed `json:"spacefed,omitempty"`
Projects []string `json:"projects,omitempty"`
RadioShow []RadioShow `json:"radioshow,omitempty"`
}
func (s *SpaceAPI) ToJSON() (string, error) {
bytes, err := json.Marshal(s)
return string(bytes), err
}
func NewSpaceAPI(api_version string, space_name string, logo_url string, website_url string, location *Location, state *State, contact *Contact, issue_report_channels []string) SpaceAPI {
return SpaceAPI{
Api: api_version,
Space: space_name,
Logo: logo_url,
Location: location,
Contact: contact,
State: state,
IssueReportChannels: issue_report_channels,
}
}
type Endpoint struct {
endpoint_url string
}
func (e *Endpoint) GetSpaceAPIData() (*SpaceAPI, error) {
resp, err := http.Get(e.endpoint_url)
if err != nil {
return nil, err
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, err
}
data := new(SpaceAPI)
err = json.Unmarshal(body, &data)
if err != nil {
return nil, err
}
return data, nil
}