summaryrefslogtreecommitdiff
path: root/src/puppetlabs/ring_middleware/utils.clj
blob: 93937cad3ee79bbcf90278a4fb44afbc7eb499cc (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
(ns puppetlabs.ring-middleware.utils
  (:require [schema.core :as schema]
            [ring.util.response :as rr]
            [slingshot.slingshot :as sling]
            [cheshire.core :as json])
  (:import (java.security.cert X509Certificate)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Schemas

(def ResponseType
  (schema/enum :json :plain))

(def RingRequest
  {:uri schema/Str
   (schema/optional-key :ssl-client-cert) (schema/maybe X509Certificate)
   schema/Keyword schema/Any})

(def RingResponse
  {:status schema/Int
   :headers {schema/Str schema/Any}
   :body schema/Any
   schema/Keyword schema/Any})


;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Helpers

(schema/defn ^:always-validate json-response
  :- RingResponse
  [status :- schema/Int
   body :- schema/Any]
  (-> body
      json/encode
      rr/response
      (rr/status status)
      (rr/content-type "application/json; charset=utf-8")))

(schema/defn ^:always-validate plain-response
  :- RingResponse
  [status :- schema/Int
   body :- schema/Str]
  (-> body
      rr/response
      (rr/status status)
      (rr/content-type "text/plain; charset=utf-8")))

(defn throw-bad-request!
  "Throw a :bad-request type slingshot error with the supplied message"
  [message]
  (sling/throw+  {:kind :bad-request
                  :msg message}))

(defn bad-request?
  [e]
  "Determine if the supplied slingshot error is for a bad request"
  (when (map? e)
    (= (:kind e)
       :bad-request)))

(defn throw-service-unavailable!
  "Throw a :service-unavailable type slingshot error with the supplied message"
  [message]
  (sling/throw+  {:kind :service-unavailable
                  :msg message}))

(defn service-unavailable?
  [e]
  "Determine if the supplied slingshot error is for an unavailable service"
  (when  (map? e)
    (= (:kind e)
       :service-unavailable)))

(defn throw-data-invalid!
  "Throw a :data-invalid type slingshot error with the supplied message"
  [message]
  (sling/throw+  {:kind :data-invalid
                  :msg message}))

(defn data-invalid?
  [e]
  "Determine if the supplied slingshot error is for invalid data"
  (when  (map? e)
    (= (:kind e)
       :data-invalid)))

(defn schema-error?
  [e]
  "Determine if the supplied slingshot error is for a schema mismatch"
  (when (map? e)
    (= (:type e)
       :schema.core/error)))