Add an option to always use HTTP 200 on the /v3 API

This commit is contained in:
luk3yx 2020-10-04 14:54:52 +13:00
parent c7840406ca
commit a5e8911855
5 changed files with 22 additions and 13 deletions

View File

@ -22,6 +22,9 @@ encounters errors processing your request, this will be false and an error code
will be added to the response. Otherwise, the response data (if any) will be in will be added to the response. Otherwise, the response data (if any) will be in
the `result` key. the `result` key.
The `X-Force-OK` header can be set to `true` to force a `200 OK` reply even
when an error occurs.
### Examples ### Examples
```json ```json

View File

@ -172,3 +172,12 @@ func MakeHTTPRouter(db lurkcoin.Database, config *Config) *httprouter.Router {
addV2API(router, db, config.Name) addV2API(router, db, config.Name)
return router return router
} }
func isYes(s string) bool {
switch strings.ToLower(s) {
case "true", "yes", "y", "1":
return true
default:
return false
}
}

View File

@ -157,15 +157,6 @@ func v2Post(router *httprouter.Router, db lurkcoin.Database, url string,
router.POST(url, f2) router.POST(url, f2)
} }
func v2IsYes(s string) bool {
switch strings.ToLower(s) {
case "true", "yes", "y", "1":
return true
default:
return false
}
}
func addV2API(router *httprouter.Router, db lurkcoin.Database, func addV2API(router *httprouter.Router, db lurkcoin.Database,
lurkcoinName string) { lurkcoinName string) {
@ -205,7 +196,7 @@ func addV2API(router *httprouter.Router, db lurkcoin.Database,
} }
_, err = r.Server.Pay("", target, targetServer, _, err = r.Server.Pay("", target, targetServer,
amount, v2IsYes(f.Get("local_currency")), true) amount, isYes(f.Get("local_currency")), true)
if err != nil { if err != nil {
return nil, err return nil, err
} }
@ -284,7 +275,7 @@ func addV2API(router *httprouter.Router, db lurkcoin.Database,
transaction.String(), transaction.String(),
} }
} }
if v2IsYes(f.Get("as_object")) { if isYes(f.Get("as_object")) {
_, exc := r.Server.GetExchangeRate(c1, false) _, exc := r.Server.GetExchangeRate(c1, false)
return map[string]interface{}{ return map[string]interface{}{
"exchange_rate": json.RawMessage(exc.String()), "exchange_rate": json.RawMessage(exc.String()),

View File

@ -56,8 +56,14 @@ func v3WrapHTTPHandler(db lurkcoin.Database, autoLogin bool,
var c int var c int
res["success"] = false res["success"] = false
res["error"], res["message"], c = lurkcoin.LookupError(err.Error()) res["error"], res["message"], c = lurkcoin.LookupError(err.Error())
// Workaround for limitations of Minetest's HTTP API
if isYes(r.Header.Get("X-Force-OK")) {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(c) w.WriteHeader(c)
} }
}
// TODO: Possibly write JSON directly to the ResponseWriter. // TODO: Possibly write JSON directly to the ResponseWriter.
raw, enc_err := json.Marshal(res) raw, enc_err := json.Marshal(res)

View File

@ -35,7 +35,7 @@ import (
) )
const SYMBOL = "¤" const SYMBOL = "¤"
const VERSION = "3.0.4" const VERSION = "3.0.5"
// Note that public source code is required by the AGPL // Note that public source code is required by the AGPL
const SOURCE_URL = "https://github.com/luk3yx/lurkcoin-core" const SOURCE_URL = "https://github.com/luk3yx/lurkcoin-core"