Skip to content

Commit

Permalink
Improve http api
Browse files Browse the repository at this point in the history
  • Loading branch information
attdona committed Jul 3, 2024
1 parent 0ad2fb3 commit f54bda9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 31 deletions.
65 changes: 35 additions & 30 deletions src/broker.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1452,20 +1452,23 @@ function authenticate_admin(router::Router, req::HTTP.Request)
return cid
end

function http_admin_msg(router, twin, msg)
admin_res = admin_command(router, twin, msg)
@debug "http admin response: $admin_res"
return admin_res
end

function command(router::Router, req::HTTP.Request, cmd::Dict)
sts = 403
cid = HTTP.getparams(req)["cid"]
topic = HTTP.getparams(req)["topic"]
twin = create_twin(cid, router)
twin.hasname = true
twin.socket = Condition()
msg = AdminReqMsg(topic, cmd)
admin_msg(router, twin, msg)
response = wait(twin.socket)
response = http_admin_msg(router, twin, msg)
if response.status == 0
sts = 200
end
destroy_twin(twin, router)
return HTTP.Response(sts, [])
end

Expand Down Expand Up @@ -1497,7 +1500,7 @@ function http_publish(router::Router, req::HTTP.Request)
twin = create_twin(cid, router)
msg = PubSubMsg(topic, content)
pubsub_msg(router, twin, msg)
destroy_twin(twin, router)
Visor.shutdown(twin.process)
return HTTP.Response(200, [])
catch e
@error "http::publish: $e"
Expand All @@ -1514,32 +1517,36 @@ function http_rpc(router::Router, req::HTTP.Request)
else
content = JSON3.read(req.body, Any)
end
twin = create_twin(cid, router)
twin.socket = Condition()
msg = RpcReqMsg(topic, content)
rpc_request(router, twin, msg)
response = wait(twin.socket)
if isa(response.data, IOBuffer)
retval = decode(response.data)
if haskey(router.id_twin, cid)
error("component $cid not available for rpc via http")
else
retval = response.data
end
twin = create_twin(cid, router)
twin.socket = Condition()
msg = RpcReqMsg(topic, content)
rpc_request(router, twin, msg)
response = wait(twin.socket)
if isa(response.data, IOBuffer)
retval = decode(response.data)
else
retval = response.data
end

if response.status == 0
sts = 200
else
sts = 403
end
if response.status == 0
sts = 200
else
sts = 403
end

destroy_twin(twin, router)
return HTTP.Response(
sts,
["Content_type" => "application/json"],
JSON3.write(retval)
)
Visor.shutdown(twin.process)
return HTTP.Response(
sts,
["Content_type" => "application/json"],
JSON3.write(retval)
)
end
catch e
@error "http::rpc: $e"
return HTTP.Response(403, [])
return HTTP.Response(404, [])
end
end

Expand All @@ -1549,11 +1556,9 @@ function http_admin_command(
try
cid = authenticate_admin(router, req)
twin = create_twin(cid, router)
twin.socket = Condition()
msg = AdminReqMsg(topic, cmd)
admin_msg(router, twin, msg)
response = wait(twin.socket)
destroy_twin(twin, router)
response = http_admin_msg(router, twin, msg)
Visor.shutdown(twin.process)
if response.status === STS_SUCCESS
if response.data !== nothing
return HTTP.Response(200, JSON3.write(response.data))
Expand Down
12 changes: 11 additions & 1 deletion test/http/test_http.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
include("../utils.jl")

# tests: 10
using Base64

# tests: 11

function mytopic(x, y)
@info "[test_http] mytopic($x,$y)"
Expand Down Expand Up @@ -53,6 +55,14 @@ function run()
@test Rembus.body(response) === nothing
@test response.status === Int16(200)

# It is not permitted to use a name of a connected component
# with HTTP rest api
auth = Base64.base64encode("myapp")
@test_throws HTTP.Exceptions.StatusError HTTP.get(
"http://localhost:9000/version",
["Authorization" => auth]
)

@terminate
end

Expand Down

2 comments on commit f54bda9

@attdona
Copy link
Member Author

@attdona attdona commented on f54bda9 Jul 3, 2024

Choose a reason for hiding this comment

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

@JuliaRegistrator register()

@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/110342

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.3.0 -m "<description of version>" f54bda99ea661659783564324452bb0f6bfe0ad7
git push origin v0.3.0

Please sign in to comment.