Shaun,

Thanks a lot for your response.  

This is a much nicer version of what I came up with.  I have one question 
remaining: would you (map) over a seq of db-entities or would you use 
something else (doseq, for, etc).

I typically use map/filter/reduce 99% of the time and I'm just wander if 
(map api-call-pipeline! seq-of-db-entities) is idiomatic in this case.

Best,
Jarrod

On Monday, February 10, 2014 7:36:49 PM UTC-5, Shaun Parker wrote:
>
> Hey Jarrod,
>
> Whenever I find myself writing out steps, like you just did, I like to use 
> a method described by Stuart Sierra, 
> pipelines<http://stuartsierra.com/2012/05/16/syntactic-pipelines>. 
> In your case, I'd write something like this:
>
> (defn make-api-call!
>   [{:keys [db-entity] :as state}]
>   (let [result (client/post "https://cool-api.com/entity";
>                             {:form-params {:x 42 :y (:id db-entity) ...}})
>         status (:status result)]
>     (assoc state :api-result result
>                  :api-call-successful? (<= 200 status 299))))
>
> (defn write-response!
>   [{:keys [api-result] :as state}]
>   (let [file ...]
>     (spit file (:body api-result))
>     (assoc state :api-result-file file)))
>
> (defn update-db-entity!
>   [{:keys [db-entity] :as state}]
>   (let [updated-db-entity (...)]
>     (assoc state :db-entity updated-db-entity)))
>
> (defn process-api-result!
>   [{:keys [api-call-successful?] :as state}]
>   (-> state
>       (update-db-entity!)
>       (cond->
>         api-call-successful? (write-response!))))
>
> (defn api-call-pipeline!
>   [db-entity]
>   (-> {:db-entity db-entity}
>       (make-api-call!)
>       (process-api-result!)
>       ;; do whatever you want with the state, for now return the entity 
> and the result file
>       (select-keys [:db-entity :api-result-file])))
>
>
> In this example you'd just call api-call-pipeline! with the database 
> entity. This lets you test each of these steps in isolation. Hope that 
> helps!
>
> Shaun
>
> On Monday, February 10, 2014 3:07:22 PM UTC-7, Jarrod Swart wrote:
>>
>> I'm having a hard time coming up with a satisfactory way to implement the 
>> following:
>>
>>
>>    1. Call an API with a map from the DB containing various endpoint 
>>    data like URL, etc.
>>    2. If the response has an HTTP 200, update the map-from-db and then 
>>    write response body to a file.
>>    3. If step 2 yields a HTTP > 2xx, update the map-from-db but do not 
>>    write file
>>
>> Because this is mostly imperative it feels "icky" in Clojure.  I would 
>> just call the api query function mapped across them all, but I am making 
>> many API requests and writing many files.  It seemed better to attempt them 
>> one at a time.  Is this wrong-headed?
>>
>> Suggestions?
>>
>>

-- 
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.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to