Use Unimplemented types as base

This commit is contained in:
some 2024-10-08 21:24:55 -04:00
parent df289d1e35
commit 604dcbf426
Signed by: some
GPG Key ID: 65D0589220B9BFC8
6 changed files with 22 additions and 134 deletions

2
go.mod
View File

@ -5,7 +5,7 @@ go 1.23.1
require (
somehole.com/common/log v0.1.3
somehole.com/common/security v0.2.1
somehole.com/service/router v0.8.0
somehole.com/service/router v0.12.0
)
require (

4
go.sum
View File

@ -8,5 +8,5 @@ somehole.com/common/log v0.1.3 h1:2PAui0+5EryTAHqVUZQeepLcJTGssHKz2OL+jBknHI0=
somehole.com/common/log v0.1.3/go.mod h1:NS2eHnN120GA6oFbBm3XhB5yHww0eXTbLuMQYZxYNyA=
somehole.com/common/security v0.2.1 h1:H7TpErYKsCOxKYYie75jU7azO5vEgeqrQ9uA+AjXafA=
somehole.com/common/security v0.2.1/go.mod h1:6SMKdIrfxT460XXWafpD6A9yT/ykwipGsGztA5g5vVM=
somehole.com/service/router v0.8.0 h1:93+qKPpllgWSXGQTvBYLVK565rrFSWTxvMevepDCesY=
somehole.com/service/router v0.8.0/go.mod h1:F+/sNY4/ei7C9QD5+iA+gUHugcOAcrWspHKwX8d8oiM=
somehole.com/service/router v0.12.0 h1:nrBn+OJF9cN22vgXcZYdcSbLTamPwMEvu4NG2lJLO88=
somehole.com/service/router v0.12.0/go.mod h1:F+/sNY4/ei7C9QD5+iA+gUHugcOAcrWspHKwX8d8oiM=

View File

@ -1,17 +1,14 @@
package server
import (
"io"
"encoding/json"
"somehole.com/service/oauth2/session"
"somehole.com/service/router"
)
type CallbackRequestBuilder struct {
allowedMethods []string
header struct {
router.Header
}
router.UnimplementedRequestBuilder
values struct {
router.Values
State session.State `form:"state"`
@ -19,38 +16,8 @@ type CallbackRequestBuilder struct {
}
}
func (crb CallbackRequestBuilder) RequestBuilder() router.RequestBuilder {
return &crb
}
func (crb *CallbackRequestBuilder) Allowed(method string) (errRes router.ErrorResponse) {
var ok bool
for _, m := range crb.allowedMethods {
if m == method {
ok = true
}
}
if !ok {
return ErrorMethodNotAllowed
}
return Ok
}
func (crb *CallbackRequestBuilder) Header(header router.Header) (errRes router.ErrorResponse) {
err := crb.header.Header.Parse(header)
if err != nil {
return ErrorBadRequest
}
return Ok
}
func (crb *CallbackRequestBuilder) ReadBody(body io.ReadCloser) (errRes router.ErrorResponse) {
defer body.Close()
return Ok
}
func (crb *CallbackRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
err := crb.values.Values.Parse(values)
func (rb *CallbackRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
err := rb.values.Values.Parse(values)
if err != nil {
return ErrorBadRequest
}
@ -58,27 +25,15 @@ func (crb *CallbackRequestBuilder) Values(values router.Values) (errRes router.E
}
type CallbackResponse struct {
header struct {
router.Header
}
router.UnimplementedResponse
Body struct {
Message string `json:"message"`
}
}
func (cr CallbackResponse) Response() router.Response {
return &cr
}
func (cr *CallbackResponse) Header() (header router.Header) {
if cr.header.Header == nil {
cr.header.Header.Parse(cr.header)
}
return cr.header.Header
}
func (cr *CallbackResponse) BodyBytes() []byte {
return mustMarshalJson(cr.Body)
func (r *CallbackResponse) BodyBytes() (body []byte) {
body, _ = json.Marshal(r.Body)
return
}
type UnimplementedCallbackServer struct{}

View File

@ -1,14 +0,0 @@
package server
import (
"encoding/json"
"fmt"
)
func mustMarshalJson(in any) []byte {
out, err := json.Marshal(in)
if err != nil {
panic(fmt.Errorf("could not marshal %#v: %v", in, err))
}
return out
}

View File

@ -1,9 +1,9 @@
package server
import (
"encoding/json"
"fmt"
"net/http"
"strings"
)
type Error uint32
@ -50,14 +50,6 @@ func (e Error) Error() (out string) {
}
func (e Error) BodyBytes() (body []byte) {
var msg string
switch e {
default:
msg = strings.Join(strings.Split(e.String(), " "), "_")
}
return mustMarshalJson(struct {
Error string `json:"error"`
}{
Error: msg,
})
body, _ = json.Marshal(struct{ Error string }{Error: e.String()})
return
}

View File

@ -1,17 +1,14 @@
package server
import (
"io"
"encoding/json"
"somehole.com/service/oauth2/session"
"somehole.com/service/router"
)
type TokenRequestBuilder struct {
allowedMethods []string
header struct {
router.Header
}
router.UnimplementedRequestBuilder
values struct {
router.Values
ResponseType string `form:"response_type"`
@ -20,38 +17,8 @@ type TokenRequestBuilder struct {
}
}
func (trb TokenRequestBuilder) RequestBuilder() router.RequestBuilder {
return &trb
}
func (trb *TokenRequestBuilder) Allowed(method string) (errRes router.ErrorResponse) {
var ok bool
for _, m := range trb.allowedMethods {
if m == method {
ok = true
}
}
if !ok {
return ErrorMethodNotAllowed
}
return Ok
}
func (trb *TokenRequestBuilder) Header(header router.Header) (errRes router.ErrorResponse) {
err := trb.header.Header.Parse(header)
if err != nil {
return ErrorBadRequest
}
return Ok
}
func (trb *TokenRequestBuilder) ReadBody(body io.ReadCloser) (errRes router.ErrorResponse) {
defer body.Close()
return Ok
}
func (trb *TokenRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
err := trb.values.Values.Parse(values)
func (rb *TokenRequestBuilder) Values(values router.Values) (errRes router.ErrorResponse) {
err := rb.values.Values.Parse(values)
if err != nil {
return ErrorBadRequest
}
@ -59,9 +26,7 @@ func (trb *TokenRequestBuilder) Values(values router.Values) (errRes router.Erro
}
type TokenResponse struct {
header struct {
router.Header
}
router.UnimplementedResponse
Body struct {
VerificationUri string `json:"verification_uri"`
UserCode session.Code `json:"user_code"`
@ -71,19 +36,9 @@ type TokenResponse struct {
}
}
func (tr TokenResponse) Response() router.Response {
return &tr
}
func (tr *TokenResponse) Header() (header router.Header) {
if tr.header.Header == nil {
tr.header.Header.Parse(tr.header)
}
return tr.header.Header
}
func (tr *TokenResponse) BodyBytes() []byte {
return mustMarshalJson(tr.Body)
func (r *TokenResponse) BodyBytes() (body []byte) {
body, _ = json.Marshal(r.Body)
return
}
type UnimplementedTokenServer struct{}