Skip to main content
Notice removed Authoritative reference needed by CommunityBot
Bounty Ended with Martin Půda's answer chosen by CommunityBot
Notice added Authoritative reference needed by Pablo
Bounty Started worth 50 reputation by Pablo
fix typo in body
Source Link
Pablo
  • 95
  • 1
  • 10

I'd like some help to improve this cli program. It queries a city's weather and pushes a weather report.

IIt takes two arguments: the city to query and the apikey for OpenWeatherMap. For example lein run glasgow abcde12345.

How to make it more idiomatic clojure? How can I avoid having to pass the apikey multiple times?

(ns sunshine.core
  (:gen-class)
  (:require [clj-http.client :as http])
  (:require [clojure.data.json :as json])
)


(defn _get
  [url]
  (http/get url)
)


(defn _post
  [url payload]
  (http/post url payload)
)


(defn as_json
  [text]
  (json/read-str text :key-fn keyword)
)


(defn query_weather
  [apikey coords]
  (as_json
   (:body (_get (str "https://api.openweathermap.org/data/2.5/weather?lat=" (:lat coords) "&lon=" (:lon coords) "&appid=" apikey "&units=metric")))
   )
)


(defn get_city_coords
  [apikey city]
  (let [{:keys [lon lat]} (first (as_json
   (:body (_get (str "http://api.openweathermap.org/geo/1.0/direct?q=" city "&appid=" apikey)))
   ))]
    {:lon lon :lat lat})
  )


(defn unpack
  [raw_message]
  {:desc (:description (first (:weather raw_message))) :temperature (:temp (:main raw_message))})


 (defn get_weather
   [apikey coords]
   (unpack (query_weather apikey coords) )
 )


(defn as_weather_report
  [city weather]
  (str "Current weather in " city ": " (:temperature weather) "ºC with " (:desc weather))
)


(defn notify
  [message]
  (if (:deleted (as_json (:body (_post, "https://api.keen.io/dev/null" {:message message}))))
    (println message)
  )
)


(defn -main
  "Query weather and notify"
  [& args]
  (let [city (first args) apikey (second args)]
    (notify (as_weather_report city (get_weather apikey (get_city_coords apikey city))))
    )
)

```

I'd like some help to improve this cli program. It queries a city's weather and pushes a weather report.

I takes two arguments: the city to query and the apikey for OpenWeatherMap. For example lein run glasgow abcde12345.

How to make it more idiomatic clojure? How can I avoid having to pass the apikey multiple times?

(ns sunshine.core
  (:gen-class)
  (:require [clj-http.client :as http])
  (:require [clojure.data.json :as json])
)


(defn _get
  [url]
  (http/get url)
)


(defn _post
  [url payload]
  (http/post url payload)
)


(defn as_json
  [text]
  (json/read-str text :key-fn keyword)
)


(defn query_weather
  [apikey coords]
  (as_json
   (:body (_get (str "https://api.openweathermap.org/data/2.5/weather?lat=" (:lat coords) "&lon=" (:lon coords) "&appid=" apikey "&units=metric")))
   )
)


(defn get_city_coords
  [apikey city]
  (let [{:keys [lon lat]} (first (as_json
   (:body (_get (str "http://api.openweathermap.org/geo/1.0/direct?q=" city "&appid=" apikey)))
   ))]
    {:lon lon :lat lat})
  )


(defn unpack
  [raw_message]
  {:desc (:description (first (:weather raw_message))) :temperature (:temp (:main raw_message))})


 (defn get_weather
   [apikey coords]
   (unpack (query_weather apikey coords) )
 )


(defn as_weather_report
  [city weather]
  (str "Current weather in " city ": " (:temperature weather) "ºC with " (:desc weather))
)


(defn notify
  [message]
  (if (:deleted (as_json (:body (_post, "https://api.keen.io/dev/null" {:message message}))))
    (println message)
  )
)


(defn -main
  "Query weather and notify"
  [& args]
  (let [city (first args) apikey (second args)]
    (notify (as_weather_report city (get_weather apikey (get_city_coords apikey city))))
    )
)

```

I'd like some help to improve this cli program. It queries a city's weather and pushes a weather report.

It takes two arguments: the city to query and the apikey for OpenWeatherMap. For example lein run glasgow abcde12345.

How to make it more idiomatic clojure? How can I avoid having to pass the apikey multiple times?

(ns sunshine.core
  (:gen-class)
  (:require [clj-http.client :as http])
  (:require [clojure.data.json :as json])
)


(defn _get
  [url]
  (http/get url)
)


(defn _post
  [url payload]
  (http/post url payload)
)


(defn as_json
  [text]
  (json/read-str text :key-fn keyword)
)


(defn query_weather
  [apikey coords]
  (as_json
   (:body (_get (str "https://api.openweathermap.org/data/2.5/weather?lat=" (:lat coords) "&lon=" (:lon coords) "&appid=" apikey "&units=metric")))
   )
)


(defn get_city_coords
  [apikey city]
  (let [{:keys [lon lat]} (first (as_json
   (:body (_get (str "http://api.openweathermap.org/geo/1.0/direct?q=" city "&appid=" apikey)))
   ))]
    {:lon lon :lat lat})
  )


(defn unpack
  [raw_message]
  {:desc (:description (first (:weather raw_message))) :temperature (:temp (:main raw_message))})


 (defn get_weather
   [apikey coords]
   (unpack (query_weather apikey coords) )
 )


(defn as_weather_report
  [city weather]
  (str "Current weather in " city ": " (:temperature weather) "ºC with " (:desc weather))
)


(defn notify
  [message]
  (if (:deleted (as_json (:body (_post, "https://api.keen.io/dev/null" {:message message}))))
    (println message)
  )
)


(defn -main
  "Query weather and notify"
  [& args]
  (let [city (first args) apikey (second args)]
    (notify (as_weather_report city (get_weather apikey (get_city_coords apikey city))))
    )
)

```
fix typo in title
Link
Pablo
  • 95
  • 1
  • 10

Get city weather and notify an enpointendpoint

Source Link
Pablo
  • 95
  • 1
  • 10

Get city weather and notify an enpoint

I'd like some help to improve this cli program. It queries a city's weather and pushes a weather report.

I takes two arguments: the city to query and the apikey for OpenWeatherMap. For example lein run glasgow abcde12345.

How to make it more idiomatic clojure? How can I avoid having to pass the apikey multiple times?

(ns sunshine.core
  (:gen-class)
  (:require [clj-http.client :as http])
  (:require [clojure.data.json :as json])
)


(defn _get
  [url]
  (http/get url)
)


(defn _post
  [url payload]
  (http/post url payload)
)


(defn as_json
  [text]
  (json/read-str text :key-fn keyword)
)


(defn query_weather
  [apikey coords]
  (as_json
   (:body (_get (str "https://api.openweathermap.org/data/2.5/weather?lat=" (:lat coords) "&lon=" (:lon coords) "&appid=" apikey "&units=metric")))
   )
)


(defn get_city_coords
  [apikey city]
  (let [{:keys [lon lat]} (first (as_json
   (:body (_get (str "http://api.openweathermap.org/geo/1.0/direct?q=" city "&appid=" apikey)))
   ))]
    {:lon lon :lat lat})
  )


(defn unpack
  [raw_message]
  {:desc (:description (first (:weather raw_message))) :temperature (:temp (:main raw_message))})


 (defn get_weather
   [apikey coords]
   (unpack (query_weather apikey coords) )
 )


(defn as_weather_report
  [city weather]
  (str "Current weather in " city ": " (:temperature weather) "ºC with " (:desc weather))
)


(defn notify
  [message]
  (if (:deleted (as_json (:body (_post, "https://api.keen.io/dev/null" {:message message}))))
    (println message)
  )
)


(defn -main
  "Query weather and notify"
  [& args]
  (let [city (first args) apikey (second args)]
    (notify (as_weather_report city (get_weather apikey (get_city_coords apikey city))))
    )
)

```