Skip to content

Commit

Permalink
Merge pull request stakwork#1956 from MahtabBukhari/Implement_Workflo…
Browse files Browse the repository at this point in the history
…w_Request_Handler

Implement Workflow Request Handler Create Workflow Processing Utilities
  • Loading branch information
humansinstitute authored Nov 22, 2024
2 parents d95a2f1 + cc78ffb commit 5d2af28
Show file tree
Hide file tree
Showing 3 changed files with 120 additions and 9 deletions.
27 changes: 20 additions & 7 deletions handlers/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@ package handlers

import (
"encoding/json"
"io"
"net/http"

"github.com/google/uuid"
"github.com/stakwork/sphinx-tribes/db"
"github.com/stakwork/sphinx-tribes/utils"

"io"
"net/http"
)

type workflowHandler struct {
Expand All @@ -20,6 +21,7 @@ func NewWorkFlowHandler(database db.Database) *workflowHandler {
}

func (wh *workflowHandler) HandleWorkflowRequest(w http.ResponseWriter, r *http.Request) {

body, err := io.ReadAll(r.Body)
if err != nil {
http.Error(w, "Error reading request body", http.StatusBadRequest)
Expand All @@ -32,10 +34,18 @@ func (wh *workflowHandler) HandleWorkflowRequest(w http.ResponseWriter, r *http.
return
}

if request.RequestID == "" {
request.RequestID = uuid.New().String()
if request.WorkflowID == "" || request.Source == "" {
http.Error(w, "Missing required fields: workflow_id or source", http.StatusBadRequest)
return
}

processedRequestID, err := utils.ProcessWorkflowRequest(request.RequestID, request.Source)
if err != nil {
http.Error(w, "Failed to process workflow request", http.StatusInternalServerError)
return
}

request.RequestID = processedRequestID
request.Status = db.StatusNew

if err := wh.db.CreateWorkflowRequest(&request); err != nil {
Expand All @@ -44,7 +54,10 @@ func (wh *workflowHandler) HandleWorkflowRequest(w http.ResponseWriter, r *http.
}

w.WriteHeader(http.StatusCreated)
json.NewEncoder(w).Encode(request)
json.NewEncoder(w).Encode(map[string]string{
"request_id": processedRequestID,
"status": "success",
})
}

func (wh *workflowHandler) HandleWorkflowResponse(w http.ResponseWriter, r *http.Request) {
Expand Down Expand Up @@ -106,4 +119,4 @@ func (wh *workflowHandler) HandleWorkflowResponse(w http.ResponseWriter, r *http
"status": "success",
"request_id": response.RequestID,
})
}
}
65 changes: 63 additions & 2 deletions handlers/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package handlers
import (
"bytes"
"encoding/json"

"net/http"
"net/http/httptest"
"testing"
Expand All @@ -13,12 +14,72 @@ import (
"github.com/stretchr/testify/assert"
)

func TestHandleWorkflowResponse(t *testing.T) {
func TestHandleWorkflowRequest(t *testing.T) {

teardownSuite := SetupSuite(t)
defer teardownSuite(t)

wh := NewWorkFlowHandler(db.TestDB)

t.Run("successful workflow request", func(t *testing.T) {

request := &db.WfRequest{
Source: "test_source_1",
Action: "test_action_1",
WorkflowID: "test_workflow",
}

body, _ := json.Marshal(request)

req := httptest.NewRequest(http.MethodPost, "/workflows/request", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()

wh.HandleWorkflowRequest(w, req)

assert.Equal(t, http.StatusCreated, w.Code)

var respBody map[string]string
err := json.NewDecoder(w.Body).Decode(&respBody)
assert.NoError(t, err)
assert.Equal(t, "success", respBody["status"])
assert.NotEmpty(t, respBody["request_id"])
})

t.Run("invalid JSON format", func(t *testing.T) {
req := httptest.NewRequest(http.MethodPost, "/workflows/request", bytes.NewBuffer([]byte("invalid-json")))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()

wh.HandleWorkflowRequest(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "Invalid request format")
})

t.Run("missing required fields", func(t *testing.T) {
request := db.WfRequest{
Source: "test_source",
}
body, _ := json.Marshal(request)

req := httptest.NewRequest(http.MethodPost, "/workflows/request", bytes.NewBuffer(body))
req.Header.Set("Content-Type", "application/json")
w := httptest.NewRecorder()

wh.HandleWorkflowRequest(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Contains(t, w.Body.String(), "Missing required fields: workflow_id or source")
})

}
func TestHandleWorkflowResponse(t *testing.T) {

teardownSuite := SetupSuite(t)
defer teardownSuite(t)

wh := NewWorkFlowHandler(db.TestDB)
t.Run("should process workflow response successfully", func(t *testing.T) {

requestData := db.PropertyMap{
Expand Down Expand Up @@ -252,4 +313,4 @@ func TestHandleWorkflowResponse(t *testing.T) {
assert.Equal(t, db.StatusPending, updatedReq.Status)
assert.Equal(t, response.ResponseData, updatedReq.ResponseData)
})
}
}
37 changes: 37 additions & 0 deletions utils/workflow_processor.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package utils

import (
"encoding/json"
"fmt"
"github.com/google/uuid"
)

type ProcessorConfig struct {
RequiresProcessing bool
HandlerFunc string
Config json.RawMessage
}

func ProcessWorkflowRequest(requestID string, source string) (string, error) {

if requestID == "" {
requestID = uuid.New().String()
}

config := lookupProcessingConfig(source)

if config == nil {
return requestID, nil
}

return processWithHandler(requestID)
}

func lookupProcessingConfig(source string) error {
return nil
}

func processWithHandler(requestID string) (string, error) {
fmt.Println("Processing with default handler")
return fmt.Sprintf("Processed with default handler, RequestID: %s", requestID), nil
}

0 comments on commit 5d2af28

Please sign in to comment.