forked from frappe/crm
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: added saas api's to communicate with frappe cloud
- Loading branch information
1 parent
71406a4
commit 32c4b24
Showing
5 changed files
with
65 additions
and
7 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import frappe | ||
import requests | ||
|
||
def get_base_url(): | ||
url = "https://frappecloud.com" | ||
if frappe.conf.developer_mode and frappe.conf.get("saas_billing_base_url"): | ||
url = frappe.conf.get("saas_billing_base_url") | ||
return url | ||
|
||
def get_site_name(): | ||
site_name = frappe.local.site | ||
if frappe.conf.developer_mode and frappe.conf.get("saas_billing_site_name"): | ||
site_name = frappe.conf.get("saas_billing_site_name") | ||
return site_name | ||
|
||
def get_headers(): | ||
# check if user is system manager | ||
if frappe.get_roles(frappe.session.user).count("System Manager") == 0: | ||
frappe.throw("You are not allowed to access this resource") | ||
|
||
# check if communication secret is set | ||
if not frappe.conf.get("fc_communication_secret"): | ||
frappe.throw("Communication secret not set") | ||
|
||
return { | ||
"X-Site-Token": frappe.conf.get("fc_communication_secret"), | ||
"X-Site": get_site_name() | ||
} | ||
|
||
@frappe.whitelist() | ||
def generate_access_token(): | ||
request = requests.post(f"{get_base_url()}/api/method/press.saas.api.auth.generate_access_token", headers=get_headers()) | ||
if request.status_code == 200: | ||
return request.json()["message"] | ||
else: | ||
frappe.throw("Failed to generate access token") | ||
|
||
@frappe.whitelist() | ||
def is_access_token_valid(token): | ||
headers={ 'Content-Type': 'application/json' } | ||
request = requests.post(f"{get_base_url()}/api/method/press.saas.api.auth.is_access_token_valid", headers, json={ token }) | ||
return request.json()["message"] | ||
|
||
@frappe.whitelist() | ||
def current_site_info(): | ||
request = requests.post(f"{get_base_url()}/api/method/press.saas.api.site.info", headers=get_headers()) | ||
if request.status_code == 200: | ||
return request.json().get("message") | ||
else: | ||
frappe.throw("Failed to get site info") | ||
|
||
@frappe.whitelist() | ||
def saas_api(method, data={}): | ||
request = requests.post(f"{get_base_url()}/api/method/press.saas.api.{method}", headers=get_headers(), json=data) | ||
print(request.json()) | ||
return request.json().get("message") |
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
Submodule frappe-ui
updated
10 files
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
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