Fork me on GitHub
#babashka
<
2022-06-30
>
folcon13:06:33

I've just started looking at https://github.com/babashka/pod-babashka-aws/ and I'm using it with holy-lambda in an aws lambda and I'm very unclear how I'm supposed to do a http get / post? There does seem to be a http-client inside the pod's source, but when I try and require it, it's not there. How exactly am I supposed to make http requests to other services etc?

borkdude13:06:52

How would you do that with cognitect aws api?

borkdude13:06:20

You can also try https://github.com/grzm/awyeah-api which works from source

folcon13:06:13

Sorry, I suppose the question is a bit wider, where is the http client in babashka then? Is it inside the client? I've seen references to curl, but I'm not sure I should be calling on curl in a lambda context?

folcon13:06:54

I'm using it for the first time to do some lambda development, so I'm still unfamiliar with the environment 😃

borkdude13:06:43

@U0JUM502E Ah right. For http you have the following options:

borkdude13:06:01

- babashka.curl
- org.httpkit.client
- java.net.http
or a wrapper for java.net.http like https://github.com/schmee/java-http-clj

borkdude13:06:23

Calling curl from a lambda context, not sure if it works, but you have the other options as well

folcon14:06:39

So for example, I just add:

java-http-clj/java-http-clj {:mvn/version "0.4.3"}
in my deps.edn, and then I should be able to require it at my repl?

borkdude14:06:55

you add it to bb.edn

borkdude14:06:10

which is similar to deps.edn in this regard

folcon14:06:29

So what's the relationship between them, there are some examples where they suggest putting it in deps.edn, are they independent?

borkdude14:06:51

bb does not read from deps.edn by default

borkdude14:06:07

there might be outdated examples - if you can point me to those, please let me know

folcon14:06:50

I don't know if they're ones you control or if it's just using a different paradigm, for example this was super confusing. I assume it works some other way... https://github.com/FieryCod/holy-lambda/tree/master/examples/bb/native/cognitect.aws.api

folcon14:06:15

Ok, that seems to be working 😃... Not sure if it's going to break when it comes time to deploy it, which is what I'll test next! Thanks for your help @U04V15CAJ 😃...

borkdude14:06:30

Sure! Let me know if you have any further questions :)

Karol Wójcik15:06:27

@U0JUM502E this is example of using cognitect AWS API with native backend whereas you’re using babashka one ;D

folcon15:06:28

Makes sense, thanks

folcon15:06:08

@U04V15CAJ, just trying out clj-http instead and I'm getting this:

(require '[clj-http.client :as client])
: Unable to resolve classname: org.apache.commons.codec.binary.Base64 humble-aws-fn.core 

folcon15:06:55

I've added imports, but it seems to not be picking it up? Basically I need form handling for the http client, so I can send forms, which java-http-clj doesn't appear to cover?

borkdude15:06:00

What's form handling?

borkdude15:06:49

Can you give an example?

folcon15:06:55

Sure, for example:

(http/post ""
  {:headers {"ContentType" "application/json"}
   :body {"id" 1234567 "data" {"a" "a"}}})
Take this, what I have to work with is :body, so if I want to send a raw json body, that's fine, I just call cheshire.core/generate-string on the value at :body, but to send form encoded? That's a bunch more complex. So normally this falls under stuff I expect the http client lib to handle, but in this case it doesn't, so I'm just wondering is there a more fully featured client, for example clj-http https://github.com/dakrone/clj-http

borkdude16:06:20

@U0JUM502E Ah right. I think for this to work you just have to do something like this:

(require '[clojure.string :as str])
(defn encode [v] (java.net.URLEncoder/encode v))
(defn encode-map [m] (str/join "&" (reduce-kv (fn [acc k v] (conj acc (str k "=" (encode v)))) [] m)) )
(encode-map {"a" "1" "b" "foobar$123"}) ;;=> "a=1&b=foobar%24123"

borkdude16:06:43

But I think it's reasonable if java-http-clj would do this for you with the given content type. What do you think @U3L6TFEJF?

folcon16:06:33

Hmm, ok, that should work I think but there's the server isn't accepting it, so there's some gap. I'm going to try and work out what...

borkdude16:06:10

Cool. Maybe you can also use hato which is closest to clj-http

borkdude16:06:44

I'll be back in an hour after dinner. For hato you need to add babashka spec alpha to your bb edn I think too

folcon17:06:58

Ok, this seems to work now

folcon17:06:13

I didn't realise the form encoding step could be done that easily, I remember having to setup some object and do some setting etc when I was in jsland. Giving hato a look

folcon17:06:06

Hmm, not sure how to use it, after I added it to bb.edn. When required it I got Could not find namespace: clojure.spec.alpha adding clojure.spec.alpha to bb.edn and deps.edn didn't seem to help. EDIT: Ahh, just spotted the message about a special version of spec alpha

borkdude18:06:19

@U0JUM502E Add this to your bb.edn:

org.babashka/spec.alpha {:git/url ""
                         :git/sha "1a841c4cc1d4f6dab7505a98ed2d532dd9d56b78"}
You can also try the built-in client org.httpkit.client:
(require '[org.httpkit.client :as client])
@(client/post "" {:form-params {"a" "foo bar"}})

folcon18:06:29

Oh, I didn't realise there was a builtin 😃

borkdude18:06:24

Yeah, it's https://github.com/http-kit/http-kit :) So we have three options in bb: • bb.curl • httpkit client • java.net.http

borkdude18:06:11

And even a fourth one: HttpUrlConnection and clj-http-lite which is based on that (external library)

😅 1
folcon18:06:47

That's a fair bit of choice

teodorlu14:06:00

Really liking Babashka's synergy with Bash and Unix. Long lines, best viewed with wide slack.

🎉 1
borkdude18:06:29

A PR to make bb compatible with #malli https://github.com/metosin/malli/pull/718 Instructions on how to try it now are in the PR!

🙌 4