]> git.immae.eu Git - perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git/blob - api/const.go
Load credentials only when user requests it.
[perso/Immae/Projets/Cryptomonnaies/Cryptoportfolio/Front.git] / api / const.go
1 package api
2
3 import "net/http"
4
5 //go:generate stringer -type=Status,ErrorCode -output const_string.go
6 type Status uint32
7 type ErrorCode uint32
8
9 const EXTERNAL_SERVICE_TIMEOUT_SECONDS = 20
10
11 const (
12 OK Status = iota
13 NOK
14
15 BadRequest ErrorCode = iota + 1
16 EmailExists
17 ExternalServiceTimeout
18 InternalError
19 InvalidCredentials
20 InvalidEmail
21 InvalidMarketCredentials
22 MarketCredentialsNotConfigured
23 IPRestrictedApiKey
24 InvalidOtp
25 InvalidPassword
26 NeedOtpValidation
27 NotAuthorized
28 NotFound
29 OtpAlreadySetup
30 OtpNotSetup
31 UserNotConfirmed
32 )
33
34 func StatusToHttpCode(status Status, code ErrorCode) int {
35 if status == OK {
36 return http.StatusOK
37 }
38
39 switch code {
40 case BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey, MarketCredentialsNotConfigured:
41 return http.StatusBadRequest
42
43 case InvalidCredentials, InvalidOtp:
44 return http.StatusUnauthorized
45
46 case UserNotConfirmed, NotAuthorized, OtpAlreadySetup, OtpNotSetup, NeedOtpValidation:
47 return http.StatusForbidden
48
49 case EmailExists:
50 return http.StatusConflict
51
52 case NotFound:
53 return http.StatusNotFound
54
55 case ExternalServiceTimeout:
56 return http.StatusGatewayTimeout
57 }
58
59 return http.StatusInternalServerError
60 }