-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
91 lines (84 loc) · 3.04 KB
/
config.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
package intrinio
import (
"encoding/json"
"log"
"os"
"strings"
)
type Provider string
const (
OPRA Provider = "OPRA"
IEX Provider = "IEX"
DELAYED_SIP Provider = "DELAYED_SIP"
NASDAQ_BASIC Provider = "NASDAQ_BASIC"
MANUAL Provider = "MANUAL"
)
type Config struct {
ApiKey string
Provider Provider
IPAddress string
}
func (config Config) getAuthUrl() string {
if config.Provider == "OPRA" {
return ("https://realtime-options.intrinio.com/auth?api_key=" + config.ApiKey)
} else if config.Provider == "DELAYED_SIP" {
return ("https://realtime-delayed-sip.intrinio.com/auth?api_key=" + config.ApiKey)
} else if config.Provider == "NASDAQ_BASIC" {
return ("https://realtime-nasdaq-basic.intrinio.com/auth?api_key=" + config.ApiKey)
} else if config.Provider == "IEX" {
return ("https://realtime-mx.intrinio.com/auth?api_key=" + config.ApiKey)
} else if config.Provider == "MANUAL" {
return ("http://" + config.IPAddress + "/auth?api_key=" + config.ApiKey)
} else {
panic("Client - Provider not specified in config")
}
}
func (config Config) getWSUrl(token string) string {
if config.Provider == "OPRA" {
return ("wss://realtime-options.intrinio.com/socket/websocket?vsn=1.0.0&token=" + token)
} else if config.Provider == "DELAYED_SIP" {
return ("wss://realtime-delayed-sip.intrinio.com/socket/websocket?vsn=1.0.0&token=" + token)
} else if config.Provider == "NASDAQ_BASIC" {
return ("wss://realtime-nasdaq-basic.intrinio.com/socket/websocket?vsn=1.0.0&token=" + token)
} else if config.Provider == "IEX" {
return ("wss://realtime-mx.intrinio.com/socket/websocket?vsn=1.0.0&token=" + token)
} else if config.Provider == "MANUAL" {
return ("ws://" + config.IPAddress + "/socket/websocket?vsn=1.0.0&token=" + token)
} else {
panic("Client - Provider not specified in config")
}
}
func LoadConfig(filename string) Config {
wd, getWdErr := os.Getwd()
if getWdErr != nil {
panic(getWdErr)
}
filepath := wd + string(os.PathSeparator) + filename
log.Printf("Client - Loading application configuration from: %s\n", filepath)
data, readFileErr := os.ReadFile(filepath)
if readFileErr != nil {
log.Fatal(readFileErr)
}
var config Config
unmarshalErr := json.Unmarshal(data, &config)
if unmarshalErr != nil {
log.Fatal(unmarshalErr)
}
if strings.TrimSpace(config.ApiKey) == "" {
config.ApiKey = os.Getenv("INTRINIO_API_KEY")
if strings.TrimSpace(config.ApiKey) == "" {
log.Fatal("Client - A valid API key must be provided (either via the config file or the INTRINIO_API_KEY env variable)")
}
}
if (config.Provider != "OPRA") &&
(config.Provider != "DELAYED_SIP") &&
(config.Provider != "NASDAQ_BASIC") &&
(config.Provider != "IEX") &&
(config.Provider != "MANUAL") {
log.Fatal("Client - Config must specify a valid provider")
}
if (config.Provider == "MANUAL") && (strings.TrimSpace(config.IPAddress) == "") {
log.Fatal("Client - Config must specify an IP address for manual configuration")
}
return config
}