Skip to content

Instantly share code, notes, and snippets.

@randylien
Forked from philippkueng/core.clj
Created June 26, 2019 07:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save randylien/7019b1952f15d5c3245341f9e332672c to your computer and use it in GitHub Desktop.
Save randylien/7019b1952f15d5c3245341f9e332672c to your computer and use it in GitHub Desktop.
Fetch & write a binary file using Clojure and clj-http
(ns the-namespace.core
(:require [clj-http.client :as client]
[clojure.java.io :as io]))
(defn- fetch-photo!
"makes an HTTP request and fetches the binary object"
[url]
(let [req (client/get url {:as :byte-array :throw-exceptions false})]
(if (= (:status req) 200)
(:body req))))
(defn- save-photo!
"downloads and stores the photo on disk"
[photo]
(let [p (fetch-photo! (:url photo))]
(if (not (nil? p))
(with-open [w (io/output-stream (str "photos/" (:id photo) ".jpg"))]
(.write w p)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment