]>
Commit | Line | Data |
---|---|---|
7a9e5112 | 1 | package api |
2 | ||
4495b984 | 3 | import ( |
4 | "net/http" | |
7a9e5112 | 5 | |
4495b984 | 6 | "github.com/jloup/utils" |
7 | ) | |
8 | ||
9 | //go:generate stringer -type=Status -output const_string.go | |
7a9e5112 | 10 | type Status uint32 |
7a9e5112 | 11 | |
24e47979 | 12 | const EXTERNAL_SERVICE_TIMEOUT_SECONDS = 20 |
2f91f20a | 13 | |
7a9e5112 | 14 | const ( |
15 | OK Status = iota | |
16 | NOK | |
4495b984 | 17 | ) |
7a9e5112 | 18 | |
4495b984 | 19 | var ( |
20 | errorFlagCounter utils.Counter = 0 | |
21 | BadRequest utils.Flag = utils.InitFlag(&errorFlagCounter, "BadRequest") | |
22 | EmailExists utils.Flag = utils.InitFlag(&errorFlagCounter, "EmailExists") | |
23 | ExternalServiceTimeout utils.Flag = utils.InitFlag(&errorFlagCounter, "ExternalServiceTimeout") | |
24 | InternalError utils.Flag = utils.InitFlag(&errorFlagCounter, "InternalError") | |
25 | InvalidCredentials utils.Flag = utils.InitFlag(&errorFlagCounter, "InvalidCredentials") | |
26 | InvalidEmail utils.Flag = utils.InitFlag(&errorFlagCounter, "InvalidEmail") | |
27 | InvalidMarketCredentials utils.Flag = utils.InitFlag(&errorFlagCounter, "InvalidMarketCredentials") | |
28 | MarketCredentialsNotConfigured utils.Flag = utils.InitFlag(&errorFlagCounter, "MarketCredentialsNotConfigured") | |
29 | IPRestrictedApiKey utils.Flag = utils.InitFlag(&errorFlagCounter, "IPRestrictedApiKey") | |
30 | InvalidOtp utils.Flag = utils.InitFlag(&errorFlagCounter, "InvalidOtp") | |
31 | InvalidPassword utils.Flag = utils.InitFlag(&errorFlagCounter, "InvalidPassword") | |
32 | NeedOtpValidation utils.Flag = utils.InitFlag(&errorFlagCounter, "NeedOtpValidation") | |
33 | NotAuthorized utils.Flag = utils.InitFlag(&errorFlagCounter, "NotAuthorized") | |
34 | NotFound utils.Flag = utils.InitFlag(&errorFlagCounter, "NotFound") | |
35 | OtpAlreadySetup utils.Flag = utils.InitFlag(&errorFlagCounter, "OtpAlreadySetup") | |
36 | OtpNotSetup utils.Flag = utils.InitFlag(&errorFlagCounter, "OtpNotSetup") | |
37 | UserNotConfirmed utils.Flag = utils.InitFlag(&errorFlagCounter, "UserNotConfirmed") | |
38 | ||
39 | StatusBadRequest utils.Flag = utils.Join("BadRequest", BadRequest, InvalidPassword, InvalidEmail, InvalidMarketCredentials, IPRestrictedApiKey, MarketCredentialsNotConfigured) | |
40 | StatusUnauthorized utils.Flag = utils.Join("Unauthorized", InvalidCredentials, InvalidOtp) | |
41 | StatusForbidden utils.Flag = utils.Join("Forbidden", UserNotConfirmed, NotAuthorized, OtpAlreadySetup, OtpNotSetup, NeedOtpValidation) | |
42 | StatusConflicts utils.Flag = utils.Join("Conflicts", EmailExists) | |
43 | StatusNotFound utils.Flag = utils.Join("NotFound", NotFound) | |
44 | StatusGatewayTimeout utils.Flag = utils.Join("GatewayTimeout", ExternalServiceTimeout) | |
7a9e5112 | 45 | ) |
46 | ||
4495b984 | 47 | func StatusToHttpCode(status Status, code utils.Flag) int { |
7a9e5112 | 48 | if status == OK { |
49 | return http.StatusOK | |
50 | } | |
51 | ||
4495b984 | 52 | switch { |
53 | case utils.Intersect(code, StatusBadRequest): | |
7a9e5112 | 54 | return http.StatusBadRequest |
4495b984 | 55 | case utils.Intersect(code, StatusUnauthorized): |
7a9e5112 | 56 | return http.StatusUnauthorized |
4495b984 | 57 | case utils.Intersect(code, StatusForbidden): |
7a9e5112 | 58 | return http.StatusForbidden |
4495b984 | 59 | case utils.Intersect(code, StatusConflicts): |
7a9e5112 | 60 | return http.StatusConflict |
4495b984 | 61 | case utils.Intersect(code, StatusNotFound): |
7a9e5112 | 62 | return http.StatusNotFound |
4495b984 | 63 | case utils.Intersect(code, StatusGatewayTimeout): |
2f91f20a | 64 | return http.StatusGatewayTimeout |
7a9e5112 | 65 | } |
66 | ||
67 | return http.StatusInternalServerError | |
68 | } |