forked from spacecodewor/fmpcloud-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclient.go
More file actions
137 lines (113 loc) · 3.31 KB
/
client.go
File metadata and controls
137 lines (113 loc) · 3.31 KB
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
package fmpcloud
import (
"time"
"github.com/go-resty/resty/v2"
"github.com/pkg/errors"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
// APIUrl type for api url
type APIUrl string
// Config for create new API client
type Config struct {
Logger *zap.Logger
HTTPClient *resty.Client
APIKey string
APIUrl APIUrl
Debug bool
RetryCount *int
RetryWaitTime *time.Duration
Timeout int
}
// APIClient ...
type APIClient struct {
Stock *Stock
Forex *Forex
Form13F *Form13F
Crypto *Crypto
CompanyValuation *CompanyValuation
TechnicalIndicator *TechnicalIndicator
InsiderTrading *InsiderTrading
AlternativeData *AlternativeData
Economics *Economics
API *API
Logger *zap.Logger
Debug bool
}
// Core params
const (
APIFmpcloudURL APIUrl = "https://fmpcloud.io/api"
APIFinancialModelingPrepURL APIUrl = "https://financialmodelingprep.com/api"
apiDefaultKey = "demo"
apiDefaultTimeout = 25
)
// NewAPIClient creates a new API client
func NewAPIClient(cfg Config) (*APIClient, error) {
APIClient := &APIClient{Logger: cfg.Logger, Debug: cfg.Debug}
if APIClient.Logger == nil {
logger, err := createNewLogger()
if err != nil {
return nil, errors.Wrap(err, "Error create new zap logger")
}
APIClient.Logger = logger
}
// Check set timeout param
if cfg.Timeout == 0 {
cfg.Timeout = apiDefaultTimeout
}
// Init rest client (resty)
if cfg.HTTPClient == nil {
cfg.HTTPClient = resty.New()
}
cfg.HTTPClient.SetDebug(APIClient.Debug)
cfg.HTTPClient.SetTimeout(time.Duration(cfg.Timeout) * time.Second)
// Check set APIUrl param
if len(cfg.APIUrl) == 0 {
cfg.APIUrl = APIFinancialModelingPrepURL
}
// Check set APIKey param
if len(cfg.APIKey) == 0 {
cfg.APIKey = apiDefaultKey
}
cfg.HTTPClient.SetHostURL(string(cfg.APIUrl))
HTTPClient := &HTTPClient{
client: cfg.HTTPClient,
apiKey: cfg.APIKey,
logger: APIClient.Logger,
}
if cfg.RetryCount != nil {
HTTPClient.retryCount = cfg.RetryCount
}
if cfg.RetryWaitTime != nil {
HTTPClient.retryWaitTime = cfg.RetryWaitTime
}
if HTTPClient.retryCount == nil || *HTTPClient.retryCount == 0 {
retryCount := 1
HTTPClient.retryCount = &retryCount
}
if HTTPClient.retryWaitTime == nil || *HTTPClient.retryWaitTime == 0 {
retryWaitTime := 1 * time.Second
HTTPClient.retryWaitTime = &retryWaitTime
}
APIClient.Stock = &Stock{Client: HTTPClient}
APIClient.Form13F = &Form13F{Client: HTTPClient}
APIClient.Forex = &Forex{Client: HTTPClient}
APIClient.Crypto = &Crypto{Client: HTTPClient}
APIClient.CompanyValuation = &CompanyValuation{Client: HTTPClient}
APIClient.TechnicalIndicator = &TechnicalIndicator{Client: HTTPClient}
APIClient.API = &API{Client: HTTPClient}
APIClient.InsiderTrading = &InsiderTrading{Client: HTTPClient}
APIClient.AlternativeData = &AlternativeData{Client: HTTPClient}
APIClient.Economics = &Economics{Client: HTTPClient}
return APIClient, nil
}
// Create new logger
func createNewLogger() (*zap.Logger, error) {
cfg := zap.NewProductionConfig()
cfg.EncoderConfig.EncodeTime = zapcore.RFC3339TimeEncoder
logger, err := cfg.Build()
if err != nil {
return nil, errors.Wrap(err, "Logger Error: init")
}
return logger, nil
}