Skip to content

Commit

Permalink
Add field in http-messages with message as json (#340)
Browse files Browse the repository at this point in the history
* Add field in http-messages with message as json

* Review feedback

* More review feedback
  • Loading branch information
mdemare authored Nov 15, 2024
1 parent a12fc41 commit 697b0ad
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 18 deletions.
7 changes: 5 additions & 2 deletions API.md
Original file line number Diff line number Diff line change
Expand Up @@ -271,7 +271,9 @@ Als de server gestart is met `STORE_HTTP_REQUESTS` variabel gezet op
OOAPI en RIO endpoint gelogd te krijgen. Om deze gegevens te krijgen
moet de query parameter `http-messages=true` doorgegeven worden bij
het aanmaken van een job en wordt het status object uitgebreid met
`http-messages`.
`http-messages`. De http-messages array bevat maps met "req" en "res"
keys. Wanneer het een json response betreft, dan bevat "res" naast "body"
(de string-representatie van de json) ook een "json-body" met de json zelf.

```
POST /job/dry-run/upsert/courses/ffeeddcc-bbaa-0099-8877-665544332211?http-messages=true
Expand All @@ -292,7 +294,8 @@ POST /job/dry-run/upsert/courses/ffeeddcc-bbaa-0099-8877-665544332211?http-messa
},
"res": {
"status": 404,
"body": null
"body": "{\"course\":{...",
"json-body": {"course":{}}
}
}, {
"req": "...",
Expand Down
45 changes: 31 additions & 14 deletions src/nl/surf/eduhub_rio_mapper/endpoints/api.clj
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
;; <https://www.gnu.org/licenses/>.

(ns nl.surf.eduhub-rio-mapper.endpoints.api
(:require [clojure.spec.alpha :as s]
(:require [clojure.data.json :as json]
[clojure.spec.alpha :as s]
[clojure.string :as str]
[compojure.core :refer [GET POST]]
[compojure.route :as route]
Expand Down Expand Up @@ -95,22 +96,38 @@
(assoc :status http-status/ok
:body (metrics/prometheus-render-metrics (count-queues-fn) (fetch-jobs-by-status) schac-home-to-name))))))

(defn json-request-headers? [headers]
(let [accept (get headers "Accept")]
(and accept
(str/starts-with? accept "application/json"))))

;; For json requests (requests with a json Accept header) add a :json-body key to the response with the
;; same content as the response body, only with the json parsed, instead of as a raw string.
(defn add-single-parsed-json-response [{{headers :headers :as req} :req, {status :status, body :body} :res}]
{:pre [req status body]}
{:req req
:res (cond-> {:status status :body body}
(json-request-headers? headers)
(assoc :json-body (json/read-str body :key-fn keyword)))})

(defn wrap-status-getter
[app {:keys [status-getter-fn]}]
(fn status-getter [req]
(let [res (app req)]
(if-let [token (:token res)]
(with-mdc {:token token}
(if-let [status (status-getter-fn token)]
(assoc res
:status http-status/ok
:body (if (= "false" (get-in req [:params :http-messages] "false"))
(dissoc status :http-messages)
status))
(assoc res
:status http-status/not-found
:body {:status :unknown})))
res))))
(let [res (app req)
token (:token res)
show-http-messages? (= "true" (get-in req [:params :http-messages] "false"))]
(if (nil? token)
res
(let [job-status (status-getter-fn token)]
(if (nil? job-status)
{:status http-status/not-found
:token token
:body {:status :unknown}}
{:status http-status/ok
:token token
:body (cond-> job-status
show-http-messages?
(assoc :http-messages (map add-single-parsed-json-response (:http-messages job-status))))}))))))

(defn wrap-uuid-validator [app]
(fn uuid-validator [req]
Expand Down
Loading

0 comments on commit 697b0ad

Please sign in to comment.