]>
Commit | Line | Data |
---|---|---|
7a9e5112 | 1 | package api |
2 | ||
3 | import ( | |
4 | "fmt" | |
2f91f20a | 5 | "strings" |
6 | "time" | |
7a9e5112 | 7 | |
2f91f20a | 8 | "github.com/jloup/utils" |
7a9e5112 | 9 | "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/db" |
24e47979 | 10 | "immae.eu/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front/markets" |
7a9e5112 | 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 | ||
16e43cc7 | 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 | ||
7a9e5112 | 54 | return config.Config, nil |
55 | } | |
56 | ||
24e47979 | 57 | type TestMarketCredentialsQuery struct { |
2f91f20a | 58 | In struct { |
24e47979 | 59 | User db.User |
60 | Market string | |
2f91f20a | 61 | } |
62 | } | |
63 | ||
24e47979 | 64 | func (q TestMarketCredentialsQuery) ValidateParams() *Error { |
2f91f20a | 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 | ||
2f91f20a | 69 | return nil |
70 | } | |
71 | ||
24e47979 | 72 | func (q TestMarketCredentialsQuery) Run() (interface{}, *Error) { |
2f91f20a | 73 | config, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) |
74 | if err != nil { | |
75 | return nil, NewInternalError(err) | |
76 | } | |
77 | ||
299b6b6d | 78 | if config == nil || config.Config["key"] == "" || config.Config["secret"] == "" { |
24e47979 | 79 | return nil, &Error{InvalidMarketCredentials, "no market credentials", fmt.Errorf("market credentials are empty for marketId '%v'", q.In.Market)} |
2f91f20a | 80 | } |
81 | ||
299b6b6d | 82 | resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error { |
24e47979 | 83 | err := Poloniex.TestCredentials(config.Config["key"], config.Config["secret"]) |
2f91f20a | 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 | ||
50c6eea6 | 89 | if utils.ErrIs(err, markets.IPRestricted) { |
90 | return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)} | |
2f91f20a | 91 | } |
92 | ||
2f91f20a | 93 | if err != nil { |
94 | return NewInternalError(err) | |
95 | } | |
96 | ||
2f91f20a | 97 | return nil |
98 | }) | |
99 | ||
100 | if resultErr != nil { | |
299b6b6d | 101 | return nil, NewInternalError(resultErr) |
2f91f20a | 102 | } |
103 | ||
24e47979 | 104 | return nil, nil |
2f91f20a | 105 | } |
106 | ||
7a9e5112 | 107 | type UpdateMarketConfigQuery struct { |
108 | In struct { | |
109 | User db.User | |
110 | Market string | |
111 | Key string | |
112 | Secret string | |
113 | } | |
114 | } | |
115 | ||
116 | func (q UpdateMarketConfigQuery) ValidateParams() *Error { | |
117 | if q.In.Market == "" { | |
118 | return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} | |
119 | } | |
120 | ||
2f91f20a | 121 | q.In.Secret = strings.TrimSpace(q.In.Secret) |
122 | q.In.Key = strings.TrimSpace(q.In.Key) | |
123 | ||
7a9e5112 | 124 | return nil |
125 | } | |
126 | ||
127 | func (q UpdateMarketConfigQuery) Run() (interface{}, *Error) { | |
128 | configMap := make(map[string]string) | |
129 | if q.In.Key != "" { | |
130 | configMap["key"] = q.In.Key | |
131 | } | |
132 | if q.In.Secret != "" { | |
133 | configMap["secret"] = q.In.Secret | |
134 | } | |
135 | ||
299b6b6d | 136 | marketConfig, err := db.SetUserMarketConfig(q.In.User.Id, q.In.Market, configMap) |
137 | if err != nil { | |
138 | return nil, NewInternalError(err) | |
139 | } | |
140 | ||
141 | resultErr := CallExternalService(fmt.Sprintf("'%s' TestCredentials", q.In.Market), EXTERNAL_SERVICE_TIMEOUT_SECONDS*time.Second, func() error { | |
142 | err := Poloniex.TestCredentials(marketConfig.Config["key"], marketConfig.Config["secret"]) | |
143 | ||
144 | if utils.ErrIs(err, markets.InvalidCredentials) { | |
145 | return &Error{InvalidMarketCredentials, "wrong market credentials", fmt.Errorf("wrong '%v' market credentials", q.In.Market)} | |
146 | } | |
147 | ||
148 | if utils.ErrIs(err, markets.IPRestricted) { | |
149 | return &Error{IPRestrictedApiKey, "ip restricted api key", fmt.Errorf("'%v' ip restricted", q.In.Market)} | |
150 | } | |
151 | ||
152 | if err != nil { | |
153 | return NewInternalError(err) | |
154 | } | |
155 | ||
156 | return nil | |
157 | }) | |
158 | ||
159 | var newStatus db.MarketConfigStatus = db.MarketConfigEnabled | |
160 | ||
161 | if ErrorIs(resultErr, InvalidMarketCredentials) || ErrorIs(resultErr, IPRestrictedApiKey) { | |
162 | newStatus = db.MarketConfigInvalidCredentials | |
163 | } else if resultErr != nil { | |
164 | return nil, NewInternalError(resultErr) | |
165 | } | |
166 | ||
167 | marketConfig, err = db.SetMarketConfigStatus(*marketConfig, newStatus) | |
7a9e5112 | 168 | if err != nil { |
169 | return nil, NewInternalError(err) | |
170 | } | |
171 | ||
172 | return nil, nil | |
173 | } | |
24e47979 | 174 | |
175 | type GetPortfolioQuery struct { | |
176 | In struct { | |
177 | User db.User | |
178 | Market string | |
179 | } | |
180 | } | |
181 | ||
182 | func (q GetPortfolioQuery) ValidateParams() *Error { | |
183 | if q.In.Market != "poloniex" { | |
184 | return &Error{BadRequest, "invalid market name", fmt.Errorf("'%v' is not a valid market name", q.In.Market)} | |
185 | } | |
186 | ||
187 | return nil | |
188 | } | |
189 | ||
190 | func (q GetPortfolioQuery) Run() (interface{}, *Error) { | |
191 | marketConfig, err := db.GetUserMarketConfig(q.In.User.Id, q.In.Market) | |
192 | if err != nil { | |
193 | return nil, NewInternalError(err) | |
194 | } | |
195 | ||
196 | report, err := GetWeekPortfolio(*marketConfig) | |
197 | if ErrorIs(err, NotFound) { | |
198 | return nil, err.(*Error) | |
199 | } | |
200 | ||
201 | if err != nil { | |
202 | return nil, NewInternalError(err) | |
203 | } | |
204 | ||
205 | return report.Round(), nil | |
206 | } |