I know that lost feeling. I came from a background of OO too. Most of the 
languages I'd used (C++, Java, Python, Go) had either total OO orientation, 
or at least pushed you into an OO way of thinking. I found that reading 
some Clojure books and other's code helped a lot (Joy of Clojure, Eric 
Normand's content etc). Once I got my head around some practical examples 
like the one you state, I found that my thinking changed. I also found the 
MIT SCIP fried the way I think and spat it out anew. It takes time and 
practice (well at least it took me time and practice).

I think that what you are doing is similar to something I did to get the 
Google Identity Keys. 

(defmethod ig/init-key ::get-google-keys [_ _]
  (let [gkeys (ref {:keys nil :expires 0})]
    (fn []
      (let [current @gkeys]
        (:keys (if (>= (c/to-epoch (t/now)) (:expires current))
                 (do
                   (log/info "Fetching Google Keys, expiry: " (:expires 
current))
                   (dosync
                    (ref-set gkeys (fetch-google-keys ::jwk)))) ;; only use 
JWK for now
                 current))))))

I was using the excellent Integrant library, but it would work the same 
without it. I guess that you could do something similar with your token, if 
it had expired, then it would fetch it?

The main thing that I changed in my mind is that it is ok to pass the state 
to a function and get back a response, hiding everything in private data 
doesn't give you as much as OO promises it will.



On Tuesday, 12 May 2020 09:38:08 UTC+1, Orestis Markou wrote:
>
> 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
>  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 <ultim...@gmail.com <javascript:>> 
> 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 clo...@googlegroups.com <javascript:>
> Note that posts from new members are moderated - please be patient with 
> your first post.
> To unsubscribe from this group, send email to
> clo...@googlegroups.com <javascript:>
> 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 clo...@googlegroups.com <javascript:>.
> 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/db04e1ee-1333-4dab-8f7b-23f67d15e898%40googlegroups.com.

Reply via email to