-
Notifications
You must be signed in to change notification settings - Fork 4.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add BE for routing billing status (#38153)
* add BE for billing status * add BE for billing status * fix whitespace * fix english * address comments * fix whitespace * fix comments * update design * update design * add test * consume json error * address comments * update url
- Loading branch information
Showing
5 changed files
with
79 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
enterprise/backend/src/metabase_enterprise/billing/api/routes.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
(ns metabase-enterprise.billing.api.routes | ||
"API endpoint(s) that are only enabled if ee is enabled. These live under `/api/ee/billing/`. We don't feature flag this | ||
endpoint unlike other ee endpoints." | ||
(:require | ||
[compojure.core :as compojure] | ||
[metabase-enterprise.billing.billing :as billing] | ||
[metabase.api.routes.common :refer [+auth]])) | ||
|
||
(compojure/defroutes ^{:doc "Ring routes for billing API endpoints."} routes | ||
(compojure/context "/" [] (+auth billing/routes))) |
43 changes: 43 additions & 0 deletions
43
enterprise/backend/src/metabase_enterprise/billing/billing.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
(ns metabase-enterprise.billing.billing | ||
"`/api/ee/billing/` endpoint(s)" | ||
(:require | ||
[cheshire.core :as json] | ||
[clj-http.client :as http] | ||
[clojure.core.memoize :as memoize] | ||
[compojure.core :as compojure :refer [GET]] | ||
[metabase.api.common :as api] | ||
[metabase.public-settings.premium-features :as premium-features] | ||
[metabase.util :as u] | ||
[metabase.util.i18n :as i18n] | ||
[toucan2.core :as t2]) | ||
(:import | ||
[com.fasterxml.jackson.core JsonParseException])) | ||
|
||
(set! *warn-on-reflection* true) | ||
|
||
(def ^:private ^String metabase-billing-info-url "https://store-api.metabase.com/api/v2/metabase/billing_info") | ||
|
||
(def ^:private ^{:arglists '([token email language])} fetch-billing-status* | ||
(memoize/ttl | ||
^{::memoize/args-fn (fn [[token email language]] [token email language])} | ||
(fn [token email language] | ||
(try (some-> metabase-billing-info-url | ||
(http/get {:basic-auth [email token] | ||
:language language | ||
:content-type :json}) | ||
:body | ||
(json/parse-string keyword)) | ||
(catch JsonParseException _ | ||
{:content nil}))) | ||
:ttl/threshold (u/hours->ms 5))) | ||
|
||
(api/defendpoint GET "/" | ||
"Get billing information. This acts as a proxy between `metabase-billing-info-url` and the client, | ||
using the embedding token and signed in user's email to fetch the billing information." | ||
[] | ||
(let [token (premium-features/premium-embedding-token) | ||
email (t2/select-one-fn :email :model/User :id api/*current-user-id*) | ||
language (i18n/user-locale-string)] | ||
(fetch-billing-status* token email language))) | ||
|
||
(api/define-routes) |
21 changes: 21 additions & 0 deletions
21
enterprise/backend/test/metabase_enterprise/billing/billing_test.clj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
(ns metabase-enterprise.billing.billing-test | ||
(:require [clj-http.client :as http] | ||
[clojure.test :refer :all] | ||
[metabase.test :as mt])) | ||
|
||
(deftest fetch-billing-status-test | ||
(testing "Passes through billing status fetched from server" | ||
(binding [http/request (fn [& _] | ||
{:status 200 | ||
:body "{\"version\":\"v1\",\"content\":null}"})] | ||
(is (= {:version "v1" | ||
:content nil} | ||
(mt/user-http-request :rasta :get 200 "/ee/billing")))))) | ||
|
||
(deftest fetch-billing-status-error-test | ||
(testing "When receiving a non json result consume the error and return an empty content blob" | ||
(binding [http/request (fn [& _] | ||
{:status 404 | ||
:body "error"})] | ||
(is (= {:content nil} | ||
(mt/user-http-request :crowberto :get 200 "/ee/billing")))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters