diff options
| author | 2023-04-05 12:20:45 -0400 | |
|---|---|---|
| committer | 2023-04-05 12:20:45 -0400 | |
| commit | d8520538e7bfb12f36ee1f743fff9bd463db3136 (patch) | |
| tree | 65d11fde2d05064ce226466a629f4103bf69ec86 | |
| parent | janet: add convenience functions slurp and spit (diff) | |
janet: improve slurp/spit semantics
| -rw-r--r-- | jurl/init.janet | 38 |
1 files changed, 25 insertions, 13 deletions
diff --git a/jurl/init.janet b/jurl/init.janet index ad6c0a4..79952b4 100644 --- a/jurl/init.janet +++ b/jurl/init.janet @@ -272,19 +272,6 @@ :headers (text/parse-headers res-hdr) :status (handle :response-code)}) -# fast and convenient functions -(defn slurp - [url &opt opts] - (default opts {}) - (def out (request (merge opts {:url url}))) - (assert (= :ok (out :error))) - (out :body)) - -(defn spit - [url body &opt opts] - (default opts {}) - (def out (request (merge opts {:url url :method :post :body body})))) - (defn- merge-request [orig new] (let [mdict |(merge (get orig $ {}) (get new $ {}))] @@ -408,3 +395,28 @@ `` [qs] {:query qs}) + +# fast and convenient functions +(defn- verify-slurpit + [f &opt opts] + (def out (f opts)) + (assert (= :ok (out :error)) (out :error)) + (assert (and + (>= (out :status) 200) + (< (out :status) 300)) + (out :status)) + (out :body)) + +(defn slurp + [url &opt opts] + (verify-slurpit + (http :get url) + opts)) + +(defn spit + [url body &opt opts] + (verify-slurpit + (->> url + (http :post) + (body body)) + opts)) |
