<commit_before>(ns cider-ci.utils.http
  (:require
    [cider-ci.utils.with :as with]
    [clj-http.client :as http-client]
    [clj-logging-config.log4j :as logging-config]
    [clojure.tools.logging :as logging]
    [ring.middleware.basic-authentication :refer [wrap-basic-authentication]]
    ))

;(logging-config/set-logger! :level :debug)
;(logging-config/set-logger! :level :info)


(defonce conf (atom nil))

(defn post [url params]
  (logging/debug post [url params])
  (let [basic-auth (:basic_auth @conf)]
    (with/logging
      (logging/debug "http/post" {:url url :basic-auth basic-auth})
      (http-client/post 
        url
        (conj {:basic-auth [(:user basic-auth) (:secret basic-auth)]
               :insecure? true
               :content-type :json
               :accept :json 
               :socket-timeout 1000  
               :conn-timeout 1000 }
              params)))))

; TODO dry-up
(defn put [url params]
  (logging/debug post [url params])
  (let [basic-auth (:basic_auth @conf)]
    (with/logging
      (logging/debug "http/put" {:url url :basic-auth basic-auth})
      (http-client/put
        url
        (conj {:basic-auth [(:user basic-auth) (:secret basic-auth)]
               :insecure? true
               :content-type :json
               :accept :json 
               :socket-timeout 1000  
               :conn-timeout 1000 }
              params)))))

; TODO dry-up
(defn patch [url params]
  (logging/debug post [url params])
  (let [basic-auth (:basic_auth @conf)]
    (with/logging
      (logging/debug "http/patch" {:url url :basic-auth basic-auth})
      (http-client/patch
        url
        (conj {:basic-auth [(:user basic-auth) (:secret basic-auth)]
               :insecure? true
               :content-type :json
               :accept :json 
               :socket-timeout 1000  
               :conn-timeout 1000 }
              params)))))




(defn initialize [new-conf]
  (reset! conf new-conf))


;### Http Basic Authentication ################################################

(defn authenticated? [application password]
  (logging/debug authenticated? [application password])
  (and 
    (= password
       (-> @conf (:basic_auth) (:secret)))
    (keyword application)))


(defn authenticate [handler]
   (wrap-basic-authentication handler authenticated?))

<commit_msg>Clean and dry off http<commit_after>(ns cider-ci.utils.http
  (:require
    [cider-ci.utils.with :as with]
    [clj-http.client :as http-client]
    [clj-logging-config.log4j :as logging-config]
    [clojure.tools.logging :as logging]
    [ring.middleware.basic-authentication :refer [wrap-basic-authentication]]
    ))

;(logging-config/set-logger! :level :debug)
;(logging-config/set-logger! :level :info)


(defonce conf (atom nil))


;### Http request #############################################################

(defn- request [method url params]
  (logging/debug [method url params])
  (let [basic-auth (:basic_auth @conf)]
    (with/logging
      (logging/debug "http/post" {:url url :basic-auth basic-auth})
      (http-client/request
        (conj {:basic-auth [(:user basic-auth) (:secret basic-auth)]
               :url url 
               :method method
               :insecure? true
               :content-type :json
               :accept :json 
               :socket-timeout 1000  
               :conn-timeout 1000 }
              params)))))

(defn post [url params]
  (logging/debug post [url params])
  (request :post url params))

(defn put [url params]
  (logging/debug put [url params])
  (request :put url params))

(defn patch [url params]
  (logging/debug patch [url params])
   (request :patch url params))


;### Http Basic Authentication ################################################

(defn authenticated? [application password]
  (logging/debug authenticated? [application password])
  (and 
    (= password
       (-> @conf (:basic_auth) (:secret)))
    (keyword application)))

(defn authenticate [handler]
   (wrap-basic-authentication handler authenticated?))


;### Initialize ###############################################################


(defn initialize [new-conf]
  (reset! conf new-conf))



{'fraction_non_alphanumeric': 0.1823178016726404, 'fraction_numerical': 0.008124253285543608, 'mean_word_length': 2.621107266435986, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '3715739', 'n_tokens_mistral': 1311, 'n_tokens_neox': 1222, 'n_words': 319}