visedriver/request/http/parse.go

38 lines
711 B
Go
Raw Normal View History

2025-01-04 09:02:44 +01:00
package http
import (
2025-01-06 06:50:53 +01:00
"context"
2025-01-04 09:02:44 +01:00
"io/ioutil"
"net/http"
2025-01-11 22:47:57 +01:00
"git.grassecon.net/grassrootseconomics/visedriver/errors"
2025-01-04 09:02:44 +01:00
)
type DefaultRequestParser struct {
}
2025-01-06 06:50:53 +01:00
func (rp *DefaultRequestParser) GetSessionId(ctx context.Context, rq any) (string, error) {
2025-01-04 09:02:44 +01:00
rqv, ok := rq.(*http.Request)
if !ok {
2025-01-11 22:47:57 +01:00
return "", errors.ErrInvalidRequest
2025-01-04 09:02:44 +01:00
}
v := rqv.Header.Get("X-Vise-Session")
if v == "" {
2025-01-11 22:47:57 +01:00
return "", errors.ErrSessionMissing
2025-01-04 09:02:44 +01:00
}
return v, nil
}
func (rp *DefaultRequestParser) GetInput(rq any) ([]byte, error) {
rqv, ok := rq.(*http.Request)
if !ok {
2025-01-11 22:47:57 +01:00
return nil, errors.ErrInvalidRequest
2025-01-04 09:02:44 +01:00
}
defer rqv.Body.Close()
v, err := ioutil.ReadAll(rqv.Body)
if err != nil {
return nil, err
}
return v, nil
}