<commit_before>(ns swarmpit.handler
  (:require [cemerick.url :refer [query->map]]
            [clojure.walk :refer [keywordize-keys]]
            [clojure.java.io :as io]
            [swarmpit.api :as api]
            [swarmpit.token :as token]))

(defn resp-error
  [status response]
  {:status status
   :body   {:error response}})

(defn resp-unauthorized
  [response]
  {:status 401
   :body   {:error response}})

(defn resp-ok
  ([] {:status 200})
  ([response] {:status 200
               :body   response}))

(defn resp-created
  ([] {:status 201})
  ([response] {:status 201
               :body   response}))

;;; Handler

(defmulti dispatch identity)

;; Index handler

(defmethod dispatch :index [_]
  (fn [_]
    {:status  200
     :headers {"Content-Type" "text/html"}
     :body    (slurp (io/resource "public/index.html"))}))

;; Login handler

(defmethod dispatch :login [_]
  (fn [{:keys [headers]}]
    (let [token (get headers "authorization")]
      (if (nil? token)
        (resp-error 400 "Missing token")
        (let [user (->> (token/decode-basic token)
                        (api/user-by-credentials))]
          (if (nil? user)
            (resp-unauthorized "Invalid credentials")
            (resp-ok {:token (token/generate-jwt user)})))))))

;; User handler

(defmethod dispatch :users [_]
  (fn [_]
    (->> (api/users)
         (resp-ok))))

(defmethod dispatch :user [{:keys [route-params]}]
  (fn [_]
    (->> (api/user (:id route-params))
         (resp-ok))))

(defmethod dispatch :user-create [{:keys [params]}]
  (fn [_]
    (let [payload (keywordize-keys params)]
      (if (some? (api/create-registry payload))
        (resp-created)
        (resp-error 400 "User already exist")))))

;; Registry handler

(defmethod dispatch :registries [_]
  (fn [_]
    (->> (api/registries)
         (resp-ok))))

(defmethod dispatch :registry [{:keys [route-params]}]
  (fn [_]
    (->> (api/registry (:id route-params))
         (resp-ok))))

(defmethod dispatch :registries-sum [_]
  (fn [_]
    (->> (api/registries-sum)
         (resp-ok))))

(defmethod dispatch :registry-create [_]
  (fn [{:keys [params]}]
    (let [payload (keywordize-keys params)]
      (if (api/registry-valid? payload)
        (if (some? (api/create-registry payload))
          (resp-created)
          (resp-error 400 "Registry already exist"))
        (resp-error 400 "Registry credentials does not match any known registry")))))

;; Service handler

(defmethod dispatch :services [_]
  (fn [_]
    (->> (api/services)
         (resp-ok))))<commit_msg>fix rest handlers<commit_after>(ns swarmpit.handler
  (:require [cemerick.url :refer [query->map]]
            [clojure.walk :refer [keywordize-keys]]
            [clojure.java.io :as io]
            [swarmpit.api :as api]
            [swarmpit.token :as token]))

(defn resp-error
  [status response]
  {:status status
   :body   {:error response}})

(defn resp-unauthorized
  [response]
  {:status 401
   :body   {:error response}})

(defn resp-ok
  ([] {:status 200})
  ([response] {:status 200
               :body   response}))

(defn resp-created
  ([] {:status 201})
  ([response] {:status 201
               :body   response}))

;;; Handler

(defmulti dispatch identity)

;; Index handler

(defmethod dispatch :index [_]
  (fn [_]
    {:status  200
     :headers {"Content-Type" "text/html"}
     :body    (slurp (io/resource "public/index.html"))}))

;; Login handler

(defmethod dispatch :login [_]
  (fn [{:keys [headers]}]
    (let [token (get headers "authorization")]
      (if (nil? token)
        (resp-error 400 "Missing token")
        (let [user (->> (token/decode-basic token)
                        (api/user-by-credentials))]
          (if (nil? user)
            (resp-unauthorized "Invalid credentials")
            (resp-ok {:token (token/generate-jwt user)})))))))

;; User handler

(defmethod dispatch :users [_]
  (fn [_]
    (->> (api/users)
         (resp-ok))))

(defmethod dispatch :user [_]
  (fn [{:keys [route-params]}]
    (->> (api/user (:id route-params))
         (resp-ok))))

(defmethod dispatch :user-create [_]
  (fn [{:keys [params]}]
    (let [payload (keywordize-keys params)]
      (if (some? (api/create-registry payload))
        (resp-created)
        (resp-error 400 "User already exist")))))

;; Registry handler

(defmethod dispatch :registries [_]
  (fn [_]
    (->> (api/registries)
         (resp-ok))))

(defmethod dispatch :registry [_]
  (fn [{:keys [route-params]}]
    (->> (api/registry (:id route-params))
         (resp-ok))))

(defmethod dispatch :registries-sum [_]
  (fn [_]
    (->> (api/registries-sum)
         (resp-ok))))

(defmethod dispatch :registry-create [_]
  (fn [{:keys [params]}]
    (let [payload (keywordize-keys params)]
      (if (api/registry-valid? payload)
        (if (some? (api/create-registry payload))
          (resp-created)
          (resp-error 400 "Registry already exist"))
        (resp-error 400 "Registry credentials does not match any known registry")))))

;; Service handler

(defmethod dispatch :services [_]
  (fn [_]
    (->> (api/services)
         (resp-ok))))
{'fraction_non_alphanumeric': 0.17328660436137072, 'fraction_numerical': 0.011682242990654205, 'mean_word_length': 2.6099789177793395, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1335090', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1664, 'n_words': 421}