|
25 | 25 | (format "%s/statements" endpoint)) |
26 | 26 |
|
27 | 27 | (defn- post-options [http-options batch] |
28 | | - (merge default-http-options |
29 | | - http-options |
30 | | - {:body (json/encode batch) |
31 | | - :as :stream})) |
| 28 | + (merge-with merge |
| 29 | + default-http-options |
| 30 | + http-options |
| 31 | + {:body (json/encode batch) |
| 32 | + :as :stream})) |
32 | 33 |
|
33 | 34 | ;; `http/post` cannot be resolved since it's defined using `http/defreq` |
34 | 35 | #_{:clj-kondo/ignore [:unresolved-var]} |
|
41 | 42 | (post-options http-options batch) |
42 | 43 | callback-fn))) |
43 | 44 |
|
| 45 | +(defn- auth-options |
| 46 | + [{:keys [username |
| 47 | + password |
| 48 | + token |
| 49 | + cookie]}] |
| 50 | + (cond |
| 51 | + (and username password) |
| 52 | + {:basic-auth [username password]} |
| 53 | + token |
| 54 | + {:headers {"Authorization" (format "Bearer %s" token)}} |
| 55 | + cookie |
| 56 | + {:headers {"Cookie" token}} |
| 57 | + :else {})) |
| 58 | + |
44 | 59 | (defn post-statements |
45 | 60 | "Given LRS options and a `statement-seq`, send them to an LRS in synchronous |
46 | 61 | batches. If `print-ids?` is `true`, returned statement IDs will be printed |
47 | 62 | to stdout. `username` and `password` in the options map are the Basic Auth |
48 | 63 | credentials of the LRS." |
49 | 64 | [{:keys [endpoint |
50 | | - batch-size |
51 | | - username |
52 | | - password] |
| 65 | + batch-size] |
| 66 | + :as options |
53 | 67 | :or {batch-size 25}} |
54 | 68 | statement-seq |
55 | 69 | & {:keys [print-ids?] |
56 | 70 | :or {print-ids? true}}] |
57 | 71 | ;; TODO: Exponential backoff, etc |
58 | | - (let [http-options {:basic-auth [username password]}] |
| 72 | + (let [http-options (auth-options options)] |
59 | 73 | (loop [batches (partition-all batch-size statement-seq) |
60 | 74 | success 0 |
61 | 75 | fail []] |
|
82 | 96 | (defn post-statements-async |
83 | 97 | "Given LRS options and a channel with statements, send them to an LRS in |
84 | 98 | asynchronous batches. `username` and `password` in the options map are the |
85 | | - Basic Auth credentials of the LRS. |
| 99 | + Basic Auth credentials of the LRS. Other auth methods are supported via |
| 100 | + `token` and `cookie`. |
86 | 101 |
|
87 | 102 | Returns a channel that will reciveve `[:success <list of statement ids>]` |
88 | 103 | for each batch or `[:fail <failing request>]`. Will stop sending on failure." |
89 | 104 | [{:keys [endpoint |
90 | | - batch-size |
91 | | - username |
92 | | - password] |
| 105 | + batch-size] |
| 106 | + :as options |
93 | 107 | :or {batch-size 25}} |
94 | 108 | statement-chan |
95 | 109 | & {:keys [concurrency |
|
98 | 112 | :or {concurrency 4 |
99 | 113 | buffer-in 100 ; 10x default batch size |
100 | 114 | buffer-out 100}}] |
101 | | - (let [http-opts {:basic-auth [username password]} |
| 115 | + (let [http-opts (auth-options options) |
102 | 116 | run? (atom true) |
103 | 117 | in-chan (a/chan buffer-in (partition-all batch-size)) |
104 | 118 | out-chan (a/chan buffer-out) ; is this.. backpressure? |
|
0 commit comments