2019-05-01 13:56:18 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
// ConstError is a wrapper to set constant errors
|
|
|
|
type ConstError string
|
|
|
|
|
|
|
|
// Error implements error
|
|
|
|
func (err ConstError) Error() string {
|
|
|
|
return string(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ErrReqParamNotFound is thrown when a request parameter is not found
|
|
|
|
const ErrReqParamNotFound = ConstError("request parameter not found")
|
|
|
|
|
|
|
|
// ErrReqParamNotType is thrown when a request parameter is not asked with the right type
|
|
|
|
const ErrReqParamNotType = ConstError("request parameter does not fulfills type")
|
|
|
|
|
|
|
|
// RequestParam defines input parameters of an api request
|
|
|
|
type RequestParam map[string]interface{}
|
|
|
|
|
2019-05-02 05:48:34 +00:00
|
|
|
// Get returns the raw value (not typed) and an error if not found
|
|
|
|
func (rp RequestParam) Get(key string) (interface{}, error) {
|
2019-05-01 13:56:18 +00:00
|
|
|
rawValue, found := rp[key]
|
|
|
|
if !found {
|
|
|
|
return "", ErrReqParamNotFound
|
|
|
|
}
|
2019-05-02 05:48:34 +00:00
|
|
|
return rawValue, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetString returns a string and an error if not found or string
|
|
|
|
func (rp RequestParam) GetString(key string) (string, error) {
|
|
|
|
rawValue, err := rp.Get(key)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2019-05-01 13:56:18 +00:00
|
|
|
|
|
|
|
convertedValue, canConvert := rawValue.(string)
|
|
|
|
if !canConvert {
|
|
|
|
return "", ErrReqParamNotType
|
|
|
|
}
|
|
|
|
|
|
|
|
return convertedValue, nil
|
|
|
|
}
|