Skip to content

Commit dc07443

Browse files
committed
Merge pull request #18 from vise890/remove-http-calls-at-complile
[RFC] Remove HTTP calls at complile time
2 parents 57c89ac + 4e75071 commit dc07443

File tree

2 files changed

+8
-10
lines changed

2 files changed

+8
-10
lines changed

src/clj/cloujera/burglar/core.clj

-3
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@
1111
(def password "letswinthisthing")
1212
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
1313

14-
;; FIXME: this makes an HTTP call at *compile* time
15-
;; that means that if the session expires, you need to recompile
16-
;; compilation/deployment can also fail if the network is flaky
1714
(def get-coursera-page
1815
(cache/persist (scraper/get-protected-page username password)))
1916

src/clj/cloujera/burglar/scraper.clj

+8-7
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
(:require [clj-http.client :as http]
33
[clj-http.cookies :as cookies]))
44

5+
56
;; inspiration from: https://github.com/coursera-dl/coursera/blob/master/coursera%2Fcookies.py
67
(defn- authenticated-cookie-store
78
"returns a cookie store that lets you get protected pages"
@@ -28,12 +29,12 @@
2829
:cookie-store cs})
2930
cs)))
3031

32+
(def authenticated-cs (atom nil))
3133
;; Username -> Password -> (ProtectedUrl -> Page)
3234
(defn get-protected-page [username password]
33-
;; FIXME: authenticated-cookie-store is not quite a referentially transparent
34-
;; function........
35-
;; serializing the cookie store + caching it with an expiration date would be
36-
;; a possible solution
37-
(let [authenticated-cs ((memoize authenticated-cookie-store) username password)]
38-
(fn [protected-url]
39-
(:body (http/get protected-url {:cookie-store authenticated-cs})))))
35+
(fn [protected-url]
36+
(let [cs (if (nil? @authenticated-cs)
37+
(reset! authenticated-cs
38+
(authenticated-cookie-store username password))
39+
@authenticated-cs)]
40+
(:body (http/get protected-url {:cookie-store cs})))))

0 commit comments

Comments
 (0)