Skip to content

Commit 85d0dd1

Browse files
authored
Merge pull request #64 from JuliaComputing/tan/misc
helper method to create HTTP response with code
2 parents 9b6bf34 + 2003e05 commit 85d0dd1

File tree

5 files changed

+31
-1
lines changed

5 files changed

+31
-1
lines changed

Project.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ keywords = ["Swagger", "OpenAPI", "REST"]
44
license = "MIT"
55
desc = "OpenAPI server and client helper for Julia"
66
authors = ["JuliaHub Inc."]
7-
version = "0.1.20"
7+
version = "0.1.21"
88

99
[deps]
1010
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"

docs/src/userguide.md

+12
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,18 @@ Optional middlewares can be one or more of:
198198
The order in which middlewares are invoked is:
199199
`init |> read |> pre_validation |> validate |> pre_invoke |> invoke |> post_invoke`
200200

201+
## Responses
202+
203+
The server APIs can return the Julia type that is specified in the OpenAPI specification. The response is serialized as JSON and sent back to the client. The default HTTP response code used in this case is 200.
204+
205+
To return a custom HTTP response code, the server API can return a `HTTP.Response` instance directly. The OpenAPI package provides a overridden constructor for `HTTP.Response` that takes the desired HTTP code and the Julia struct that needs to be serialized as JSON and sent back to the client. It also sets the `Content-Type` header to `application/json`.
206+
207+
```julia
208+
HTTP.Response(code::Integer, o::APIModel)
209+
```
210+
211+
Structured error messages can also be returned in similar fashion. Any uncaught exception thrown by the server API is caught and converted into a `HTTP.Response` instance with the HTTP code set to 500 and the exception message as the response body.
212+
201213
## Streaming Responses
202214

203215
Some OpenAPI implementations implement streaming of responses by sending more than one items in the response, each of which is of the type declared as the return type in the specification. E.g. the [Twitter OpenAPI specification](https://api.twitter.com/2/openapi.json) that keeps sending tweets in JSON like this forever:

src/server.jl

+4
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ function to_param(T, source::Vector{HTTP.Forms.Multipart}, name::String; require
109109
end
110110
end
111111

112+
function HTTP.Response(code::Integer, o::APIModel)
113+
return HTTP.Response(code, [Pair("Content-Type", "application/json")], to_json(o))
114+
end
115+
112116
server_response(resp::HTTP.Response) = resp
113117
server_response(::Nothing) = server_response("")
114118
server_response(ret) =

test/client/allany/runtests.jl

+10
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ include(joinpath(@__DIR__, "AllAnyClient", "src", "AllAnyClient.jl"))
44
using .AllAnyClient
55
using Test
66
using JSON
7+
using HTTP
78
using OpenAPI
89
using OpenAPI.Clients
910
import OpenAPI.Clients: Client
@@ -146,4 +147,13 @@ function test_debug()
146147
end
147148
end
148149

150+
function test_http_resp()
151+
resp = HTTP.Response(200, dog)
152+
153+
@test resp.status == 200
154+
@test resp.headers == ["Content-Type" => "application/json"]
155+
json = JSON.parse(String(copy(resp.body)))
156+
@test pet_equals(OpenAPI.Clients.from_json(M.Dog, json), dog)
157+
end
158+
149159
end # module AllAnyTests

test/runtests.jl

+4
Original file line numberDiff line numberDiff line change
@@ -149,4 +149,8 @@ include("forms/forms_client.jl")
149149
run_tests_with_servers && servers_running && stop_server(8081, ret, out)
150150
end
151151
end
152+
153+
@testset "Helper Methods" begin
154+
AllAnyTests.test_http_resp()
155+
end
152156
end

0 commit comments

Comments
 (0)