-
Notifications
You must be signed in to change notification settings - Fork 206
/
Copy pathrequester.go
51 lines (45 loc) · 1.46 KB
/
requester.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
package reqrespupdate
import (
"context"
"fmt"
"go.temporal.io/sdk/client"
)
// Requester can request uppercasing of strings.
type Requester struct {
options RequesterOptions
}
// RequesterOptions are options for NewRequester.
type RequesterOptions struct {
// Client to the Temporal server. Required.
Client client.Client
// ID of the workflow listening for signals to uppercase. Required.
TargetWorkflowID string
}
// NewRequester creates a new Requester for the given options.
func NewRequester(options RequesterOptions) (*Requester, error) {
if options.Client == nil {
return nil, fmt.Errorf("client required")
} else if options.TargetWorkflowID == "" {
return nil, fmt.Errorf("target workflow required")
}
return &Requester{options}, nil
}
// RequestUppercase sends a request and returns a response.
func (r *Requester) RequestUppercase(ctx context.Context, str string) (string, error) {
// Send request and poll on an interval for response
handle, err := r.options.Client.UpdateWorkflow(ctx, client.UpdateWorkflowOptions{
WorkflowID: r.options.TargetWorkflowID,
UpdateName: UpdateHandler,
WaitForStage: client.WorkflowUpdateStageCompleted,
Args: []interface{}{Request{Input: str}},
})
if err != nil {
return "", fmt.Errorf("failed updating workflow: %w", err)
}
var response Response
err = handle.Get(ctx, &response)
if err != nil {
return "", fmt.Errorf("failed getting update response: %w", err)
}
return response.Output, nil
}