<commit_before>(ns cordula.proxy
  (:require [clojure.algo.generic.functor :refer [fmap]]
            [clojure.string :as string]
            [clojure.tools.logging :as log]
            [clojure.walk :refer [stringify-keys]]
            [clj-http.client :as client]
            [cordula.schema :refer :all]
            [ring.util.http-response :refer :all]
            [schema.core :as s]))

(defn with-default-options
  [p]
  (into p
        {:throw-exceptions false
         :as :stream
         :insecure? true
         :force-redirects false
         :follow-redirects false
         :decompress-body false
         :socket-timeout 1000
         :conn-timeout 1000
         :debug true}))

(defn- format-param->value
  [[_ param-str] values]
  (let [path (string/split param-str #"\/")]
    (get-in values path "")))

(defn format-str
  "Replace variables with their value. Variables are tags enclosed by curly
  brackets prefixed by a tild and will be replaced with the respective data.

  Ex:
  Template:
  \"Hello ~{params/param1}!\"

  Data:
  {\"params\" {\"param1\" \"Matthieu\"}}

  Result:
  \"Hello Matthieu!\"
  "
  [s values]
  (string/replace s #"~\{([^~\{]*)\}" #(format-param->value % values)))

(s/defn with-headers
  "Merge request headers with those configured."
  [params req conf :- Request apply-template]
  (let [req-headers (dissoc (:headers req) "host" "content-length")
        conf-headers (fmap apply-template
                           (get-in conf [:proxy :headers] {}))]
    (into params {:headers (into req-headers conf-headers)})))

(s/defn with-body
  "Replace the request body by the configured one."
  [params req conf :- Request apply-template]
  (let [conf-body (apply-template
                   (get-in conf [:proxy :body]))<commit_msg>Check nil value for template string<commit_after>(ns cordula.proxy
  (:require [clojure.algo.generic.functor :refer [fmap]]
            [clojure.string :as string]
            [clojure.tools.logging :as log]
            [clojure.walk :refer [stringify-keys]]
            [clj-http.client :as client]
            [cordula.schema :refer :all]
            [ring.util.http-response :refer :all]
            [schema.core :as s]))

(defn with-default-options
  [p]
  (into p
        {:throw-exceptions false
         :as :stream
         :insecure? true
         :force-redirects false
         :follow-redirects false
         :decompress-body false
         :socket-timeout 1000
         :conn-timeout 1000
         :debug true}))

(defn- format-param->value
  [[_ param-str] values]
  (let [path (string/split param-str #"\/")]
    (get-in values path "")))

(defn format-str
  "Replace variables with their value. Variables are tags enclosed by curly
  brackets prefixed by a tild and will be replaced with the respective data.

  Ex:
  Template:
  \"Hello ~{params/param1}!\"

  Data:
  {\"params\" {\"param1\" \"Matthieu\"}}

  Result:
  \"Hello Matthieu!\"
  "
  [s values]
  (when s
    (string/replace s #"~\{([^~\{]*)\}" #(format-param->value % values))))

(s/defn with-headers
  "Merge request headers with those configured."
  [params req conf :- Request apply-template]
  (let [req-headers (dissoc (:headers req) "host" "content-length")
        conf-headers (fmap apply-template
                           (get-in conf [:proxy :headers] {}))]
    (into params {:headers (into req-headers conf-headers)})))

(s/defn with-body
  "Replace the request body by the configured one."
  [params req conf :- Request apply-template]
  (let [conf-body (apply-template
                   (get-in conf [:proxy :body]))
{'fraction_non_alphanumeric': 0.1414647730437204, 'fraction_numerical': 0.00556947925368978, 'mean_word_length': 2.414448669201521, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '4700717', 'n_tokens_mistral': 1165, 'n_tokens_neox': 1120, 'n_words': 324}