All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.reborg.applenews_api.bytes.clj Maven / Gradle / Ivy

Go to download

Java/Clojure client for the Apple News REST api (https://developer.apple.com/go/?id=news-api-ref)

There is a newer version: 1.0.9
Show newest version
(ns net.reborg.applenews-api.bytes
  (:require [clojure.java.io :refer [copy]])
  (:import [java.io ByteArrayOutputStream ByteArrayInputStream FileInputStream]
           [java.net URL URLConnection]))

(defn to-bytes [^String s]
  (.getBytes s "UTF-8"))

(defn with-url
  "Execute f in the context of an open input stream for the url.
  f must be a function of an inputstream and an outputstream.
  It is expected that f process the inputstream from the URL in
  some meaningful way and dump the results into the provided
  outputstream."
  [url f]
  (with-open [out (java.io.ByteArrayOutputStream.)]
    (let [^URLConnection conn (doto
                                (.openConnection (URL. url))
                                (.setConnectTimeout 30000)
                                (.setReadTimeout 90000))]
      (f (.getInputStream conn) out)
      (.toByteArray out))))

(defn url-to-bytearray
  "Slurp the bytes from the url into a ByteArrayOuputStream and returns it."
  [url]
  (with-url url copy))




© 2015 - 2025 Weber Informatics LLC | Privacy Policy