Skip to content

Commit

Permalink
add support for debug mode
Browse files Browse the repository at this point in the history
  • Loading branch information
kacurez committed Sep 6, 2024
1 parent e974cf0 commit a0873dd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
5 changes: 5 additions & 0 deletions src/keboola/facebook/extractor/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
[keboola.facebook.extractor.query :as query]
[keboola.facebook.extractor.sync-actions :as sync-actions]
[keboola.http.client :refer [fb-requests-count]]
[keboola.http.recording :refer [turn-log-responses-on]]
[keboola.utils.json-to-csv :as csv]
[slingshot.slingshot :refer [throw+ try+]]))

Expand Down Expand Up @@ -64,6 +65,10 @@
app-access-token (docker-config/app-access-token datadir)
out-dir-path (docker-config/out-dir-path datadir)
credentials (docker-config/user-credentials datadir)]
(when (:debug-mode parameters)
(log "Running in debug mode")
(sync-actions/disable-log-token)
(turn-log-responses-on))
(cond
(empty? credentials) (docker-runtime/user-error "Missing facebook credentials")
(empty? (docker-config/get-fb-token credentials)) (docker-runtime/user-error "Missing facebook token"))
Expand Down
18 changes: 17 additions & 1 deletion src/keboola/http/recording.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@

(def recording (atom '()))
(def do-recording? (atom false))
(def do-log-responses? (atom false))


(defn reset-recording [] (reset! recording '()))

(defn turn-recording-on [] (reset! do-recording? true))
(defn turn-recording-off [] (reset! do-recording? false))

(defn turn-log-responses-on [] (reset! do-log-responses? true))

(def VALID-CHARS
(map char (concat (range 48 58) ; 0-9
(range 66 91) ; A-Z
Expand All @@ -36,8 +40,20 @@
(defn anonymize-map [m]
(postwalk anonymize-item m))


(defn replace-token-by-regexp [item]
(if (string? item)
(clojure.string/replace item #"access_token=[^&]*" "access_token=TOKEN")
item))

(defn record-request [response method url request-rest]
#_(println (pr-str request-rest))
(when @do-log-responses?
(let [result-map {:method method
:url url
:response {:status (:status response) :body (:body response)}}
result (postwalk replace-token-by-regexp result-map)]
(println (generate-string result {:pretty true})))
(Thread/sleep 350))
(if @do-recording?
(let [request-base {:method method :address url}
request (merge request-base (apply hash-map request-rest))
Expand Down

0 comments on commit a0873dd

Please sign in to comment.