2019-05-01 11:44:45 +00:00
|
|
|
package api
|
2018-06-01 08:51:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2019-04-30 22:02:28 +00:00
|
|
|
// Request represents an API request i.e. HTTP
|
|
|
|
type Request struct {
|
|
|
|
// corresponds to the list of uri components
|
2019-05-01 11:44:45 +00:00
|
|
|
// featured in the request URI
|
2019-04-30 22:02:28 +00:00
|
|
|
URI []string
|
|
|
|
|
2019-05-01 13:56:18 +00:00
|
|
|
// Scope from the configuration file of the current service
|
|
|
|
Scope [][]string
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// original HTTP request
|
|
|
|
Request *http.Request
|
2019-04-30 22:02:28 +00:00
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// input parameters
|
|
|
|
Param RequestParam
|
2019-04-30 22:02:28 +00:00
|
|
|
}
|
|
|
|
|
2019-05-01 11:44:45 +00:00
|
|
|
// NewRequest builds an interface request from a http.Request
|
2020-04-04 10:05:17 +00:00
|
|
|
func NewRequest(req *http.Request) *Request {
|
2018-07-08 23:34:21 +00:00
|
|
|
uri := normaliseURI(req.URL.Path)
|
2018-06-01 08:51:51 +00:00
|
|
|
uriparts := strings.Split(uri, "/")
|
|
|
|
|
2020-04-04 10:05:17 +00:00
|
|
|
return &Request{
|
2019-05-01 11:44:45 +00:00
|
|
|
URI: uriparts,
|
2019-05-01 13:56:18 +00:00
|
|
|
Scope: nil,
|
2019-05-01 11:44:45 +00:00
|
|
|
Request: req,
|
|
|
|
Param: make(RequestParam),
|
2018-06-01 08:51:51 +00:00
|
|
|
}
|
|
|
|
}
|
2019-04-30 22:02:28 +00:00
|
|
|
|
|
|
|
// normaliseURI removes the trailing '/' to always
|
|
|
|
// have the same Uri format for later processing
|
|
|
|
func normaliseURI(uri string) string {
|
|
|
|
|
|
|
|
if len(uri) < 1 {
|
|
|
|
return uri
|
|
|
|
}
|
|
|
|
|
|
|
|
if uri[0] == '/' {
|
|
|
|
uri = uri[1:]
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(uri) > 1 && uri[len(uri)-1] == '/' {
|
|
|
|
uri = uri[0 : len(uri)-1]
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri
|
|
|
|
}
|