]>
Commit | Line | Data |
---|---|---|
1 | package api | |
2 | ||
3 | import ( | |
4 | "fmt" | |
5 | "strings" | |
6 | "time" | |
7 | ||
8 | "git.immae.eu/Cryptoportfolio/Front.git/db" | |
9 | "git.immae.eu/Cryptoportfolio/Front.git/markets" | |
10 | "github.com/jloup/utils" | |
11 | ) | |
12 | ||
13 | type MarketConfigQuery struct { | |
14 | In struct { | |
15 | User db.User | |
16 | Market string | |
17 | } | |
18 | } | |
19 | ||
20 | func (q MarketConfigQuery) ValidateParams() *Error { | |
21 | if q.In.Market != "poloniex" { | |
22 | return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} | |
23 | } | |
24 | ||
25 | return nil | |
26 | } | |
27 | ||
28 | func (q MarketConfigQuery) Run() (interface{}, *Error) { | |
29 | config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) | |
30 | if err != nil { | |
31 | return nil, NewInternalError(err) | |
32 | } | |
33 | ||
34 | if config == nil { | |
35 | configMap := make(map[string]string) | |
36 | configMap["key"] = "" | |
37 | configMap["secret"] = "" | |
38 | ||
39 | config, err = db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap) | |
40 | if err != nil { | |
41 | return nil, NewInternalError(err) | |
42 | } | |
43 | ||
44 | } | |
45 | ||
46 | if _, ok := config.Config["key"]; !ok { | |
47 | config.Config["key"] = "" | |
48 | } | |
49 | ||
50 | if _, ok := config.Config["secret"]; !ok { | |
51 | config.Config["secret"] = "" | |
52 | } | |
53 | ||
54 | return config.Config, nil | |
55 | } | |
56 | ||
57 | type TestMarketCredentialsQuery struct { | |
58 | In struct { | |
59 | User db.User | |
60 | Market string | |
61 | } | |
62 | } | |
63 | ||
64 | func (q TestMarketCredentialsQuery) ValidateParams() *Error { | |
65 | if q.In.Market != "poloniex" { | |
66 | return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} | |
67 | } | |
68 | ||
69 | return nil | |
70 | } | |
71 | ||
72 | func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) { | |
73 | config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) | |
74 | if err != nil { | |
75 | return nil, NewInternalError(err) | |
76 | } | |
77 | ||
78 | if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" { | |
79 | return nil, &Error{MarketCredentialsNotConfigured, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)} | |
80 | } | |
81 | ||
82 | resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error { | |
83 | err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"]) | |
84 | ||
85 | if utils.ErrIs(err, markets.InvalidCredentials) { | |
86 | return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)} | |
87 | } | |
88 | ||
89 | if utils.ErrIs(err, markets.IPRestricted) { | |
90 | return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)} | |
91 | } | |
92 | ||
93 | if err != nil { | |
94 | return NewInternalError(err) | |
95 | } | |
96 | ||
97 | return nil | |
98 | }) | |
99 | ||
100 | var newStatus db.MarketConfigStatus = config.Status | |
101 | ||
102 | if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) || ErrorIs(resultErr, MarketCredentialsNotConfigured) { | |
103 | newStatus = db.MarketConfigInvalidCredentials | |
104 | } else if resultErr != nil { | |
105 | return nil, NewInternalError(resultErr) | |
106 | } else if resultErr != nil { | |
107 | return nil, NewInternalError(resultErr) | |
108 | } | |
109 | ||
110 | if newStatus != config.Status { | |
111 | config, err = db.SetMarketConfigStatus(*config, newStatus) | |
112 | if err != nil { | |
113 | return nil, NewInternalError(err) | |
114 | } | |
115 | } | |
116 | ||
117 | return nil, nil | |
118 | } | |
119 | ||
120 | type UpdateMarketConfigQuery struct { | |
121 | In struct { | |
122 | User db.User | |
123 | Market string | |
124 | Key string | |
125 | Secret string | |
126 | } | |
127 | } | |
128 | ||
129 | func (q UpdateMarketConfigQuery) ValidateParams() *Error { | |
130 | if q.In.Market == "" { | |
131 | return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} | |
132 | } | |
133 | ||
134 | q.In.Secret = strings.TrimSpace(q.In.Secret) | |
135 | q.In.Key = strings.TrimSpace(q.In.Key) | |
136 | ||
137 | return nil | |
138 | } | |
139 | ||
140 | func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) { | |
141 | configMap := make(map[string]string) | |
142 | if q.In.Key != "" { | |
143 | configMap["key"] = q.In.Key | |
144 | } | |
145 | if q.In.Secret != "" { | |
146 | configMap["secret"] = q.In.Secret | |
147 | } | |
148 | ||
149 | marketConfig, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap) | |
150 | if err != nil { | |
151 | return nil, NewInternalError(err) | |
152 | } | |
153 | ||
154 | resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error { | |
155 | err := Poloniex.TestCredentials(marketConfig.Config["key"], marketConfig.Config["secret"]) | |
156 | ||
157 | if utils.ErrIs(err, markets.InvalidCredentials) { | |
158 | return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)} | |
159 | } | |
160 | ||
161 | if utils.ErrIs(err, markets.IPRestricted) { | |
162 | return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)} | |
163 | } | |
164 | ||
165 | if err != nil { | |
166 | return NewInternalError(err) | |
167 | } | |
168 | ||
169 | return nil | |
170 | }) | |
171 | ||
172 | var newStatus db.MarketConfigStatus = db.MarketConfigEnabled | |
173 | ||
174 | if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) || ErrorIs(resultErr, MarketCredentialsNotConfigured) { | |
175 | newStatus = db.MarketConfigInvalidCredentials | |
176 | } else if resultErr != nil { | |
177 | return nil, NewInternalError(resultErr) | |
178 | } | |
179 | ||
180 | marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus) | |
181 | if err != nil { | |
182 | return nil, NewInternalError(err) | |
183 | } | |
184 | ||
185 | return nil, nil | |
186 | } | |
187 | ||
188 | type GetPortfolioQuery struct { | |
189 | In struct { | |
190 | User db.User | |
191 | Market string | |
192 | } | |
193 | } | |
194 | ||
195 | func (q GetPortfolioQuery) ValidateParams() *Error { | |
196 | if q.In.Market != "poloniex" { | |
197 | return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} | |
198 | } | |
199 | ||
200 | return nil | |
201 | } | |
202 | ||
203 | func (q GetPortfolioQuery) Run() (interface{}, *Error) { | |
204 | marketConfig, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) | |
205 | if err != nil { | |
206 | return nil, NewInternalError(err) | |
207 | } | |
208 | ||
209 | report, err := GetWeekPortfolio(*marketConfig) | |
210 | if ErrorIs(err, NotFound) { | |
211 | return nil, err.(*Error) | |
212 | } | |
213 | ||
214 | if err != nil { | |
215 | return nil, NewInternalError(err) | |
216 | } | |
217 | ||
218 | return report.Round(), nil | |
219 | } |