Last active
August 25, 2022 01:50
-
-
Save luksamuk/bc9529a60a3aaa9f77f803ddd8e9a3cd to your computer and use it in GitHub Desktop.
Plan 9 REST Requests (with Fennel and Lu9)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(local p9 (require :p9)) | |
(local clone (p9.open "/mnt/web/clone" "read")) | |
(local n (math.tointeger (clone:read))) | |
(local webpath (.. "/mnt/web/" n "/")) ; /mnt/web/n/ | |
(print (.. "Using " webpath)) | |
(fn open-webfile [file mode] | |
(p9.open (.. webpath file) mode)) | |
(fn set-endpoint [url] | |
(let [ctl (open-webfile "ctl" "write")] | |
(ctl:write (.. "url " url)) | |
(ctl:close))) | |
(fn set-method [method] | |
(let [ctl (open-webfile "ctl" "write")] | |
(ctl:write (.. "request " method)) | |
(ctl:close))) | |
(fn set-body [type body] | |
(let [ctl (open-webfile "ctl" "write")] | |
(ctl:write (.. "contenttype " type)) | |
(ctl:close)) | |
(let [postbody (open-webfile "postbody" "write")] | |
(postbody:write body) | |
(postbody:close))) | |
(fn get-body [] | |
(let [body (open-webfile "body" "read")] | |
(if body | |
(let [result (body:read)] | |
(body:close) | |
result) | |
"ERROR"))) | |
(fn run-test [endpoint method body] | |
(print (.. " --- " method " " endpoint " --- ")) | |
(set-endpoint endpoint) | |
(set-method method) | |
(when (and body (not (= body ""))) | |
(set-body "application/json" body)) | |
(print "Performing request...") | |
(let [response (get-body)] | |
(print (.. "Response body:\n" response))) | |
(print "")) | |
;; Running tests | |
(local json "{ \"foo\": \"bar\" }") | |
(run-test "https://catfact.ninja/fact" "get") | |
(run-test "http://postman-echo.com/get" "get") | |
(run-test "http://postman-echo.com/post" "post" json) | |
(run-test "http://postman-echo.com/put" "put" json) | |
(run-test "http://postman-echo.com/patch" "patch" json) | |
(run-test "http://postman-echo.com/delete" "delete" json) | |
(clone:close) ; finish |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cpu% fennel request.fnl | |
Using /mnt/web/3/ | |
--- get https://catfact.ninja/fact --- | |
Performing request... | |
Response body: | |
{"fact":"Some Siamese cats appear cross-eyed because the nerves from the left side of the brain go to mostly the right eye and the nerves from the right side of the brain go mostly to the left eye. This causes some double vision, which the cat tries to correct by \u201ccrossing\u201d its eyes.","length":275} | |
--- get http://postman-echo.com/get --- | |
Performing request... | |
Response body: | |
{"args":{},"headers":{"x-forwarded-proto":"http","x-forwarded-port":"80","host":"postman-echo.com","x-amzn-trace-id":"Root=1-6306d49f-512426de2d23bec37e54df46","cookie":"sails.sid=s%3AgxmFcIIkxrWIOYxjZGCGz2D5ObSuuJGm.KUoxXgHlw23fQBNvOhpGRs2Df14v1OOJWW%2BeHab3NG0","user-agent":"NetSurf/3.11 (Plan9)","accept":"*/*"},"url":"http://postman-echo.com/get" | |
--- post http://postman-echo.com/post --- | |
Performing request... | |
Response body: | |
{"args":{},"data":{"foo":"bar"},"files":{},"form":{},"headers":{"x-forwarded-proto":"http","x-forwarded-port":"80","host":"postman-echo.com","x-amzn-trace-id":"Root=1-6306d49f-639654bd40140c30112f68a1","content-length":"16","cookie":"sails.sid=s%3AgxmFcIIkxrWIOYxjZGCGz2D5ObSuuJGm.KUoxXgHlw23fQBNvOhpGRs2Df14v1OOJWW%2BeHab3NG0","user-agent":"NetSurf/3.11 (Plan9)","accept":"*/*","content-type":"application/json"},"json":{"foo":"bar"},"url":"http://postman-echo.com/post" | |
--- put http://postman-echo.com/put --- | |
Performing request... | |
Response body: | |
{"args":{},"data":{"foo":"bar"},"files":{},"form":{},"headers":{"x-forwarded-proto":"http","x-forwarded-port":"80","host":"postman-echo.com","x-amzn-trace-id":"Root=1-6306d4a0-785301a71357a0e8539f4394","content-length":"16","cookie":"sails.sid=s%3AOGx5nCNMFJMQ2OGxRvSYKmYqmQTcXs7P.6bKJm4sI%2BiX4AveyqATABHEkPutsFP504uD6Rp2xdxk","user-agent":"NetSurf/3.11 (Plan9)","accept":"*/*","content-type":"application/json"},"json":{"foo":"bar"},"url":"http://postman-echo.com/put" | |
--- patch http://postman-echo.com/patch --- | |
Performing request... | |
Response body: | |
{"args":{},"data":{"foo":"bar"},"files":{},"form":{},"headers":{"x-forwarded-proto":"http","x-forwarded-port":"80","host":"postman-echo.com","x-amzn-trace-id":"Root=1-6306d4a0-7764dd315cc253132c49a7c5","content-length":"16","cookie":"sails.sid=s%3AOGx5nCNMFJMQ2OGxRvSYKmYqmQTcXs7P.6bKJm4sI%2BiX4AveyqATABHEkPutsFP504uD6Rp2xdxk","user-agent":"NetSurf/3.11 (Plan9)","accept":"*/*","content-type":"application/json"},"json":{"foo":"bar"},"url":"http://postman-echo.com/patch" | |
--- delete http://postman-echo.com/delete --- | |
Performing request... | |
Response body: | |
{"args":{},"data":{"foo":"bar"},"files":{},"form":{},"headers":{"x-forwarded-proto":"http","x-forwarded-port":"80","host":"postman-echo.com","x-amzn-trace-id":"Root=1-6306d4a1-434c6df762c86a0177876b35","content-length":"16","cookie":"sails.sid=s%3A0aneTxZFBBdDCjC1s_ozm5UrRzZQvXN-.i3uUXzve2uSdiMqqw258mD98D5C8cCERaDFA1TMGYw4","user-agent":"NetSurf/3.11 (Plan9)","accept":"*/*","content-type":"application/json"},"json":{"foo":"bar"},"url":"http://postman-echo.com/delete" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment