perf: defer many requests imports
This commit is contained in:
parent
4aa20c72c1
commit
71b44efcac
5 changed files with 22 additions and 13 deletions
|
|
@ -7,8 +7,6 @@ from typing import TYPE_CHECKING, Optional
|
|||
from urllib.parse import unquote
|
||||
|
||||
import filetype
|
||||
import requests
|
||||
import requests.exceptions
|
||||
from PIL import Image
|
||||
|
||||
import frappe
|
||||
|
|
@ -116,6 +114,9 @@ def get_local_image(file_url: str) -> tuple["ImageFile", str, str]:
|
|||
|
||||
|
||||
def get_web_image(file_url: str) -> tuple["ImageFile", str, str]:
|
||||
import requests
|
||||
import requests.exceptions
|
||||
|
||||
# download
|
||||
file_url = frappe.utils.get_url(file_url)
|
||||
r = requests.get(file_url, stream=True)
|
||||
|
|
|
|||
|
|
@ -3,8 +3,6 @@
|
|||
|
||||
import json
|
||||
|
||||
import requests
|
||||
|
||||
import frappe
|
||||
from frappe import _
|
||||
from frappe.model.document import Document
|
||||
|
|
@ -24,6 +22,8 @@ class SlackWebhookURL(Document):
|
|||
|
||||
|
||||
def send_slack_message(webhook_url, message, reference_doctype, reference_name):
|
||||
import requests
|
||||
|
||||
data = {"text": message, "attachments": []}
|
||||
|
||||
slack_url, show_link = frappe.db.get_value(
|
||||
|
|
|
|||
|
|
@ -5,11 +5,10 @@ import base64
|
|||
import hashlib
|
||||
import hmac
|
||||
import json
|
||||
import typing
|
||||
from time import sleep
|
||||
from urllib.parse import urlparse
|
||||
|
||||
import requests
|
||||
|
||||
import frappe
|
||||
from frappe import _
|
||||
from frappe.model.document import Document
|
||||
|
|
@ -18,6 +17,9 @@ from frappe.utils.safe_exec import get_safe_globals
|
|||
|
||||
WEBHOOK_SECRET_HEADER = "X-Frappe-Webhook-Signature"
|
||||
|
||||
if typing.TYPE_CHECKING:
|
||||
import requests
|
||||
|
||||
|
||||
class Webhook(Document):
|
||||
def validate(self):
|
||||
|
|
@ -112,6 +114,8 @@ def get_context(doc):
|
|||
|
||||
|
||||
def enqueue_webhook(doc, webhook) -> None:
|
||||
import requests
|
||||
|
||||
webhook: Webhook = frappe.get_doc("Webhook", webhook.get("name"))
|
||||
headers = get_webhook_headers(doc, webhook)
|
||||
data = get_webhook_data(doc, webhook)
|
||||
|
|
@ -154,7 +158,7 @@ def log_request(
|
|||
url: str,
|
||||
headers: dict,
|
||||
data: dict,
|
||||
res: requests.Response | None = None,
|
||||
res: typing.Optional["requests.Response"] = None,
|
||||
):
|
||||
request_log = frappe.get_doc(
|
||||
{
|
||||
|
|
|
|||
|
|
@ -2,7 +2,6 @@ import json
|
|||
|
||||
from google.oauth2.credentials import Credentials
|
||||
from googleapiclient.discovery import build
|
||||
from requests import get, post
|
||||
|
||||
import frappe
|
||||
from frappe.utils import get_request_site_address
|
||||
|
|
@ -56,6 +55,8 @@ class GoogleOAuth:
|
|||
frappe.throw(frappe._("Please update {} before continuing.").format(google_settings))
|
||||
|
||||
def authorize(self, oauth_code: str) -> dict[str, str | int]:
|
||||
import requests
|
||||
|
||||
"""Returns a dict with access and refresh token.
|
||||
|
||||
:param oauth_code: code got back from google upon successful auhtorization
|
||||
|
|
@ -73,13 +74,14 @@ class GoogleOAuth:
|
|||
}
|
||||
|
||||
return handle_response(
|
||||
post(self.OAUTH_URL, data=data).json(),
|
||||
requests.post(self.OAUTH_URL, data=data).json(),
|
||||
"Google Oauth Authorization Error",
|
||||
"Something went wrong during the authorization.",
|
||||
)
|
||||
|
||||
def refresh_access_token(self, refresh_token: str) -> dict[str, str | int]:
|
||||
"""Refreshes google access token using refresh token"""
|
||||
import requests
|
||||
|
||||
data = {
|
||||
"client_id": self.google_settings.client_id,
|
||||
|
|
@ -92,7 +94,7 @@ class GoogleOAuth:
|
|||
}
|
||||
|
||||
return handle_response(
|
||||
post(self.OAUTH_URL, data=data).json(),
|
||||
requests.post(self.OAUTH_URL, data=data).json(),
|
||||
"Google Oauth Access Token Refresh Error",
|
||||
"Something went wrong during the access token generation.",
|
||||
raise_err=True,
|
||||
|
|
@ -158,7 +160,9 @@ def handle_response(
|
|||
|
||||
|
||||
def is_valid_access_token(access_token: str) -> bool:
|
||||
response = get(
|
||||
import requests
|
||||
|
||||
response = requests.get(
|
||||
"https://oauth2.googleapis.com/tokeninfo", params={"access_token": access_token}
|
||||
).json()
|
||||
|
||||
|
|
|
|||
|
|
@ -4,8 +4,6 @@ import csv
|
|||
import json
|
||||
from io import StringIO
|
||||
|
||||
import requests
|
||||
|
||||
import frappe
|
||||
from frappe import _, msgprint
|
||||
from frappe.utils import cint, comma_or, cstr, flt
|
||||
|
|
@ -178,6 +176,8 @@ def getlink(doctype, name):
|
|||
|
||||
|
||||
def get_csv_content_from_google_sheets(url):
|
||||
import requests
|
||||
|
||||
# https://docs.google.com/spreadsheets/d/{sheetid}}/edit#gid={gid}
|
||||
validate_google_sheets_url(url)
|
||||
# get gid, defaults to first sheet
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue