Skip to content

Commit

Permalink
Merge pull request #51 from avisi-apps/task/50-retrieving-existing-at…
Browse files Browse the repository at this point in the history
…tributes-should-not-throw-error-when-contact-not-found

Only log warning when retrieving app attributes for non-existing contact
  • Loading branch information
Demivk authored Dec 19, 2023
2 parents 830fcef + 98cc086 commit c1f9eba
Showing 1 changed file with 38 additions and 19 deletions.
57 changes: 38 additions & 19 deletions modules/brevo/src/com/avisi_apps/gaps/brevo/core.cljc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
(ns com.avisi-apps.gaps.brevo.core
(:require
[clojure.string :as str]
[com.avisi-apps.gaps.log :as log]
#?(:clj [clj-http.client :as http])
#?(:clj [jsonista.core :as json])
#?(:clj [clj-http.util :as http-util])
Expand Down Expand Up @@ -44,6 +45,9 @@

(def monday-monetization-enabled-field "MONDAY_MONETIZATION_ENABLED")

; Exceptions
(def document-not-found-code "document_not_found")

(defn create-app-attribute-string
"Transforms clojure map into a string value for the app specific attribute
{A 1, B 2} -> A:1__B:2"
Expand Down Expand Up @@ -104,26 +108,41 @@
"Returns the attributes for app-attribute as a map."
[api-key endpoint app-attribute]
#?(:cljs
(p/then
(axios-client/get
{:endpoint endpoint
:headers {:api-key api-key}})
(fn [result]
(->
(js->clj result :keywordize-keys true)
(get-in [:data :attributes (keyword app-attribute)])
(read-app-attribute-string))))
:clj
(->
(http/get
endpoint
{:headers {"api-key" api-key}
:content-type :json})
:body
(json/read-value (json/object-mapper {:decode-key-fn true}))
:attributes
(keyword app-attribute)
(read-app-attribute-string))))
(p/then
(axios-client/get
{:endpoint endpoint
:headers {:api-key api-key}})
(fn [result]
(->
(js->clj result :keywordize-keys true)
(get-in [:data :attributes (keyword app-attribute)])
(read-app-attribute-string))))
(p/catch
(fn [e]
(let [code (->
(ex-data e)
(js->clj :keywordize-keys true)
(get-in [:response-data "code"]))]
(if (= code document-not-found-code) (log/warn {:message "Contact could not be found"}) (throw e))))))
:clj
(try
(->
(http/get
endpoint
{:headers {"api-key" api-key}
:content-type :json})
:body
(json/read-value (json/object-mapper {:decode-key-fn true}))
:attributes
(keyword app-attribute)
(read-app-attribute-string))
(catch Exception e
(let [code (->
(ex-data e)
(json/read-value (json/object-mapper {:decode-key-fn true}))
(get-in [:response-data "code"]))]
(if (= code document-not-found-code) (log/warn {:message "Contact could not be found"}) (throw e)))))))

(defn get-contact-app-attribute-by-id [api-key brevo-id app-attribute]
(if-not (str/blank? brevo-id)
Expand Down

0 comments on commit c1f9eba

Please sign in to comment.