I’m interested in this too — you can get inspiration from https://github.com/cognitect-labs/aws-api/blob/master/src/cognitect/aws/credentials.clj <https://github.com/cognitect-labs/aws-api/blob/master/src/cognitect/aws/credentials.clj> which does something similar for expired credentials. Seems like a mutable internal field is fine for this use case.
> On 12 May 2020, at 9:27 AM, Scaramaccai <ultimoam...@gmail.com> wrote: > > Hi everyone, > > I wanted to give a try to Clojure and functional programming in general but I > can't really stop thinking "object oriented" or "with state everywhere". > After 20+ years with objects + state I guess I'm lost without them :) > > The first thing I want to try is to get some data from an API that needs > OAuth authentication. I would like to hide the fact that there's an OAuth > token to be sent. In pseudo-Java I would do something like: > > class MyAuthHttpClient { > private token; > public MyAuthHttpClient(String usr, String psw) {...} > > public ... getResponse(Url) { > // here check if token is available and if it is expiring; > // if expiring -> fetch a new token before call the http service > // caller doesn't even know there's a token involved in the process > } > } > > What's the proper way to do that in Clojure? > I guess one way would be to have a function that returns a new token given a > (possibly old) token and user+psw > > (defn gettkn [usr, psw, tkn] (return a new token if tkn is expiring or tkn if > not expiring)) > > (def wrap-gettkn (partial gettkn "myuser" "mypass")) > > (defn geturl [url, tkn] client/get url {:oauth-token (wrap-gettkn tkn)}) > > > I can "save" usr and psw, but I always have to "keep" the tkn around at every > level; > while I would like the token to be "hidden" to the "geturl" clients (just > like I did in the "private token" in the pseudo-Java). > > What's the proper way of doing something like this in Clojure? > > -- > You received this message because you are subscribed to the Google > Groups "Clojure" group. > To post to this group, send email to clojure@googlegroups.com > Note that posts from new members are moderated - please be patient with your > first post. > To unsubscribe from this group, send email to > clojure+unsubscr...@googlegroups.com > For more options, visit this group at > http://groups.google.com/group/clojure?hl=en > <http://groups.google.com/group/clojure?hl=en> > --- > You received this message because you are subscribed to the Google Groups > "Clojure" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to clojure+unsubscr...@googlegroups.com > <mailto:clojure+unsubscr...@googlegroups.com>. > To view this discussion on the web visit > https://groups.google.com/d/msgid/clojure/6aa66613-24d9-4ebc-87d4-e9a6cca05165%40googlegroups.com > > <https://groups.google.com/d/msgid/clojure/6aa66613-24d9-4ebc-87d4-e9a6cca05165%40googlegroups.com?utm_medium=email&utm_source=footer>. -- You received this message because you are subscribed to the Google Groups "Clojure" group. To post to this group, send email to clojure@googlegroups.com Note that posts from new members are moderated - please be patient with your first post. To unsubscribe from this group, send email to clojure+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/clojure?hl=en --- You received this message because you are subscribed to the Google Groups "Clojure" group. To unsubscribe from this group and stop receiving emails from it, send an email to clojure+unsubscr...@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/clojure/7C785215-2169-461D-917F-0605F30B0154%40orestis.gr.