Fork me on GitHub
#babashka
<
2022-03-23
>
stagmoose16:03:32

hi, if I want to use curl (https://github.com/babashka/babashka.curl) in babashka to send http POST request of the content of "pic 1", how could I do that? How could I put "content-type" when calling curl/post And is it required to convert map to json like I did using cheshire.core

(ns )


(require '[babashka.curl :as curl])
(require '[cheshire.core :as json])

(def req
  (json/generate-string {:apiKey "xxxxx"
                         :userId "xxxxx"
                         :name "Personal"}))


(curl/post "" req)
I could use online api test tool to get the response successfully after http POST (like pic. 3) (btw, the "Raw" tab in the pic 3 is pic 1)

stagmoose16:03:43

@U04V15CAJ thanks for the quick apply! I'll try it out. Many thanks!🙇

👍 1
borkdude16:03:03

The next version of babashka is going to have support for pods in bb.edn:

{:pods {org.babashka/go-sqlite3 {:pod/version "0.1.0"}}
 :tasks {:requires ([pod.babashka.go-sqlite3 :as sqlite])
         create-db {:task (do (sqlite/execute! "/tmp/foo.db"
                                               ["create table if not exists foo (the_text TEXT, the_int INTEGER, the_real REAL, the_blob BLOB)"]))}}}
Thanks to @cap10morgan . This is already available on master. To try it out:
bash <(curl ) --version 0.7.9-SNAPSHOT --dir .

🎉 6
borkdude16:03:00

Also, when you declare the pods in your bb.edn, you no longer have to use load-pod in your code, the pod is automatically loaded whenever a related namespace is required.

Benjamin16:03:49

(ns
    example-ns
    (:require [clojure.core.async])
    (:import
     [clojure.core.async.impl.channels
      ManyToManyChannel]))


(defn -chan? [x]
  (instance? ManyToManyChannel x))
I'm trying to do the same trick from tasks.clj but why
Message:  Unable to resolve classname: clojure.core.async.impl.channels.ManyToManyChannel

borkdude16:03:57

This class isn't exposed, but I will do that, since there is no other way to check if a chan is a chan...

Benjamin16:03:09

I see kinda

borkdude16:03:15

@benjamin.schwerdtner Hmm, I guess you can still do this:

(.getName (class (a/chan))) ;;=> 
"clojure.core.async.impl.channels.ManyToManyChannel"
and then compare the string

👍 1
Benjamin17:03:56

the following code:

(async/<!!
   (async/go
     (async/alts!
      [(async/go
         (async/<! (async/timeout 100))
         10)])))
on babashka: nil on jvm: [10 #object[clojure.core.async.impl.channels.ManyToManyChannel 0x1343db14 "clojure.core.async.impl.channels.ManyToManyChannel@1343db14"]]

1
borkdude17:03:47

Issue welcome

Benjamin17:03:40

ok I make an issue because I have no idea why 😛

borkdude17:03:01

Workaround:

(async/<!!
 (async/thread
   (async/alts!!
    [(async/thread
       (async/<!! (async/timeout 100))
       10)])))

Benjamin17:03:23

now I can keep going 5min 😛

Kyle Kingsbury18:03:06

Hey y'all! Is there a way to get a stacktrace that's not just the babashka/sci interpreter?

(defn evil []
  (/ 1 0))

(try
  (evil)
  (catch Throwable t
    t))

borkdude18:03:45

Currently this is only available from outside of SCI...

Kyle Kingsbury18:03:29

Ah, gotcha--so if I want my program itself to catch an error and print a stacktrace, I'm not gonna have access to the babashka frames?

borkdude18:03:57

Currently not, maybe this can be fixed somehow.

Kyle Kingsbury18:03:47

OK! Just wanted to make sure I wasn't missing something. Thanks for Babashka! 🙂

👍 1
borkdude18:03:36

Not sure how, since not all exception types take extra data and if you use try/catch you just get the "naked" exception. If you would not catch the exception, then the SCI evaluation throws an ex-info with the "frames" which can be printed using sci/stack-trace and sci/format-stacktrace

Kyle Kingsbury18:03:25

Right, right. I'm doing something a bit weird and writing a long-running daemon (well, 10 seconds) in Babashka--it needs to log errors and keep running

borkdude11:07:23

@U038RGYDGUR FYI, going on in another thread about this. I think I may have found a solution. https://clojurians.slack.com/archives/CLX41ASCS/p1657302458137849

Kyle Kingsbury18:03:51

This bottoms out in

[[clojure.lang.Numbers divide "Numbers.java" 188]
  [clojure.core$_SLASH_ invokeStatic "core.clj" 1029]
  [clojure.core$_SLASH_ invoke "core.clj" 1022]
  [sci.impl.analyzer$return_call$fn__8904 invoke "analyzer.cljc" 1036]
  [sci.impl.evaluator$eval invokeStatic "evaluator.cljc" 334
... which makes it kind of hard to tell where the error actually happened

Dig22:03:21

slight update/improvement on bash completion

# babashka tasks completer
_bb_complete() {
    BB_TASKS=$(bb tasks|bb -io '(->> *input* (drop 2) (map #(-> % (str/split #" ") first)))')
    BB_HELP=$(bb help|bb -io '(->> *input* (map #(->> % (re-find #"^  ([-a-z]+)") second)) (filter some?))')
    COMPREPLY=($(compgen -W "$BB_TASKS $BB_HELP" -- "${COMP_WORDS[$COMP_CWORD]}"))
}
complete -f -F _bb_complete bb # autocomplete filenames as well

borkdude23:03:40

Awesome! That looks similar to this PR https://github.com/babashka/book/pull/46/. but yours has more I think, so this is a superset of that PR?

borkdude23:03:36

If so, then maybe we could merge your PR or first merge his PR and then yours on top

borkdude23:03:17

I went ahead and merged his

borkdude23:03:27

so if you want to add /change something to that, please go ahead

Dig23:03:45

mine is quite more progress from that change to use compgen it does not use tail or sed, just bb it is hard for me to submit PRs right now i would just overwrite it with my version

Dig23:03:20

try it out if you like it

borkdude23:03:29

I don't use bash, but zsh

borkdude23:03:40

but I'm fine with using your version in the book

Dig23:03:52

hehe, you are much more advance then

Dig23:03:42

done

❤️ 1