diff --git a/cmd/africastalking/main.go b/cmd/africastalking/main.go index 0019239..dfcaca1 100644 --- a/cmd/africastalking/main.go +++ b/cmd/africastalking/main.go @@ -121,9 +121,7 @@ func main() { } defer stateStore.Close() - rp := &at.ATRequestParser{ - Context: ctx, - } + rp := &at.ATRequestParser{} bsh := handlers.NewBaseSessionHandler(cfg, rs, stateStore, userdataStore, rp, hl) sh := httpserver.NewATSessionHandler(bsh) diff --git a/cmd/async/main.go b/cmd/async/main.go index 9cd04b3..bf23d9f 100644 --- a/cmd/async/main.go +++ b/cmd/async/main.go @@ -21,8 +21,8 @@ import ( ) var ( - logg = logging.NewVanilla() - scriptDir = path.Join("services", "registration") + logg = logging.NewVanilla() + scriptDir = path.Join("services", "registration") menuSeparator = ": " ) @@ -35,7 +35,7 @@ type asyncRequestParser struct { input []byte } -func (p *asyncRequestParser) GetSessionId(r any) (string, error) { +func (p *asyncRequestParser) GetSessionId(ctx context.Context, r any) (string, error) { return p.sessionId, nil } diff --git a/internal/handlers/single.go b/internal/handlers/single.go index 6929617..1b11a64 100644 --- a/internal/handlers/single.go +++ b/internal/handlers/single.go @@ -6,9 +6,9 @@ import ( "io" "git.defalsify.org/vise.git/engine" - "git.defalsify.org/vise.git/resource" - "git.defalsify.org/vise.git/persist" "git.defalsify.org/vise.git/logging" + "git.defalsify.org/vise.git/persist" + "git.defalsify.org/vise.git/resource" "git.grassecon.net/urdt/ussd/internal/storage" ) @@ -20,33 +20,33 @@ var ( var ( ErrInvalidRequest = errors.New("invalid request for context") ErrSessionMissing = errors.New("missing session") - ErrInvalidInput = errors.New("invalid input") - ErrStorage = errors.New("storage retrieval fail") - ErrEngineType = errors.New("incompatible engine") - ErrEngineInit = errors.New("engine init fail") - ErrEngineExec = errors.New("engine exec fail") + ErrInvalidInput = errors.New("invalid input") + ErrStorage = errors.New("storage retrieval fail") + ErrEngineType = errors.New("incompatible engine") + ErrEngineInit = errors.New("engine init fail") + ErrEngineExec = errors.New("engine exec fail") ) type RequestSession struct { - Ctx context.Context - Config engine.Config - Engine engine.Engine - Input []byte - Storage *storage.Storage - Writer io.Writer + Ctx context.Context + Config engine.Config + Engine engine.Engine + Input []byte + Storage *storage.Storage + Writer io.Writer Continue bool } // TODO: seems like can remove this. type RequestParser interface { - GetSessionId(rq any) (string, error) + GetSessionId(context context.Context, rq any) (string, error) GetInput(rq any) ([]byte, error) } type RequestHandler interface { GetConfig() engine.Config GetRequestParser() RequestParser - GetEngine(cfg engine.Config, rs resource.Resource, pe *persist.Persister) engine.Engine + GetEngine(cfg engine.Config, rs resource.Resource, pe *persist.Persister) engine.Engine Process(rs RequestSession) (RequestSession, error) Output(rs RequestSession) (RequestSession, error) Reset(rs RequestSession) (RequestSession, error) diff --git a/internal/http/parse.go b/internal/http/parse.go index ec8e00b..b4e784d 100644 --- a/internal/http/parse.go +++ b/internal/http/parse.go @@ -1,6 +1,7 @@ package http import ( + "context" "io/ioutil" "net/http" @@ -10,7 +11,7 @@ import ( type DefaultRequestParser struct { } -func (rp *DefaultRequestParser) GetSessionId(rq any) (string, error) { +func (rp *DefaultRequestParser) GetSessionId(ctx context.Context, rq any) (string, error) { rqv, ok := rq.(*http.Request) if !ok { return "", handlers.ErrInvalidRequest @@ -34,5 +35,3 @@ func (rp *DefaultRequestParser) GetInput(rq any) ([]byte, error) { } return v, nil } - - diff --git a/internal/http/server_test.go b/internal/http/server_test.go index a46f98e..23afd5d 100644 --- a/internal/http/server_test.go +++ b/internal/http/server_test.go @@ -2,6 +2,7 @@ package http import ( "bytes" + "context" "errors" "net/http" "net/http/httptest" @@ -161,7 +162,7 @@ func TestDefaultRequestParser_GetSessionId(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { - id, err := parser.GetSessionId(tt.request) + id, err := parser.GetSessionId(context.Background(),tt.request) if id != tt.expectedID { t.Errorf("Expected session ID %s, got %s", tt.expectedID, id) diff --git a/internal/testutil/mocks/httpmocks/requestparsermock.go b/internal/testutil/mocks/httpmocks/requestparsermock.go index 54b16bf..3c19e12 100644 --- a/internal/testutil/mocks/httpmocks/requestparsermock.go +++ b/internal/testutil/mocks/httpmocks/requestparsermock.go @@ -1,12 +1,14 @@ package httpmocks +import "context" + // MockRequestParser implements the handlers.RequestParser interface for testing type MockRequestParser struct { GetSessionIdFunc func(any) (string, error) GetInputFunc func(any) ([]byte, error) } -func (m *MockRequestParser) GetSessionId(rq any) (string, error) { +func (m *MockRequestParser) GetSessionId(ctx context.Context, rq any) (string, error) { return m.GetSessionIdFunc(rq) }