Skip to content

Commit 384ec81

Browse files
committed

File tree

1 file changed

+30
-0
lines changed

1 file changed

+30
-0
lines changed

api/handler.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,33 @@ func FromWorkflow[Input any, Output any](f func(polycode.WorkflowContext, Input)
4444
c.JSON(http.StatusOK, output)
4545
}
4646
}
47+
48+
func ExecService(c *gin.Context, tenantId string, partitionKey string, service string, method string,
49+
options polycode.TaskOptions, input any, outputTransform func(any) (any, error)) {
50+
apiCtx, err := apicontext.FromContext(c)
51+
if err != nil {
52+
c.JSON(http.StatusInternalServerError, gin.H{
53+
"error": "Failed to execute controller: " + err.Error(),
54+
})
55+
return
56+
}
57+
58+
s := apiCtx.Service(service).WithTenantId(tenantId).WithPartitionKey(partitionKey).Get()
59+
output, err := s.RequestReply(options, method, input).GetAny()
60+
if err != nil {
61+
c.JSON(http.StatusInternalServerError, gin.H{
62+
"error": "Failed to execute controller: " + err.Error(),
63+
})
64+
return
65+
}
66+
67+
transformedOutput, err := outputTransform(output)
68+
if err != nil {
69+
c.JSON(http.StatusInternalServerError, gin.H{
70+
"error": "Failed to execute controller: " + err.Error(),
71+
})
72+
return
73+
}
74+
75+
c.JSON(http.StatusOK, transformedOutput)
76+
}

0 commit comments

Comments
 (0)