-
Notifications
You must be signed in to change notification settings - Fork 0
/
options.go
72 lines (58 loc) · 1.5 KB
/
options.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
package sdk
import (
"net/url"
)
// Options represents general device options, like URL endpoints.
type Options struct {
httpServer *url.URL
mqttServer *url.URL
}
// Option is a single option configuration of a device
type Option interface {
apply(*Device)
}
type optionFunc func(*Device)
func (f optionFunc) apply(device *Device) {
f(device)
}
const httpEndpoint string = "https://api.allthingstalk.io"
const mqttEndpoint string = "ssl://api.allthingstalk.io:8883"
// NewOptions creates default options.
func newOptions() *Options {
return newCustomOptions(httpEndpoint, mqttEndpoint)
}
// NewCustomOptions creates options with HTTP and MQTT endpoints.
func newCustomOptions(http string, mqtt string) *Options {
httpURL, _ := url.Parse(http)
mqttURL, _ := url.Parse(mqtt)
return &Options{
httpServer: httpURL,
mqttServer: mqttURL,
}
}
// WithHTTP sets HTTP REST API endpoint.
func WithHTTP(endpoint string) Option {
return optionFunc(func(device *Device) {
u, _ := url.Parse(endpoint)
device.options.httpServer = u
})
}
// WithMQTT sets MQTT API endpoint.
func WithMQTT(endpoint string) Option {
return optionFunc(func(device *Device) {
u, _ := url.Parse(endpoint)
device.options.mqttServer = u
})
}
// SetAPI sets HTTP REST API endpoint.
func (o *Options) SetAPI(endpoint string) *Options {
u, _ := url.Parse(endpoint)
o.httpServer = u
return o
}
// SetMqtt sets MQTT API endpoint.
func (o *Options) SetMqtt(endpoint string) *Options {
u, _ := url.Parse(endpoint)
o.mqttServer = u
return o
}