diff options
| author | Teddy Wing | 2021-01-24 04:33:57 +0100 | 
|---|---|---|
| committer | Teddy Wing | 2021-01-24 04:33:57 +0100 | 
| commit | 336f3761ab63ac95d12a7699a387b6fee17c852d (patch) | |
| tree | f125a223a8e165ef8a7a857cf66b80250ed52a05 /l/src | |
| parent | 1daff6888cf830ad9dbfd20dd22b0c58eb6b713d (diff) | |
| download | extreload-336f3761ab63ac95d12a7699a387b6fee17c852d.tar.bz2 | |
main.lisp: Parse `Target.getTargets` response
Use `jsown` to parse the response from the `Target.getTargets` message.
Get a list of `targetInfos` from the response.
Extracting keys from the JSON result with `jsown:val` raises an `error`
exception when the key is not present. Turn the exception into `nil`
with the `json-obj-get` function.
Diffstat (limited to 'l/src')
| -rw-r--r-- | l/src/main.lisp | 22 | 
1 files changed, 19 insertions, 3 deletions
| diff --git a/l/src/main.lisp b/l/src/main.lisp index d78f027..50acdc3 100644 --- a/l/src/main.lisp +++ b/l/src/main.lisp @@ -5,9 +5,7 @@  (defun main ()    (wsd:start-connection *client*) -  (wsd:on :message *client* -          (lambda (message) -            (format t "~&Got: ~A~%" message))) +  (wsd:on :message *client* #'ws-on-message)    (wsd:send *client* (get-targets-msg 1)) @@ -19,3 +17,21 @@    (jsown:to-json      `(:obj ("id" . ,call-id)             ("method" . "Target.getTargets")))) + +(defun ws-on-message (message) +  (let* ((response (jsown:parse message)) +         (targets (parse-get-targets-response response))) +    (format t "~&Got: ~A~%" targets))) + +(defun parse-get-targets-response (response) +  (let* ((result (json-obj-get response "result")) +         (targetInfos (json-obj-get result "targetInfos"))) +    targetInfos)) + +(defun json-obj-get (obj key) +  (handler-case +    (jsown:val obj key) +    (simple-error (e) +                  (let ((s (format nil "~A" e))) +                    (if (search "not available" s) +                        nil))))) | 
