A Read-Eval-Print-Loop (REPL) shell to demonstrate the KnowSheet Bricks C++ syntax.
Node.js v0.12.0 is required to install and run the REPL.
git clone https://github.com/KnowSheet/KnowSheet-REPL.git
cd KnowSheet-REPL
npm installnode runThe command prompt KnowSheet> will appear.
echo 'HTTP(GET("http://httpbin.org/get?query=1")).body' | node runIf stdin is not a tty, the REPL starts in a non-interactive mode.
echo 'HTTP(GET("http://httpbin.org/get?query=1")).body' | node run | catIf stdout is not a tty, the result is emitted as text into stdout, the errors are emitted as text into stderr.
node run --prompt 'Bricks> 'The command prompt Bricks> will appear.
Bricks provides utilities to perform HTTP requests. By default, redirects are forbidden and result in an error; they can be enabled per request.
KnowSheet> HTTP(GET("http://httpbin.org/get?query=1")).body
KnowSheet> HTTP(POST("http://httpbin.org/post", "BODY", "text/plain")).body
KnowSheet> HTTP(GET("http://httpbin.org/redirect/5").AllowRedirects()).body
KnowSheet> HTTP(GET("http://httpbin.org/get").UserAgent("KnowSheet-REPL/1.0.0")).body
For GET requests:
KnowSheet> HTTP(GET("http://httpbin.org/get?query=1", HTTPHeaders().Set("Custom", "Header"))).body
For POST requests:
KnowSheet> HTTP(POST("http://httpbin.org/post", "BODY", "text/plain", HTTPHeaders().Set("Custom", "Header"))).body
Bricks provides utilities to create simple HTTP-based servers.
KnowSheet> HTTP(2015)
// KnowSheet Bricks HTTPServer at port 2015
- Bricks maintains a single server per port. No need to explicitly start a server: the first call to
HTTPwith a port number starts a server on that port and returns an instance of the server; the next calls with the same port reference the started server. - The calls to the server methods can be chained (each method returns the server instance).
- Each server accepts connections since its start till the process exits. No need to explicitly stop it.
KnowSheet> HTTP(2015).Register("/ping", [](Request r) { r("pong"); })
// KnowSheet Bricks HTTPServer at port 2015
KnowSheet> HTTP(GET("http://localhost:2015/ping")).body
--------------------------------------------------------------------------------
pong
--------------------------------------------------------------------------------
(32ms)
- There can only be a single handler for an endpoint path. The handler is expected to dispatch by HTTP method (verb).
- The handler lambda syntax in the REPL mimics C++11 to a certain extent -- no full lambda support is guaranteed.
KnowSheet> HTTP(2015).UnRegister("/ping")
KnowSheet> HTTP(2015).Register("/test", [](Request r){ r(r.timestamp + " " + r.method + " " + r.url.ComposeURL() + "\n" + r.body); })
// KnowSheet Bricks HTTPServer at port 2015
KnowSheet> HTTP(POST("localhost:2015/test", "BODY")).body
--------------------------------------------------------------------------------
1426024237701 POST /test
BODY
--------------------------------------------------------------------------------
(12ms)
Bricks provides utilities to parse JSON strings into instances of serializable types and serialize them back into JSON. The REPL mimics Bricks C++ syntax to a certain extent.
KnowSheet> HTTP(POST("http://httpbin.org/post", DemoObject())).body
The syntax mimics C++ Bricks exactly, as long as DemoObject is defined as a serializable type.
KnowSheet> ParseJSON(HTTP(GET("http://httpbin.org/get?query=1")).body).args
The syntax deviates from C++ Bricks which requires a serializable type specified for the response object, for example, auto response = ParseJSON<HttpbinGetResponse>(response_text); or HttpbinGetResponse response; ParseJSON(response_text, response);.
KnowSheet> ParseJSON(HTTP(POST("http://httpbin.org/post", ParseJSON(HTTP(GET("http://httpbin.org/get?query=1")).body))).body)
In C++ Bricks, the calls to ParseJSON would be templated by serializable types of the response objects, for example, ParseJSON<HttpbinPostResponse>( /* ... */ ).