Remove unimplemented package

This commit is contained in:
some 2024-10-08 21:23:02 -04:00
parent eba04df948
commit 89a0f458c7
Signed by: some
GPG Key ID: 65D0589220B9BFC8
4 changed files with 129 additions and 143 deletions

129
unimplemented.go Normal file
View File

@ -0,0 +1,129 @@
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
}

View File

@ -1,55 +0,0 @@
package unimplemented
import (
"encoding/json"
"fmt"
"net/http"
)
type Error uint32
const (
Ok Error = http.StatusOK
ErrorNotImplemented Error = http.StatusNotImplemented
ErrorMethodNotAllowed Error = http.StatusMethodNotAllowed
ErrorBadRequest Error = http.StatusBadRequest
ErrorUnauthorized Error = http.StatusUnauthorized
ErrorServerError Error = http.StatusInternalServerError
)
func (e Error) Ok() (ok bool) {
return e == Ok
}
func (e Error) HttpStatus() (code int) {
return int(e)
}
func (e Error) String() (out string) {
switch e {
case Ok:
out = "ok"
case ErrorNotImplemented:
out = "server not implemented"
case ErrorMethodNotAllowed:
out = "method not allowed"
case ErrorBadRequest:
out = "bad request"
case ErrorUnauthorized:
out = "user unauthorized"
case ErrorServerError:
out = "internal server error"
default:
out = "unhandled error"
}
return
}
func (e Error) Error() (out string) {
return fmt.Sprintf("%s (%d)", e.String(), e.HttpStatus())
}
func (e Error) BodyBytes() (body []byte) {
body, _ = json.Marshal(struct{ Error string }{Error: e.String()})
return
}

View File

@ -1,58 +0,0 @@
package unimplemented
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
}

View File

@ -1,30 +0,0 @@
package unimplemented
import (
"encoding/json"
"somehole.com/service/router"
)
type Response struct {
header struct {
router.Header
}
Body struct{}
}
func (r Response) Response() router.Response {
return &r
}
func (r *Response) Header() (header router.Header) {
if r.header.Header == nil {
r.header.Header.Parse(r.header)
}
return r.header.Header
}
func (r *Response) BodyBytes() (body []byte) {
body, _ = json.Marshal(r.Body)
return
}