Update http-swagger example

This commit is contained in:
Tommi Reiman 2018-10-21 23:11:09 +03:00
parent 8fb6582b67
commit 0beeaef1f8
3 changed files with 54 additions and 6 deletions

View file

@ -11,8 +11,24 @@
[reitit.http.interceptors.multipart :as multipart] [reitit.http.interceptors.multipart :as multipart]
[reitit.interceptor.sieppari :as sieppari] [reitit.interceptor.sieppari :as sieppari]
[ring.adapter.jetty :as jetty] [ring.adapter.jetty :as jetty]
[aleph.http :as client]
[muuntaja.core :as m] [muuntaja.core :as m]
[clojure.java.io :as io])) [clojure.java.io :as io]
[clojure.spec.alpha :as s]
[spec-tools.core :as st]
[manifold.deferred :as d]))
(s/def ::x int?)
(s/def ::y int?)
(s/def ::total pos-int?)
(s/def ::seed string?)
(s/def ::results
(st/spec
{:spec (s/and int? #(< 0 % 100))
:description "between 1-100"
:swagger/default 10
:reason "invalid number"}))
(def app (def app
(http/ring-handler (http/ring-handler
@ -44,22 +60,53 @@
:body (io/input-stream :body (io/input-stream
(io/resource "reitit.png"))})}}]] (io/resource "reitit.png"))})}}]]
["/async"
{:get {:swagger {:tags ["async"]}
:summary "fetches random users asynchronously over the internet"
:parameters {:query (s/keys :req-un [::results] :opt-un [::seed])}
:responses {200 {:body any?}}
:handler (fn [{{{:keys [seed results]} :query} :parameters}]
(d/chain
(client/get
"https://randomuser.me/api/"
{:query-params {:seed seed, :results results}})
:body
(partial m/decode m/instance "application/json")
:results
(fn [results]
{:status 200
:body results})))}}]
["/math" ["/math"
{:swagger {:tags ["math"]}} {:swagger {:tags ["math"]}}
["/plus" ["/plus"
{:get {:summary "plus with spec query parameters" {:get {:summary "plus with data-spec query parameters"
:parameters {:query {:x int?, :y int?}} :parameters {:query {:x int?, :y int?}}
:responses {200 {:body {:total int?}}} :responses {200 {:body {:total pos-int?}}}
:handler (fn [{{{:keys [x y]} :query} :parameters}] :handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200 {:status 200
:body {:total (+ x y)}})} :body {:total (+ x y)}})}
:post {:summary "plus with spec body parameters" :post {:summary "plus with data-spec body parameters"
:parameters {:body {:x int?, :y int?}} :parameters {:body {:x int?, :y int?}}
:responses {200 {:body {:total int?}}} :responses {200 {:body {:total int?}}}
:handler (fn [{{{:keys [x y]} :body} :parameters}] :handler (fn [{{{:keys [x y]} :body} :parameters}]
{:status 200 {:status 200
:body {:total (+ x y)}})}}]]] :body {:total (+ x y)}})}}]
["/minus"
{:get {:summary "minus with clojure.spec query parameters"
:parameters {:query (s/keys :req-un [::x ::y])}
:responses {200 {:body (s/keys :req-un [::total])}}
:handler (fn [{{{:keys [x y]} :query} :parameters}]
{:status 200
:body {:total (- x y)}})}
:post {:summary "minus with clojure.spec body parameters"
:parameters {:body (s/keys :req-un [::x ::y])}
:responses {200 {:body (s/keys :req-un [::total])}}
:handler (fn [{{{:keys [x y]} :body} :parameters}]
{:status 200
:body {:total (- x y)}})}}]]]
{:data {:coercion spec-coercion/coercion {:data {:coercion spec-coercion/coercion
:muuntaja m/instance :muuntaja m/instance
@ -87,7 +134,7 @@
{:executor sieppari/executor})) {:executor sieppari/executor}))
(defn start [] (defn start []
(jetty/run-jetty #'app {:port 3000, :join? false}) (jetty/run-jetty #'app {:port 3000, :join? false, :async true})
(println "server running in port 3000")) (println "server running in port 3000"))
(comment (comment

Binary file not shown.

Before

Width:  |  Height:  |  Size: 228 KiB

After

Width:  |  Height:  |  Size: 253 KiB

View file

@ -95,6 +95,7 @@
[funcool/promesa "1.9.0"] [funcool/promesa "1.9.0"]
[metosin/sieppari "0.0.0-alpha5"] [metosin/sieppari "0.0.0-alpha5"]
[yada "1.2.15"] [yada "1.2.15"]
[aleph "0.4.6"]
[ring/ring-defaults "0.3.2"] [ring/ring-defaults "0.3.2"]
[ataraxy "0.4.2"] [ataraxy "0.4.2"]
[bidi "2.1.4"]]} [bidi "2.1.4"]]}