-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add BE for routing billing status #38153
Merged
Merged
Changes from all commits
Commits
Show all changes
18 commits
Select commit
Hold shift + click to select a range
19b20de
add BE for billing status
qwef 540b1ae
add BE for billing status
qwef bf91cfc
fix whitespace
qwef aa6a1fe
fix english
qwef 6c56877
address comments
qwef 6701f84
fix whitespace
qwef 1f03e42
fix comments
qwef a66dbb6
update design
qwef ee91fc8
update design
qwef bcb32cc
add test
qwef fede230
consume json error
qwef 9d65c12
Merge branch 'master' into jh-billing-status
qwef 0cef8d2
address comments
qwef 3741eea
Merge branch 'master' into jh-billing-status
qwef a797597
update url
qwef 841845c
Merge branch 'master' into jh-billing-status
qwef 7b0ff08
merge
qwef 26e2dfe
Merge branch 'master' into jh-billing-status
qwef File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should I add a premium feature here? Unsure because it seems like anyone with a token should access this, but no other route is like this.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems fine