Skip to content

Commit

Permalink
xero-request added
Browse files Browse the repository at this point in the history
  • Loading branch information
awb99 committed Jan 9, 2025
1 parent 9319614 commit 8e2c430
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 30 deletions.
35 changes: 29 additions & 6 deletions demo/src/rest/xero.clj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
[martian.core :as martian]
[modular.system]
[modular.rest.paging :refer [request-paginated]]
[rest.provider.xero :refer [martian-xero martian-xero-tenant]]))
[rest.provider.xero :refer [martian-xero martian-xero-tenant xero-request]]))



; this comes from the xero identity token:
Expand Down Expand Up @@ -151,9 +152,25 @@
"woo/KleenToDiTee")

; get contact
#_(martian/response-for
(martian/response-for
t :contact
)


(xero-request t :contact
{:contact-id "2dd7ecba-df6b-4a5d-98a3-daca48f41fae"}
:Contacts)

(try
(xero-request
t :contact
{:contact-id "2dd7ecba-df6b-4a5d-98a3-daca48f41fae"})
{:contact-id "aasdf"})
(catch Exception ex
(println (ex-message ex) (ex-data ex))
)
)



; create contact
#_(martian/response-for
Expand All @@ -166,6 +183,14 @@
{:Name "BatMan! v2"
:ContactID "b93a996f-6a3b-4c63-bc30-249f2e662808" })

(try
(xero-request
t :contact-create
{:Name1 "asdf"})
(catch Exception ex
(println (ex-message ex) (ex-data ex))))


(update-contact-name
"b93a996f-6a3b-4c63-bc30-249f2e662808"
"BatMan! v5")
Expand Down Expand Up @@ -239,9 +264,7 @@
print-invoices)

;(get-request :xero/contacts) );





(let [tenant-id "791f3cb4-97b9-45f9-b5e6-7319cda87626"
params {:where "(Type == \"ACCREC\")"} ;"Date >= DateTime(2022, 01, 01)"
Expand Down
85 changes: 61 additions & 24 deletions rest/src/rest/provider/xero.clj
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
(ns rest.provider.xero
(:require
[jsonista.core :as j] ; json read/write
[martian.core :as martian]
[schema.core :as s]
[martian.interceptors :as interceptors]
[martian.clj-http :as martian-http]
[rest.oauth2 :refer [martian-oauth2 add-authentication-header]]))

(defn parse-json [json]
(j/read-value json j/keyword-keys-object-mapper))

(def endpoints
[{:route-name :userinfo
:summary "user info"
Expand Down Expand Up @@ -40,14 +44,14 @@
:body-schema {:c s/Any}
:produces ["application/json"]
:consumes ["application/json"]}
#_{:route-name :contact-update ; post works for update; put is not needed
:summary "update contact"
:method :put
:path-parts ["/api.xro/2.0/Contacts/" :contact-id]
:path-schema {:contact-id s/Str}
:body-schema {:c {:Contacts s/Any}}
:produces ["application/json"]
:consumes ["application/json"]}
#_{:route-name :contact-update ; post works for update; put is not needed
:summary "update contact"
:method :put
:path-parts ["/api.xro/2.0/Contacts/" :contact-id]
:path-schema {:contact-id s/Str}
:body-schema {:c {:Contacts s/Any}}
:produces ["application/json"]
:consumes ["application/json"]}
{:route-name :add-contacts-to-group
:summary "adds contacts to contact-group"
:method :put
Expand Down Expand Up @@ -137,9 +141,9 @@

(defn martian-xero [this]
(let [m (martian-oauth2 this
:xero
"https://api.xero.com"
endpoints)]
:xero
"https://api.xero.com"
endpoints)]
m))

;; XERO-TENANT
Expand All @@ -153,12 +157,12 @@

(defn- interceptors-tenant [this tenant-id]
(concat
martian/default-interceptors
[(add-authentication-header this :xero)
(add-tenant-header tenant-id)
interceptors/default-encode-body
interceptors/default-coerce-response
martian-http/perform-request]))
martian/default-interceptors
[(add-authentication-header this :xero)
(add-tenant-header tenant-id)
interceptors/default-encode-body
interceptors/default-coerce-response
martian-http/perform-request]))

(defn martian-xero-tenant
([this tenant-id]
Expand All @@ -181,12 +185,45 @@

(defn- interceptors-tenant-since [this tenant-id since]
(concat
(interceptors-tenant this tenant-id)
[(add-modified-since-header since)]))
(interceptors-tenant this tenant-id)
[(add-modified-since-header since)]))

(defn martian-xero-tenant-since [this tenant-id since]
(martian-http/bootstrap
"https://api.xero.com"
endpoints
{:interceptors (interceptors-tenant-since this tenant-id since)}
))
"https://api.xero.com"
endpoints
{:interceptors (interceptors-tenant-since this tenant-id since)}))


(defn xero-request
([m req-type req-opts]
(xero-request m req-type req-opts nil))
([m req-type req-opts extract-type]
(try
(let [body (-> (martian/response-for m req-type req-opts)
:body)]
(if extract-type
(extract-type body)
body))
(catch Exception ex
(let [data (ex-data ex)
data-short (select-keys data [:reason-phrase :type
:status :length
:body])
body (:body data)
body (if (= (:status data) 400)
(try (parse-json body)
(catch Exception _jsex
body))
body)]
;(println "keys: " (keys data))
(println "xero req " req-type req-opts " failed: " data-short)
(if body
(throw (ex-info (str "xero error for " req-type)
(if (map? body)
body
{:error body})))
(throw ex)))))))



0 comments on commit 8e2c430

Please sign in to comment.