<commit_before>(ns stripe.http
  (:require [cheshire.core :as json]
            [clojure.core.async :as a]
            [environ.core :as e]
            [org.httpkit.client :as http]
            [schema.core :as s]
            [stripe.schema :as ss]
            [stripe.util :as u]))

;; ## Schema

(def ^:dynamic url "https://api.stripe.com/v1/")

(s/defschema ApiToken
  (s/named s/Str "Secret stripe key."))

(s/defschema Expansion
  "Supply a keyword like :balance_transaction or a sequence to expand
  multiple fields. To expand nested fields, string the items together
  in the keyword with a dot, like :balance_transaction.source."
  (s/either s/Keyword [s/Keyword]))

(s/defschema HttpKitOptions
  "Options for Http-Kit's client."
  {s/Any s/Any})

(s/defschema StripeParams
  "Post data or Get req data for Stripe."
  {s/Any s/Any})

;; ## Authorization

(def ^:dynamic *token* nil)
(def ^:dynamic *api-version* nil)

(s/defn api-token :- (s/maybe ApiToken)
  []
  (or *token* (:stripe-secret e/env)))

(s/defn api-version :- (s/maybe s/Str)
  []
  (or *api-version* (:stripe-api-version e/env)))

(defmacro with-token [k & forms]
  `(binding [*token* ~k]
     ~@forms))

(defn use-token!
  "Permanently sets a base token. The token can still be overridden on
  a per-thread basis using with-token."
  [s]
  (alter-var-root #'*token* (constantly s)))

(defmacro with-api-version
  [v & forms]
  `(binding [*api-version* ~v]
     ~@forms))

(defn use-api-version!
  "Permanently sets an API version. The api version can still be
  overridden on a per-thread basis using with-api-version."
  [s]
  (alter-var-root #'*api-version* (constantly s)))


;; ## Private

(s/defn method-url :- s/Str
  "URL for calling a method."
  [method :- s/Str]
  (str url method))

(s/defn prepare-expansion :- [s/Str]
  "Turns the expansion's keywords into strings. In the future, this
  might validate that all expansions are reliable jumps based on the
  Stripe API's allowed expansion fields for each object."
  [expand :- Expansion]
  (map name (u/collectify expand)))

(s/defn prepare-params :- {s/Any s/Any}
  "Returns a parameter map suitable for feeding in to a request to Stripe.

  `opts` is a set of options for http-kit's client. These kick out the
  defaults.

  `params` is the parameters for the stripe API calls."
  [token :- ApiToken
   params :- StripeParams
   opts :- HttpKitOptions]
  (let [params (if-let [expand (:expand params)]
                 (-> (dissoc params :expand)
                     (assoc "expand[]" (prepare-expansion expand)))
                 params)
        base-params {:basic-auth token
                     :query-params params
                     :throw-exceptions false}
        version (when-let [v (or (:api-version opts) *api-version*)]
                  {:headers {"Stripe-Version" v}})]
    (merge base-params version (dissoc opts :api-version))))

;; ## Public Methods
;;
;; These are a mishmash of the stripe API methods, and should probably
;; be broken out into a bunch of sub namespaces.
;;
;; ## Method Call Helpers

(def Method<commit_msg>fix api version bug<commit_after>(ns stripe.http
  (:require [cheshire.core :as json]
            [clojure.core.async :as a]
            [environ.core :as e]
            [org.httpkit.client :as http]
            [schema.core :as s]
            [stripe.schema :as ss]
            [stripe.util :as u]))

;; ## Schema

(def ^:dynamic url "https://api.stripe.com/v1/")

(s/defschema ApiToken
  (s/named s/Str "Secret stripe key."))

(s/defschema Expansion
  "Supply a keyword like :balance_transaction or a sequence to expand
  multiple fields. To expand nested fields, string the items together
  in the keyword with a dot, like :balance_transaction.source."
  (s/either s/Keyword [s/Keyword]))

(s/defschema HttpKitOptions
  "Options for Http-Kit's client."
  {s/Any s/Any})

(s/defschema StripeParams
  "Post data or Get req data for Stripe."
  {s/Any s/Any})

;; ## Authorization

(def ^:dynamic *token* nil)
(def ^:dynamic *api-version* nil)

(s/defn api-token :- (s/maybe ApiToken)
  []
  (or *token* (:stripe-secret e/env)))

(s/defn api-version :- (s/maybe s/Str)
  []
  (or *api-version* (:stripe-api-version e/env)))

(defmacro with-token [k & forms]
  `(binding [*token* ~k]
     ~@forms))

(defn use-token!
  "Permanently sets a base token. The token can still be overridden on
  a per-thread basis using with-token."
  [s]
  (alter-var-root #'*token* (constantly s)))

(defmacro with-api-version
  [v & forms]
  `(binding [*api-version* ~v]
     ~@forms))

(defn use-api-version!
  "Permanently sets an API version. The api version can still be
  overridden on a per-thread basis using with-api-version."
  [s]
  (alter-var-root #'*api-version* (constantly s)))


;; ## Private

(s/defn method-url :- s/Str
  "URL for calling a method."
  [method :- s/Str]
  (str url method))

(s/defn prepare-expansion :- [s/Str]
  "Turns the expansion's keywords into strings. In the future, this
  might validate that all expansions are reliable jumps based on the
  Stripe API's allowed expansion fields for each object."
  [expand :- Expansion]
  (map name (u/collectify expand)))

(s/defn prepare-params :- {s/Any s/Any}
  "Returns a parameter map suitable for feeding in to a request to Stripe.

  `opts` is a set of options for http-kit's client. These kick out the
  defaults.

  `params` is the parameters for the stripe API calls."
  [token :- ApiToken
   params :- StripeParams
   opts :- HttpKitOptions]
  (let [params (if-let [expand (:expand params)]
                 (-> (dissoc params :expand)
                     (assoc "expand[]" (prepare-expansion expand)))
                 params)
        base-params {:basic-auth token
                     :query-params params
                     :throw-exceptions false}
        version (when-let [v (or (:api-version opts) (api-version))]
                  {:headers {"Stripe-Version" v}})]
    (merge base-params version (dissoc opts :api-version))))

;; ## Public Methods
;;
;; These are a mishmash of the stripe API methods, and should probably
;; be broken out into a bunch of sub namespaces.
;;
;; ## Method Call Helpers

(def Method
{'fraction_non_alphanumeric': 0.13240530916154095, 'fraction_numerical': 0.0003237293622531564, 'mean_word_length': 2.809494451294698, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 2, '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': '1038279', 'n_tokens_mistral': 2080, 'n_tokens_neox': 2001, 'n_words': 686}