Skip to content

A peer-to-peer stack for code collaboration

Create http module. (#653)

James Haydonbbe26e02c91d
diff --git a/bin/rad-replicate b/bin/rad-replicate
index 260d922..7d44673 100755
--- a/bin/rad-replicate
+++ b/bin/rad-replicate
@@ -4,6 +4,7 @@
(load! (find-module-file! "monadic/project.rad"))
(import prelude/strings '[ends-with?] :unqualified)
+(import prelude/http :as 'http)
(def default-url
"Default replication URL"
@@ -48,14 +49,12 @@
"v0/machines/"
machine
"/query"))
- (match (process-with-stdout-stderr-exitcode!
- "curl"
- ["-s" url "-d" "{ \"expression\": \"(+ 3 2)\" }" "-H" "Content-Type: application/json"]
- "")
- [_ _ :ok] :ok
- [_ _ _ ] (do
- (put-str! (string-append "Machine " machine ": error reaching replicator"))
- (exit! 1)))))
+ (catch 'http
+ (do (http/json-post! url {:expression "(+ 3 2)"})
+ :ok)
+ (fn [err]
+ (put-str! (string-append "Machine " machine ": error reaching replicator"))
+ (exit! 1)))))
(def replicate-all
(fn [murl]
diff --git a/docs/source/reference.rst b/docs/source/reference.rst
index 46b461b..44a21d1 100644
--- a/docs/source/reference.rst
+++ b/docs/source/reference.rst
@@ -453,6 +453,13 @@ Lists and vectors are converted to JSON arrays. Dicts are converted to
JSON objects as long as all the keys are strings, atoms, keywords,
booleans or numbers.
+``from-json``
+~~~~~~~~~~~~~
+
+Converts a JSON string into Radicle data. If the string is not valid
+JSON then ``:nothing`` is returned, otherwise ``[:just v]`` is returned
+where ``v`` is a Radicle representation of the JSON data.
+
``uuid!``
~~~~~~~~~
diff --git a/rad/prelude.rad b/rad/prelude.rad
index 24b347a..0bbf332 100644
--- a/rad/prelude.rad
+++ b/rad/prelude.rad
@@ -20,6 +20,7 @@
(file-module! "prelude/ref.rad")
(file-module! "prelude/lens.rad")
(file-module! "prelude/io-utils.rad")
+(file-module! "prelude/http.rad")
(file-module! "prelude/key-management.rad")
(file-module! "prelude/machine.rad")
(file-module! "prelude/machine-remote.rad")
diff --git a/rad/prelude/http.rad b/rad/prelude/http.rad
new file mode 100644
index 0000000..1f858c0
--- /dev/null
+++ b/rad/prelude/http.rad
@@ -0,0 +1,22 @@
+{:module 'prelude/http
+ :doc "Functions for making HTTP requests."
+ :exports '[json-get! json-post!]}
+
+(import prelude/strings ['intercalate] :unqualified)
+(import prelude/io :unqualified)
+
+(def json-req!
+ (fn [url extra-opts]
+ (def opts (<> ["-s" url "-H" "Accept: application/json"] extra-opts))
+ (match (process-with-stdout-stderr-exitcode! "curl" opts "")
+ ['resp _ :ok] (from-json (intercalate "" resp))
+ [_ _ 'err] (throw 'http err))))
+
+(def json-get!
+ "Makes an HTTP request for some JSON data, and parses the response."
+ (fn [url] (json-req! url [])))
+
+(def json-post!
+ "HTTP JSON post-request."
+ (fn [url data]
+ (json-req! url ["-d" (to-json data)])))
diff --git a/reference-doc.yaml b/reference-doc.yaml
index 0d6a73b..d3eaad8 100644
--- a/reference-doc.yaml
+++ b/reference-doc.yaml
@@ -98,6 +98,7 @@ primFns:
- now!
# JSON
- to-json
+- from-json
# UUIDs
- uuid!
- uuid?
diff --git a/src/Radicle/Internal/PrimFns.hs b/src/Radicle/Internal/PrimFns.hs
index c70c4ec..93aec31 100644
--- a/src/Radicle/Internal/PrimFns.hs
+++ b/src/Radicle/Internal/PrimFns.hs
@@ -577,6 +577,14 @@ purePrimFns = fromList $ allDocs $
Left e -> throwErrorHere $ OtherError $ "Could not convert to JSON: " <> e
Right js -> pure js
)
+ , ( "from-json"
+ , "Converts a JSON string into Radicle data. If the string is not valid JSON\
+ \ then `:nothing` is returned, otherwise `[:just v]` is returned where `v`\
+ \ is a Radicle representation of the JSON data."
+ , oneArg "from-json" $ \case
+ String json -> pure $ toRad $ Json.fromJson <$> Aeson.decode (toS json)
+ v -> throwErrorHere $ TypeError "from-json" 0 TString v
+ )
, ( "default-ecc-curve"
, "Returns the default elliptic-curve used for generating cryptographic keys."
,