This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2018-03-27
Channels
- # aws (8)
- # beginners (172)
- # boot-dev (4)
- # cider (16)
- # cljs-dev (123)
- # cljsjs (4)
- # clojure (90)
- # clojure-brasil (3)
- # clojure-dev (7)
- # clojure-dusseldorf (1)
- # clojure-finland (1)
- # clojure-italy (59)
- # clojure-russia (3)
- # clojure-seattle (2)
- # clojure-seattle-old (1)
- # clojure-spec (40)
- # clojure-uk (28)
- # clojurescript (327)
- # clojurewerkz (3)
- # code-reviews (8)
- # cursive (4)
- # datomic (24)
- # editors (1)
- # emacs (19)
- # fulcro (147)
- # funcool (1)
- # graphql (1)
- # hoplon (34)
- # jobs-rus (1)
- # lein-figwheel (5)
- # leiningen (20)
- # luminus (14)
- # midje (1)
- # off-topic (8)
- # onyx (7)
- # parinfer (47)
- # pedestal (1)
- # perun (1)
- # portkey (46)
- # re-frame (25)
- # reagent (9)
- # remote-jobs (4)
- # ring-swagger (5)
- # rum (1)
- # shadow-cljs (113)
- # slack-help (8)
- # spacemacs (7)
- # sql (9)
- # tools-deps (23)
- # uncomplicate (3)
- # unrepl (3)
- # yada (6)
Hi all, I have a code review request, if anyone has a min. I’m writing some code to interface with APIs, requesting creation of reports, waiting for creation, downloading or terminating.
(defn download-stats
"Requests creation of `report-type` for `date`. Once the report job is
BUILT, returns the data as a java.io.BufferedReader. The report is in tsv
format.
Cleans up after itself by deleting requested job."
[customer-id report-type date]
(let [c (creds customer-id)
id (-> (reports.stat/create! c {:reportTp report-type :statDt date})
:body :reportJobId)]
(loop [job (reports.stat/fetch-job c id)]
(Thread/sleep 50)
(if-not (#{"RUNNING" "REGIST"} (:status job))
(if (= "BUILT" (:status job))
(let [buf-r (reports.common/download c job)]
(reports.stat/delete! c id)
buf-r)
(do
(reports.stat/delete! c id)
(throw
(Exception.
(str "Requested job returned status of: " (:status job))))))
(recur (reports.stat/fetch-job c id))))))
@sooheon To avoid the duplicated call to delete!
I'd probably wrap the loop
in try
/ finally
(containing the delete!
call) to make it clearer this is a post-operation cleanup. That would allow the inner let
to be removed. I'd use a case
to avoid the nested if
.
(defn download-stats
"Requests creation of `report-type` for `date`. Once the report job is
BUILT, returns the data as a java.io.BufferedReader. The report is in tsv
format.
Cleans up after itself by deleting requested job."
[customer-id report-type date]
(let [c (creds customer-id)
id (-> (reports.stat/create! c {:reportTp report-type :statDt date})
:body :reportJobId)
fetch (fn []
(Thread/sleep 50)
(reports.stat/fetch-job c id))]
(try
(loop [job (fetch)]
(case (:status job)
("RUNNING" "REGIST")
(recur (fetch))
"BUILT"
(reports.common/download c job)
(throw
(Exception.
(str "Requested job returned status of: " (:status job))))))
(finally
(reports.stat/delete! c id)))))
(edited to use a helper that sleeps then fetches the job)Also, since you're fetching the job then sleeping, I'd probably add a helper function that accepted c
and id
and slept first, then fetched the job -- I'll edit the above code.
Great, try/catch/throw/finally are constructs I need to get more familiar with, this was a really helpful example. Thanks @seancorfield