Functional way to make multiple API requests with Clojure -


i'm working on clojure application interact web api return random result meeting specific criterion. because of limitations of api, have retrieve number of results , use clojure filter out appropriate one.

i 99% or more of time, @ least 1 of results first request meet criterion. because of other 1%, feel have build in functionality make multiple requests. odds pretty second request successful i'm leery writing function recurs endlessly until gets right result in case goes wrong , end ddosing api.

my first thought build counter function , limit to, say, 5 requests. feels bit procedural approach. what's idiomatic clojure way go this?

here's have far:

(ns randchar.api   (:require [clj-http.client :as client]))  (defn api-request    [url]   (get-in     (client/request       {:url url        :method :get        :content-type :json        :as :json}) [:body :results]))  (defn filter-results   "returns first result meets criterion."   [results criterion]   (take 1          (filter #(= (get-in % [:nested :sub-nested]) criterion) results)))  (defn build-url   "returns url randomized query string api request; not shown."   []   )  (defn return-result   "currently recurs endlessly if no results meet criterion."   [criterion]   (let [x (filter-results (api-request (build-url)) criterion)]     (if (not (empty? x))       x       (recur)))) 

you can try like:

(defn return-result   [criterion count]   (->> (repeatedly count #(filter-results (api-request build-url) criterion))        (filter (complement empty?))        first)) 

Comments

Popular posts from this blog

html - How to style widget with post count different than without post count -

How to remove text and logo OR add Overflow on Android ActionBar using AppCompat on API 8? -

javascript - storing input from prompt in array and displaying the array -