-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
99 lines (85 loc) · 2.25 KB
/
client.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
package mango
import (
"fmt"
"github.com/spf13/viper"
"net/http"
"os"
"sync"
"time"
)
// Client represents the main Mango client, used to make requests to the Manifold API
type Client struct {
client http.Client
key string
url string
}
var lock = &sync.Mutex{}
var mcInstance *Client // TODO: figure out whether this should really be a singleton or not
// ClientInstance creates a singleton of the Mango Client.
// It optionally takes a http.Client, base URL, and API key.
//
// If you don't specify a base URL, the default Manifold Markets domain will be used.
//
// If no API key is provided then you will need to specify a `MANIFOLD_API_KEY` in your .env file
//
// Just because you *can* specify an API key here doesn't mean that you *should*!
// Please don't put your API key in code.
func ClientInstance(client *http.Client, url, ak *string) *Client {
if mcInstance == nil {
lock.Lock()
defer lock.Unlock()
if mcInstance == nil {
if client == nil {
client = &http.Client{
Timeout: time.Second * 10,
}
}
if url == nil {
u := base
url = &u
}
if ak == nil {
a := apiKey()
ak = &a
}
mcInstance = &Client{
client: *client,
key: *ak,
url: *url,
}
}
}
return mcInstance
}
// DefaultClientInstance returns a singleton of the Mango Client using all default values.
//
// It will use a default http.Client, the primary Manifold domain as the base URL, and
// the value of `MANIFOLD_API_KEY` in your .env file as the API key.
func DefaultClientInstance() *Client {
return ClientInstance(nil, nil, nil)
}
// Destroy destroys the current singleton of the Mango client.
//
// Useful for testing.
func (mc *Client) Destroy() {
if mcInstance != nil {
lock.Lock()
defer lock.Unlock()
mcInstance = nil
}
}
func apiKey() string {
v := viper.New()
v.SetConfigName(".env")
v.SetConfigType("env")
v.AddConfigPath(".")
err := v.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
fmt.Errorf("fatal error config file: %w", err)
}
if mak := v.GetString("MANIFOLD_API_KEY"); mak != "" {
return mak
}
fmt.Println("No value for MANIFOLD_API_KEY found in .env file, falling back to env vars")
return os.Getenv("MANIFOLD_API_KEY")
}