Skip to content

Commit

Permalink
Merge pull request #64 from JuliaComputing/tan/misc
Browse files Browse the repository at this point in the history
helper method to create HTTP response with code
  • Loading branch information
tanmaykm authored Dec 6, 2023
2 parents 9b6bf34 + 2003e05 commit 85d0dd1
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ keywords = ["Swagger", "OpenAPI", "REST"]
license = "MIT"
desc = "OpenAPI server and client helper for Julia"
authors = ["JuliaHub Inc."]
version = "0.1.20"
version = "0.1.21"

[deps]
Base64 = "2a0f44e3-6c83-55bd-87e4-b1978d98bd5f"
Expand Down
12 changes: 12 additions & 0 deletions docs/src/userguide.md
Original file line number Diff line number Diff line change
Expand Up @@ -198,6 +198,18 @@ Optional middlewares can be one or more of:
The order in which middlewares are invoked is:
`init |> read |> pre_validation |> validate |> pre_invoke |> invoke |> post_invoke`

## Responses

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.

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`.

```julia
HTTP.Response(code::Integer, o::APIModel)
```

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.

## Streaming Responses

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:
Expand Down
4 changes: 4 additions & 0 deletions src/server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ function to_param(T, source::Vector{HTTP.Forms.Multipart}, name::String; require
end
end

function HTTP.Response(code::Integer, o::APIModel)
return HTTP.Response(code, [Pair("Content-Type", "application/json")], to_json(o))
end

server_response(resp::HTTP.Response) = resp
server_response(::Nothing) = server_response("")
server_response(ret) =
Expand Down
10 changes: 10 additions & 0 deletions test/client/allany/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ include(joinpath(@__DIR__, "AllAnyClient", "src", "AllAnyClient.jl"))
using .AllAnyClient
using Test
using JSON
using HTTP
using OpenAPI
using OpenAPI.Clients
import OpenAPI.Clients: Client
Expand Down Expand Up @@ -146,4 +147,13 @@ function test_debug()
end
end

function test_http_resp()
resp = HTTP.Response(200, dog)

@test resp.status == 200
@test resp.headers == ["Content-Type" => "application/json"]
json = JSON.parse(String(copy(resp.body)))
@test pet_equals(OpenAPI.Clients.from_json(M.Dog, json), dog)
end

end # module AllAnyTests
4 changes: 4 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -149,4 +149,8 @@ include("forms/forms_client.jl")
run_tests_with_servers && servers_running && stop_server(8081, ret, out)
end
end

@testset "Helper Methods" begin
AllAnyTests.test_http_resp()
end
end

2 comments on commit 85d0dd1

@tanmaykm
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/96603

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.1.21 -m "<description of version>" 85d0dd1863fa027f9acc266853416d077c8a30a2
git push origin v0.1.21

Please sign in to comment.