router/generic/request.go

59 lines
1.1 KiB
Go

package generic
import (
"encoding/json"
"io"
"somehole.com/service/router"
)
type RequestBuilder struct {
allowedMethods []string
header struct {
router.Header
}
values struct {
router.Values
}
Body struct{}
}
func (rb RequestBuilder) RequestBuilder() router.RequestBuilder {
return &rb
}
func (rb *RequestBuilder) Allowed(method string) (errRes router.ErrorResponse) {
var ok bool
for _, m := range rb.allowedMethods {
if m == method {
ok = true
}
}
if !ok {
return ErrorMethodNotAllowed
}
return Ok
}
func (rb *RequestBuilder) Header(header router.Header) (errRes router.ErrorResponse) {
err := rb.header.Header.Parse(header)
if err != nil {
return ErrorBadRequest
}
return Ok
}
func (rb *RequestBuilder) ReadBody(body io.ReadCloser) (errRes router.ErrorResponse) {
defer body.Close()
json.NewDecoder(body).Decode(&rb.Body)
return Ok
}
func (rb *RequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
err := rb.values.Values.Parse(values)
if err != nil {
return ErrorBadRequest
}
return Ok
}