Skip to content

Commit

Permalink
Add portal.client.py
Browse files Browse the repository at this point in the history
  • Loading branch information
djblue committed Oct 30, 2024
1 parent 3e41b09 commit 681fb64
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions src/portal/client/py.lpy
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
(ns portal.client.py
(:require [basilisp.json :as json])
(:import [urllib.request :as request]))

(defn- serialize [encoding value]
(.encode
(try
(case encoding
:json (json/write-str value)
:edn (binding [*print-meta* true]
(pr-str value)))
(catch Exception ex
(serialize encoding (pr-str ex))))
"utf-8"))

(defn submit
([value] (submit nil value))
([{:keys [encoding port host]
:or {encoding :edn
host "localhost"
port 53755}}
value]
(let [req (request/Request
(str "http://" host ":" port "/submit")
** :data (serialize encoding value))]
(.add_header
req "content-type"
(case encoding
:json "application/json"
:cson "application/cson"
:transit "application/transit+json"
:edn "application/edn"))
(request/urlopen req))))

0 comments on commit 681fb64

Please sign in to comment.