Fork me on GitHub
#babashka
<
2023-09-01
>
mmer13:09:41

Hi, having just watch the "talking to martians" talk, I was wondering which of the list of libraries of martian you would use with BB?

borkdude13:09:43

you can use martian + babashka.http-client

mmer13:09:54

i.e which of these should I use:

borkdude13:09:37

if it's not listed in that list, provide a PR to martian

borkdude13:09:39

@U4C3ZU6KX I tested this and it works:

(babashka.deps/add-deps '{:deps {com.github.oliyh/martian-babashka-http-client {:mvn/version "0.1.25"}}})

mmer14:09:41

I have sort of got it working, but I have hit the normal issues of :insecure (I have tried to add a client into the ctx but not sure if that is the correct way to handle it. Also I am getting errors, from the library and I was wondering where I could ask questions. Can't find a #martian channel.

borkdude14:09:28

The author is @U076R6N1L - perhaps we can ask him to create a martian channel, but feel free to continue here as well. As for creating a separate client, makes sense. Let's have a look at the source...

borkdude15:09:13

I think you would need to pass ctx as well and add :client from ctx to the request map

borkdude15:09:00

@U4C3ZU6KX you could experiment locally with this and use the martian "plugin" as a :local/root project in in bb.edn, let me know if you need any help

oliy15:09:11

Hi, I can have a look at this a bit later on. There is no #martian channel, happy to chat on this thread

oliy15:09:38

Have you got it working using the BB http client directly?

borkdude15:09:41

@U076R6N1L one issue is that the README of the martian project does not list:

com.github.oliyh/martian-babashka-http-client 
as a clojars dependency

borkdude15:09:00

the other issue, being able to pass a custom client, is another one. not sure if you've done something similar for the other clients?

oliy18:09:46

Yep I will add the babashka sub library to the readme!

oliy18:09:19

What do we mean by custom client here? Is that a babashka-http thing?

borkdude18:09:40

yes, but also a concept that is known to other http libraries like http-kit and clj-http

oliy18:09:03

Ok like the connection manager thing

oliy18:09:46

I have a DM with @U4C3ZU6KX but will share the answer back here once we've got it sorted

borkdude18:09:07

with the clj-http you can pass "load-opts" which get later merged when doing a request. https://github.com/oliyh/martian/blob/2540b6273b32a099ce9632a68115c6d83da83890/clj-http/src/martian/clj_http.clj#L22-L31 This would also be a good solution for the bb client lib

borkdude18:09:21

Then you can just pass :client ... as part of the load-opts

borkdude18:09:14

actually I think that should already work as of now

borkdude18:09:18

without any changes

oliy18:09:39

The load-opts are only for loading the swagger json

oliy18:09:09

The entire request map in the context is passed to the http library, so you can write an interceptor to put anything you want in there

borkdude18:09:27

cool, the only thing is when bootstrapping also a request is made

borkdude18:09:05

so you should still pass the client through the load opts as well

oliy18:09:09

Yeah, that's why load opts is special, because that happens before you have an instance, therefore no interceptors yet

borkdude18:09:27

the load-opts are merged with the request so this should work

oliy18:09:37

I need to check if all the other ones work the same way, they should. And yes, that's how you do it for loading the swagger json. After that, you can write an interceptor like this

(fn [ctx]
  (assoc-in ctx [:request :client] your-client-thing))

mmer18:09:04

Thanks guys.

borkdude18:09:51

Perhaps adding some docs about this would be good, if you can verify that it works for you

mmer18:09:25

I still get

; clojure.lang.ExceptionInfo: unable to find valid certification path to requested target mexp.core tripod/context.cljc:207:7

borkdude18:09:54

what did you do exactly

oliy18:09:47

I would make sure you can get the request working in the http client directly first

👍 2
mmer18:09:56

The snippet from my code

(def client (http/client (assoc-in http/default-client-opts [:ssl-context :insecure] true)))

(def allow-insecure
  {:name ::allow-insecure
   :enter (fn [ctx]
            (assoc-in ctx [:request :client] client))})

(def add-authentication-header
  {:name ::add-authentication-header
   :enter (fn [ctx]
            (assoc-in ctx [:request :headers "Authorization"] "Token: <token str>" ))})

(def m (martian/bootstrap-openapi (str "https://" (:cpd-route (u/get_config (u/get-user))) "/api/v2/catalog")
                                  (mf/load-local-resource "resources/descriptors.yaml")
                                  {:interceptors (concat martian/default-interceptors
                                                         [add-authentication-header
                                                          allow-insecure
                                                          martian-http/perform-request])}))

(martian/response-for m :get-descriptors)

mmer18:09:34

output:

No matching content-type available {:supported-content-types #{}, :available-content-types (application/json), :header Content-Type}
No matching content-type available {:supported-content-types #{}, :available-content-types (application/json), :header Content-Type}
; clojure.lang.ExceptionInfo: Exceptional status code: 404 mexp.core tripod/context.cljc:207:7

borkdude18:09:42

you need to add :client client to the load-opts for the bootstrap thing as well

mmer18:09:02

Isn't that what I have done in the allow-insecure?

borkdude18:09:37

add {:interceptors ... :client client}

mmer18:09:35

Ok thanks

borkdude18:09:48

did that work?

mmer19:09:23

When adding it through the interceptor I no longer get the message about the certificates, but I get a 404 which means I got through. so the following seemed to work ok

{:name ::allow-insecure
   :enter (fn [ctx]
            (assoc-in ctx [:request :client] client))})
Putting the client on the end of the load-opts did not work I got the error about the certs. Thank you for your persistence. very much appreciated. Have a good rest of the evening.

borkdude19:09:04

The http-kit client should also work with bb btw and clj-http-lite as well, so you have more options.

borkdude19:09:03

as @U076R6N1L suggested, just trying to load the swagger docs using a bare minimal example using only the client might be good to try

mmer19:09:55

Ok thanks

mmer19:09:35

That @U04V15CAJ, I am afraid I could not get the Authentication to work with the martian-babashka-http library so I swap to clj-http-lite and it works. The insecure set up with the client worked, so that was good. Just struggle to get oauth token injected. I wish I understood more to take load off you. Hope you have a good weekend.

borkdude19:09:17

no worries, and thanks!

oliy14:09:43

i updated the martian readme to hopefully make the http-client library choice easier to understand: https://github.com/oliyh/martian/blob/master/README.md

👍 2