This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-11-06
Channels
- # announcements (12)
- # babashka (34)
- # beginners (85)
- # calva (76)
- # cider (14)
- # clj-kondo (49)
- # cljs-dev (32)
- # clojure (418)
- # clojure-europe (3)
- # clojure-france (4)
- # clojure-italy (17)
- # clojure-losangeles (8)
- # clojure-nl (5)
- # clojure-norway (2)
- # clojure-spec (2)
- # clojure-uk (88)
- # clojuredesign-podcast (4)
- # clojurescript (49)
- # clojurex (75)
- # clr (2)
- # core-async (13)
- # cursive (6)
- # datomic (57)
- # duct (31)
- # emacs (6)
- # fulcro (25)
- # graalvm (67)
- # graphql (13)
- # hoplon (1)
- # java (6)
- # juxt (11)
- # kaocha (5)
- # keechma (2)
- # leiningen (16)
- # mount (1)
- # off-topic (19)
- # pathom (2)
- # pedestal (1)
- # re-frame (11)
- # reagent (21)
- # reitit (22)
- # rewrite-clj (1)
- # shadow-cljs (98)
- # spacemacs (5)
- # sql (16)
- # tools-deps (8)
- # vim (28)
- # xtdb (4)
Hello everyone... does someone have a simple example of cljs-http
? I'm very new to Clojure and can't seem to wrap my head around cljs-http
. The README.md
's examples haven't helped either...
My test setup is a file.json
in a directory (the file contains a JSON string). For serving, I'm using python3 -m http.server
(it creates a simple server on localhost:8000
by default serving the contents of the current folder).
What I've been unable to achieve is just get the contents of the file.json
as-is (`curl` and opening the URL in the browser work fine... I tested with Python's requests package too)... What I need is something in Clojurescript please đ
Have you added [org.clojure/core.async "0.4.500"]
as a dependency and the imports/requires listed at the top of the cljs-http readme to your namespace?
cljs-http isn't very newbie-friendly due to it using core.async
. If you'd prefer to start off with simple callbacks, https://github.com/JulianBirch/cljs-ajax uses this
thanks for the response @UCW9TUDNK... I'll give cljs-ajax a shot too Though I'm perplexed why this simple test isn't working :thinking_face: This is what I got when I ran your example
dev:example.core=> (go (prn (<! (http/get ""))))
#object[cljs.core.async.impl.channels.ManyToManyChannel]
{:status 0, :success false, :body "", :headers {}, :trace-redirects ["" ""], :error-code :http-error, :error-text " [0]"}
I'm getting the same error actually! Using npm serve
to create a web server serving a json file. I'm also getting this error in my browser Access to XMLHttpRequest at '
So I'm guessing it's a CORS policy problem between the browser and these minimal web servers
Interesting... yeah, the request to http://api.github.com worked for me too :man-facepalming: But I don't see the CORS issue in my browser
huh?? I set up a full spring-boot application (for which I was preparing the client and it ran directly) Is this something we should report to the developer?
Sorry, I don't understand what you mean? Are you still getting the same 0 status response?
and the data I was looking for got printed đ
It's strange why the simple python3 -m http.server
didn't work ... In fact my idea (since I'm new to Clojure) was to figure out handling JSON using this python server first and I'd look into the authentication etc. with our actual server later. But this worked in the first attempt:
(go (prn (<! (http/get "" {:basic-auth {:username "user" :password "PassW0rt"}}))))
ahh that's great to hear! Yeah, I think it's definitely something with these micro http servers not being fully compliant.

hi, I am using clojure.spec
heavily on my project and I started to have duplicated entries during my tests because of a mis-understanding of the parameter distinct
in the s/coll-of
function. This is my example:
(s/def ::value int?)
(s/def ::name string?)
(s/def ::insta (s/keys :req [::value ::name]))
(s/def ::list-values (s/coll-of ::insta :distinct true :into [] :count 3))
I would need to generate values where either ::name
and ::value
are distincts for every element of ::list-values
.right now, it only certifies that the whole map called ::insta
is distinct. but it's possible to have equal ::value
and different ::name
for example
Hello everybody, I would to know if I can kick off a one command line simple http server in Clojure like Python does python3 -m http.server
?
With tools.deps for eg. ? clj -Sdeps '{:deps {some/clojars {:mvn/version "x.x.x"}}}' -m http.server
This librarie does exactly the job ! https://github.com/kachayev/nasus
@rahul080327 Yes it works, thx. Any chance to find equivalent without lein and any config ? With Clojure cli (tools.deps) ? I found this usefull list of one line http server command : https://gist.github.com/willurd/5720255. Clojure and lein-simpleton is mentionned in comments section.
does anyone uses @r0manâs https://github.com/r0man/cljs-http here. I have a problem when i try to send a JWT key with http header it can be send via post method but get method header can not be attached
[response (<! (http/get (str config/api-url "/api/v1/customer/" id)
{:with-credentials? false
:headers {"Authorization" @(rf/subscribe [:jwt-token])}
}))]
This matches my code, which works fine:
(http/get api-uri
{:with-credentials? false
:headers (if jwt-token
{"Authorization" jwt-token}
{})})
Hey, when I run
(do
(for [a [1 2 3]] (println a))
(for [a [4 5 6]] (println a)))
it only prints out 4, 5 and 6 (when I was expecting 1,2,3,4,5,6), why is it the case ?oh actually I think I got it, for is lazy but as do returns the last expression the second one is evaluated
i think this is weird behavior i didn't undertand the reason why it doesn't print 1,2,3
ended up doing
(dorun
(concat
(for [a [1 2 3]] (println a))
(for [a [4 5 6]] (println a))))
@m373h4n the reason is for
gives you a lazy sequences so do
as two lazy sequences and returns the last one which forces evaluation of the second one
@thomas.ormezzano doseq
is mostly the same as for, but runs for side effects instead of lazily producing data
so (doseq [a [1 2 3]] (println a)) (doseq [a [4 5 6]] (println a))
or (doseq [a (concat [1 2 3] [4 5 6]] (println a))
or just (run! println (concat [1 2 3] [4 5 6]))
(run! is like map, but for side effects)
still can't figure out how to solve my problem with clojure.spec
(s/def ::value int?)
(s/def ::name string?)
(s/def ::insta (s/keys :req [::value ::name]))
(s/def ::list-values (s/coll-of ::insta :distinct true :into [] :count 3))
I would need to both ::name
and ::value
to be distinct for every element in the collection ::list-values
I am trying to use custom generators
to do that:
(gen/not-empty
(check-gen/list-distinct-by
::value
(gen/hash-map ::value (-> ::value s/gen) ::name (-> ::name s/gen))))
not sure if the right direction, but at least I have control over the distinct values in the map createdcan you write a function that verifies this property? if so, s/and an invocation of that function with your spec
oh, you're on the generator side
well, won't help you with generation
but you could just gen and then filter
way easier..
(s/def ::list-values
(s/and (s/coll-of ::insta :distinct true :into [] :count 3)
#(check-distinct-keys %)))
(defn- check-distinct-keys [entry]
(and (apply distinct? (map ::value entry))
(apply distinct? (map ::name entry))))
This librarie does exactly the job ! https://github.com/kachayev/nasus
how do I bake a text file into a jar with tools.deps? Do I use :extra-paths
? Is there already a resources
convention?
Although I guess really it depends on the tool making the jar
is there a way to define this inline?
(defn- wrap [x]
{:body x
:statusCode 200})
something so I could do this?
(-> (something)
#({:body % :statusCode 200})
(.flush ....)
)
you can literally use (wrap)
in your second form as it's defined there
or even ((fn [x] {... ...}))
inline (but that's bad style)
I think he means he doesn't want to have to define wrap but doesn't want to have to do weird stuff to jump out of the thread
cljs.user=> (-> 2
#_=> (as-> x {:body x :statusCode 200})
#_=> )
{:body 2, :statusCode 200}
@U050MP39D: thanks!
people, I'm new to spec generators
I want to generate an instant after 2010
(defn gen-MMyyDate []
(gen/fmap #(.format (java.text.SimpleDateFormat. "MMyy") %)
(s/gen inst?)))
Take a look at gen/such-that
https://clojure.github.io/test.check/clojure.test.check.generators.html#var-such-that
Is it possible to return a collection of elements as is-- as in not wrapped in a list? I have this
(defn fields
[config & args]
(for [f args]
[:field config f]))
(def layout
[[:section {:name :initial
:label "Initial"}
[:field {:class "unique-class" :label "Number?"} :_att1]
(fields {:class "common-class"}
:_att2
:_att3
:_att4)]])
This returns what I expect, except ideally I would like to have the fields function return the vectors outside of a list. So the output ideally would look like:
[[:section
{:name :initial, :label "Initial"}
[:field {:class "my-class", :label "Hello"} :_att1]
[:field {:class "common-class"} :_att2]
[:field {:class "common-class"} :_att3]
[:field {:class "common-class"} :_att4]]]
Instead of
[[:section
{:name :initial, :label "Initial"}
[:field {:class "my-class", :label "Hello"} :_att1]
([:field {:class "common-class"} :_att2]
[:field {:class "common-class"} :_att3]
[:field {:class "common-class"} :_att4])]]
You want into
(def layout [(into [:section ,,, [:field ,,,]] (fields ,,,))])
(I think I have the nesting right there)
the fact that into
(or concat etc.) are needed here actually touches on one of my favorite things about clojure: you can't alter the semantics of a parent form from a child
there's a predictable meaning that isn't changed via "spooky" interactions
so you can't change the arg count of a parent based on how the child is implemented
which is why you need some explicit combining-of-collections operation to be done
@mario.cordova.862 Then you'll want two into
calls and it might be clearer to thread them...
another option, especially since you aren't using quoted symbols here, is using `, with ~@, but into is more idiomatic with hiccup
(def layout
[(-> [:section {:name :initial :label "Initial"} [:field ,,,]]
(into (fields ,,,))
(into [[:field ,,,] [:field ,,,]]))])
honestly I'm not sure why this never caught on in reagent code
user=> `[:a ~@(range 3)]
[:a 0 1 2]
maybe I am forgetting some gotchasorry to bother you guys I have the simple JS code and would love to port to clojure isolating the side effects how can I do that ?
var invoicesService = require('./service-layer/users')
function createInvoiceWebHandler(request) {
const user = request.currentUser
invoicesService.createInvoice(user, request)
}
var invoicesRepository = require('./data-later-layer/users')
var mailer = require('./mailer')
function createInvoice(user, invoice) {
const invoiceTotal = invoice.total
const itemsTotal = invoice.items.reduce((acc, item) => acc + (item.unitValue * item.qty), 0)
if (itemsTotal != invoiceTotal) {
throw "Invoite items sum different from invoice total"
}
const savedInvoice = invoicesRepository.save(invoice)
mailer.sendInvoice(user.email, savedInvoice)
}
why isolate side effects? every single line is a side effect isn't it?
the interop translation is pretty mechanical, mostly just moving a (
one token to the left
My point is , how to write test easily for that function . The service layer depends of ârepositoryâ and âmailerâ . How easily can I test
Probably the example I gave is not the best , as you said , we have side effect everywhere ... my solution for the problem was injecting the dependency which make my test super easy . A better example I will give you , news to understand how you guys do idiomatic clojure
var invoicesService = require('./service-layer/users')
function createInvoiceWebHandler(request) {
const user = request.currentUser
invoicesService.createInvoice(user, request)
}
var invoicesRepository = require('./data-later-layer/users')
var mailer = require('./mailer')
function createInvoice(user, invoice) {
const invoiceTotal = invoice.total
const itemsTotal = invoice.items.reduce((acc, item) => acc + (item.unitValue * item.qty), 0)
if (itemsTotal != invoiceTotal) {
throw "Invoite items sum different from invoice total"
}
const savedInvoice = invoicesRepository.save(invoice)
mailer.sendInvoice(user.email, savedInvoice)
}
to better understand my point, I did some changes on the sample to introduce some ânon-side-effectâ functionality. my point is, everyone is always saying âplease, pure fuctions everywhere, isolate the side effectsâ , just need to see how clojurians is doing on real code. ( code is updated already )