Skip to content

Commit bf1ddd3

Browse files
committed
Implement basic RPC API
1 parent 69ce9b2 commit bf1ddd3

4 files changed

Lines changed: 37 additions & 7 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
## 0.1 2025-06-17
22

3+
- Implement context param validation
34
- Preliminary version

lib/syntropy/rpc_api.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,6 @@ def initialize(mount_path)
1212

1313
def call(ctx)
1414
response, status = invoke(ctx)
15-
p response: response
16-
p status: status
1715
ctx.request.respond(
1816
response.to_json,
1917
':status' => status,
@@ -22,7 +20,7 @@ def call(ctx)
2220
end
2321

2422
def invoke(ctx)
25-
q = ctx.params[:q]
23+
q = ctx.validate_param(:q, String)
2624
response = case ctx.request.method
2725
when 'get'
2826
send(q.to_sym, ctx)
@@ -33,7 +31,10 @@ def invoke(ctx)
3331
end
3432
[{ status: 'OK', response: response }, Qeweney::Status::OK]
3533
rescue => e
36-
p e.backtrace
34+
if !e.is_a?(Syntropy::Error)
35+
p e
36+
p e.backtrace
37+
end
3738
error_response(e)
3839
end
3940

test/helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,15 @@ def response_status
8484
adapter.status
8585
end
8686

87+
def response_body
88+
adapter.body
89+
end
90+
91+
def response_json
92+
raise if response_content_type != 'application/json'
93+
JSON.parse(response_body, symbolize_names: true)
94+
end
95+
8796
def response_content_type
8897
response_headers['Content-Type']
8998
end

test/test_rpc_api.rb

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ def get(ctx)
1010

1111
def set!(ctx)
1212
@value = ctx.params[:v]
13+
true
1314
end
1415
end
1516

@@ -23,10 +24,28 @@ def test_kernel_version
2324
assert_in_range 600..700, v
2425
end
2526

26-
def test_req_harness
27+
def test_rpc_api
2728
req = mock_req(':method' => 'GET', ':path' => '/foo')
2829
ctx = Syntropy::Context.new(req)
29-
ret = @app.call(ctx)
30-
assert_equal Qeweney::Status::NOT_FOUND, req.response_status
30+
@app.call(ctx)
31+
assert_equal Qeweney::Status::BAD_REQUEST, req.response_status
32+
33+
req = mock_req(':method' => 'GET', ':path' => '/foo?q=get')
34+
ctx = Syntropy::Context.new(req)
35+
@app.call(ctx)
36+
assert_equal Qeweney::Status::OK, req.response_status
37+
assert_equal({ status: 'OK', response: nil }, req.response_json)
38+
39+
req = mock_req(':method' => 'POST', ':path' => '/foo?q=set&v=foo')
40+
ctx = Syntropy::Context.new(req)
41+
@app.call(ctx)
42+
assert_equal Qeweney::Status::OK, req.response_status
43+
assert_equal({ status: 'OK', response: true }, req.response_json)
44+
45+
req = mock_req(':method' => 'GET', ':path' => '/foo?q=get')
46+
ctx = Syntropy::Context.new(req)
47+
@app.call(ctx)
48+
assert_equal Qeweney::Status::OK, req.response_status
49+
assert_equal({ status: 'OK', response: 'foo' }, req.response_json)
3150
end
3251
end

0 commit comments

Comments
 (0)