Skip to content

Commit

Permalink
Use generated types + stuck at return string not being jsonified and …
Browse files Browse the repository at this point in the history
…apikey not in stub
  • Loading branch information
sverhoeven committed Sep 4, 2024
1 parent c0eb014 commit bb8d880
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 9 deletions.
5 changes: 5 additions & 0 deletions RemoteBMI.jl/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,9 @@ The openapi server stubs where generated using the following command:
```shell
npx @openapitools/openapi-generator-cli generate -i ./openapi.yaml -g julia-server -o julia-server --additional-properties=packageName=BmiServer --additional-properties=exportModels=true
# Copy the generated files to RemoteBMI.jl/src/

# TODO find out why api key is not in the generated code
# At example https://github.com/JuliaComputing/OpenAPI.jl/blob/2d447986b6377a6724ae59380f087c2b270e7795/test/server/openapigenerator_petstore_v3/petstore/src/apis/api_PetApi.jl#L56
# It is given to impl function as second argument
# but in the generated code it is missing
```
10 changes: 6 additions & 4 deletions RemoteBMI.jl/example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ dev ..
CTRL-D
# Run server
export BMI_SERVER_PORT=50555
export BMI_SERVER_SECRET="somesecret"
julia --project=$PWD heat_bmi_server.jl
```

Expand All @@ -24,16 +25,17 @@ Interact with it using the Python client.

```python
from remotebmi.client.client import RemoteBmiClient
from remotebmi.client.reserve import reserve_values
from remotebmi.reserve import reserve_values

client = RemoteBmiClient('http://localhost:50555')
# TODO use placeholder for path
# TODO use placeholder for path instead of path on my machine
# client.initialize('<absolute path>/heat.toml')
client.initialize('/home/stefanv/git/eWaterCycle/remotebmi/python/heat.toml')
# TODO Julia server throws error here
client.initialize('/home/verhoes/git/eWaterCycle/remotebmi/python/heat.toml')
# TODO eget_component_name errors because server returns plain/text instead of json
client.get_component_name()
client.update()
client.get_current_time()
client.get_var_type('plate_surface__temperature')
dest = reserve_values(client, 'plate_surface__temperature')
r = client.get_value('plate_surface__temperature', dest)
r
Expand Down
5 changes: 3 additions & 2 deletions RemoteBMI.jl/example/heat_bmi_server.jl
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ using Heat

import RemoteBMI

port = parse(Int, get(ENV, "BMI_PORT", "50051"))
RemoteBMI.run(Heat.Model, "0.0.0.0", port)
port = parse(Int, get(ENV, "BMI_SERVER_PORT", "50051"))
secret = get(ENV, "BMI_SERVER_SECRET", "")
RemoteBMI.run(Heat.Model, "0.0.0.0", port, secret)
18 changes: 15 additions & 3 deletions RemoteBMI.jl/src/RemoteBMI.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,25 @@ module RemoteBMI

using HTTP

# Treat returned string as something that must be JSON serialized
# At https://github.com/JuliaComputing/OpenAPI.jl/blob/2d447986b6377a6724ae59380f087c2b270e7795/src/server.jl#L167
# string is force tod return as plain text
# Treat returned string from route implementations as thing that must be JSON serialized
# Without this, the string would be returned as plain text
#using OpenAPI
#using OpenAPI.Servers
#OpenAPI.Servers.server_response(resp::AbstractString, headers=HTTP.Headers()) = OpenAPI.Servers.server_response(OpenAPI.to_json(resp), [Pair("Content-Type", "application/json")])
# TODO find way that does not break OpenAPI.Servers.server_response as with overwrite a client request is timing out
# TODO Alternative is to return plain/text in paths that return strings

include("./BmiServer.jl")
using .BmiServer

import BasicModelInterface as BMI

# TODO move route implementations to own module

function initialize(req::HTTP.Request, bmi_initialize_request::BmiInitializeRequest)::Nothing
function initialize(req::HTTP.Request, bmi_initialize_request::InitializeRequest)::Nothing
global m
m = BMI.initialize(MyModel, bmi_initialize_request.config_file)
return nothing
Expand Down Expand Up @@ -119,6 +130,7 @@ function reserve_grid_coords(m, grid::Int64, dim_index::Int8)::Vector{Float64}
size = BMI.get_grid_node_count(m, grid)
else
error("Unsupported grid type: $mtype")
end
return zeros(Float64, size)
end

Expand All @@ -141,7 +153,7 @@ function set_value(req::HTTP.Request, name::String, request_body::Vector{Float64
BMI.set_value(m, name, request_body)
end

function set_value_at_indices(req::HTTP.Request, name::String, bmi_set_value_at_indices_request::BmiSetValueAtIndicesRequest;)::Nothing
function set_value_at_indices(req::HTTP.Request, name::String, bmi_set_value_at_indices_request::SetValueAtIndicesRequest;)::Nothing
BMI.set_value_at_indices(m, name, bmi_set_value_at_indices_request)
end

Expand Down Expand Up @@ -274,7 +286,7 @@ function get_var_units(req::HTTP.Request, name::String;)::String
return BMI.get_var_units(m, name)
end

function run(model, host, port)
function run(model, host, port, trusted_apikey)
global MyModel = model
try
router = HTTP.Router()
Expand Down

0 comments on commit bb8d880

Please sign in to comment.