Skip to content

Commit

Permalink
refactor: make updating OI knowledge a babashka task
Browse files Browse the repository at this point in the history
Switching to babashka tasks to help organize the code a bit better.
  • Loading branch information
djwhitt committed Oct 23, 2024
1 parent 9165b02 commit f5cc838
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 67 deletions.
4 changes: 4 additions & 0 deletions bb.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{:paths ["bb"]
:tasks
{:requires ([metasys.tasks :as tasks])
update-oi-knowledge (tasks/update-oi-knowledge)}}
43 changes: 43 additions & 0 deletions bb/metasys/tasks.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
(ns metasys.tasks
(:require [babashka.http-client :as http]
[cheshire.core :as json]
[clojure.edn :as edn]
[clojure.string :as str]
[taoensso.timbre :as log]))

(defn strip-quotes [s]
(clojure.string/replace s #"^[\"](.*)[\"]$" "$1"))

(defn read-env []
(->> (slurp ".env")
(str/split-lines)
(remove #(str/starts-with? % "#")) ; Remove comments
(remove #(str/blank? %)) ; Remove blank lines
(map #(str/split % #"\s*=\s*")) ; Split key-value pairs
(map (fn [[k v]] [k (strip-quotes v)])) ; Strip value leading and trailing quotes
(into {})))

;; Read and parse .env into a map
(def env (read-env))

(def oi-api-key (env "OI_API_KEY"))
(def oi-url (env "OI_URL"))

(def oi-knowledge (-> "oi-knowledge.edn" slurp edn/read-string))

(defn update-oi-knowledge []
(doseq [[k-id k] oi-knowledge]
(doseq [[f-id path] (:files k)
:let [f-url (str oi-url "/api/v1/files/" f-id "/data/content/update")
k-url (str oi-url "/api/v1/knowledge/" k-id "/file/update")]]
(log/infof "Updating file %s in knowledge %s with contents of %s..." f-id k-id path)
(http/post f-url
{:headers {:content-type "application/json"}
:oauth-token oi-api-key
:body (json/generate-string {"content" (slurp path)})})
(log/infof "Updating file %s in knowledge %s..." f-id k-id path)
(http/post k-url
{:headers {:content-type "application/json"}
:oauth-token oi-api-key
:body (json/generate-string {"file_id" f-id})})
(log/info "Updates complete."))))
1 change: 1 addition & 0 deletions deps.edn
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{:paths ["bb"]}
20 changes: 0 additions & 20 deletions scripts/update-oi-files

This file was deleted.

47 changes: 0 additions & 47 deletions scripts/update-oi-knowledge

This file was deleted.

0 comments on commit f5cc838

Please sign in to comment.