package router import ( "encoding/json" "fmt" "io" "net/http" ) type UnimplementedError uint32 const ( UnimplementedOk UnimplementedError = http.StatusOK UnimplementedErrorNotImplemented UnimplementedError = http.StatusNotImplemented UnimplementedErrorMethodNotAllowed UnimplementedError = http.StatusMethodNotAllowed UnimplementedErrorBadRequest UnimplementedError = http.StatusBadRequest UnimplementedErrorUnauthorized UnimplementedError = http.StatusUnauthorized UnimplementedErrorServerError UnimplementedError = http.StatusInternalServerError ) func (e UnimplementedError) Ok() (ok bool) { return e == UnimplementedOk } func (e UnimplementedError) HttpStatus() (code int) { return int(e) } func (e UnimplementedError) String() (out string) { switch e { case UnimplementedOk: out = "ok" case UnimplementedErrorNotImplemented: out = "server not implemented" case UnimplementedErrorMethodNotAllowed: out = "method not allowed" case UnimplementedErrorBadRequest: out = "bad request" case UnimplementedErrorUnauthorized: out = "user unauthorized" case UnimplementedErrorServerError: out = "internal server error" default: out = "unhandled error" } return } func (e UnimplementedError) Error() (out string) { return fmt.Sprintf("%s (%d)", e.String(), e.HttpStatus()) } func (e UnimplementedError) BodyBytes() (body []byte) { body, _ = json.Marshal(struct{ Error string }{Error: e.String()}) return } type UnimplementedRequestBuilder struct { allowedMethods []string header struct { Header } values struct { Values } Body struct{} } func (rb UnimplementedRequestBuilder) RequestBuilder() RequestBuilder { return &rb } func (rb *UnimplementedRequestBuilder) Allowed(method string) (errRes ErrorResponse) { var ok bool for _, m := range rb.allowedMethods { if m == method { ok = true } } if !ok { return UnimplementedErrorMethodNotAllowed } return UnimplementedOk } func (rb *UnimplementedRequestBuilder) Header(header Header) (errRes ErrorResponse) { err := rb.header.Header.Parse(header) if err != nil { return UnimplementedErrorBadRequest } return UnimplementedOk } func (rb *UnimplementedRequestBuilder) ReadBody(body io.ReadCloser) (errRes ErrorResponse) { defer body.Close() json.NewDecoder(body).Decode(&rb.Body) return UnimplementedOk } func (rb *UnimplementedRequestBuilder) Values(values Values) (errRes ErrorResponse) { err := rb.values.Values.Parse(values) if err != nil { return UnimplementedErrorBadRequest } return UnimplementedOk } type UnimplementedResponse struct { header struct { Header } Body struct{} } func (r UnimplementedResponse) Response() Response { return &r } func (r *UnimplementedResponse) Header() (header Header) { if r.header.Header == nil { r.header.Header.Parse(r.header) } return r.header.Header } func (r *UnimplementedResponse) BodyBytes() (body []byte) { body, _ = json.Marshal(r.Body) return }