Skip to content

Commit b753f0d

Browse files
authored
Add links to HandlerStartOperationResultSync (#30)
Adding links field to sync response to support attaching links to synchronous operations.
1 parent e803dc4 commit b753f0d

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

nexus/operation.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func (h *syncOperation[I, O]) Start(ctx context.Context, input I, options StartO
125125
if err != nil {
126126
return nil, err
127127
}
128-
return &HandlerStartOperationResultSync[O]{o}, err
128+
return &HandlerStartOperationResultSync[O]{Value: o, Links: options.Links}, err
129129
}
130130

131131
// A Service is a container for a group of operations.

nexus/server.go

+14-1
Original file line numberDiff line numberDiff line change
@@ -24,17 +24,30 @@ type HandlerStartOperationResult[T any] interface {
2424

2525
// HandlerStartOperationResultSync indicates that an operation completed successfully.
2626
type HandlerStartOperationResultSync[T any] struct {
27+
// Value is the output of the operation.
2728
Value T
29+
// Links to be associated with the operation.
30+
Links []Link
2831
}
2932

3033
func (r *HandlerStartOperationResultSync[T]) applyToHTTPResponse(writer http.ResponseWriter, handler *httpHandler) {
34+
if err := addLinksToHTTPHeader(r.Links, writer.Header()); err != nil {
35+
handler.logger.Error("failed to serialize links into header", "error", err)
36+
// clear any previous links already written to the header
37+
writer.Header().Del(headerLink)
38+
writer.WriteHeader(http.StatusInternalServerError)
39+
return
40+
}
41+
3142
handler.writeResult(writer, r.Value)
3243
}
3344

3445
// HandlerStartOperationResultAsync indicates that an operation has been accepted and will complete asynchronously.
3546
type HandlerStartOperationResultAsync struct {
47+
// OperationID is a unique ID to identify the operation.
3648
OperationID string
37-
Links []Link
49+
// Links to be associated with the operation.
50+
Links []Link
3851
}
3952

4053
func (r *HandlerStartOperationResultAsync) applyToHTTPResponse(writer http.ResponseWriter, handler *httpHandler) {

nexus/start_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ func (h *successHandler) StartOperation(ctx context.Context, service, operation
4747
return nil, HandlerErrorf(HandlerErrorTypeBadRequest, "invalid 'User-Agent' header: %q", options.Header.Get("User-Agent"))
4848
}
4949

50-
return &HandlerStartOperationResultSync[any]{body}, nil
50+
return &HandlerStartOperationResultSync[any]{Value: body, Links: options.Links}, nil
5151
}
5252

5353
func TestSuccess(t *testing.T) {

0 commit comments

Comments
 (0)