Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add Go helper functions for HttpResponse #2622

Merged
merged 4 commits into from
Sep 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,7 @@ type QueryParamRequest struct {

//ftl:ingress http GET /queryparams
func Query(ctx context.Context, req builtin.HttpRequest[ftl.Unit, ftl.Unit, QueryParamRequest]) (builtin.HttpResponse[string, string], error) {
return builtin.HttpResponse[string, string]{
Body: ftl.Some(req.Query.Foo.Default("No value")),
}, nil
return builtin.HttpSuccess[string](200, req.Query.Foo.Default("No value")), nil
}

//ftl:ingress http GET /html
Expand Down Expand Up @@ -138,7 +136,7 @@ func Int(ctx context.Context, req builtin.HttpRequest[int, ftl.Unit, ftl.Unit])

//ftl:ingress http POST /float
func Float(ctx context.Context, req builtin.HttpRequest[float64, ftl.Unit, ftl.Unit]) (builtin.HttpResponse[float64, string], error) {
return builtin.HttpResponse[float64, string]{Body: ftl.Some(req.Body)}, nil
return builtin.HttpSuccess[string](200, req.Body), nil
}

//ftl:ingress http POST /bool
Expand All @@ -148,10 +146,7 @@ func Bool(ctx context.Context, req builtin.HttpRequest[bool, ftl.Unit, ftl.Unit]

//ftl:ingress http GET /error
func Error(ctx context.Context, req builtin.HttpRequest[ftl.Unit, ftl.Unit, ftl.Unit]) (builtin.HttpResponse[ftl.Unit, string], error) {
return builtin.HttpResponse[ftl.Unit, string]{
Status: 500,
Error: ftl.Some("Error from FTL"),
}, nil
return builtin.HttpError[ftl.Unit](500, "Error from FTL"), nil
}

//ftl:ingress http POST /array/string
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,3 +93,17 @@ func init() {
)
}
{{- end}}
{{- if eq .Module.Name "builtin"}}

// A helper for successful HttpResponse
//
func HttpSuccess[E any, B any](code int, body B) HttpResponse[B, E] {
return HttpResponse[B, E]{Status: code, Body: ftl.Some(body)}
}

// A helper for HttpResponse with error
//
func HttpError[B any, E any](code int, e E) HttpResponse[B, E] {
return HttpResponse[B, E]{Status: code, Error: ftl.Some(e)}
}
{{- end}}
Loading