(20241223) Exception handling in Quart API endpoints can now return stuff.

This commit is contained in:
2024-12-23 16:27:04 +05:30
parent 78ce884e87
commit 3296fec639
18 changed files with 1373 additions and 2 deletions
+202
View File
@@ -0,0 +1,202 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
Create: Saturday, 18th May, 2022
Update: Thursday, 22nd Aug. 2024
OBJECTIVE:
To provide an easy way to work with '.json' data and files.
REFERENCES:
1) https://www.w3schools.com/python/python_json.asp
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# System-level activities:
import io
# To work with the JSON standard:
import json
# To work with files:
from utils_v2.system import files
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
def from_string(json_data):
"""
Decodes a JSON string to a pythonic variable like a dict.
:param json_data: The JSON string to decode.
:return: The decoded pythonic variable.
"""
python_data = json.loads(json_data)
return python_data
# ---------------------------------------------------------------------------------------------------------------------
def to_string(
python_data,
indent = 4,
default = None,
separators = None,
no_space = False
):
"""
Converts the given pythonic data to a JSON string.
:param python_data: The input data like a dict.
:param indent: The tab-width for pretty presentation.
:param default: The function to use on something that cannot be directly parsed into a JSON string.
:param separators: Custom separators to use.
:param no_space: If you want a dense JSON string that saves memory by not using spaces or tabs or line-breaks. Not
good for human readability, very good for saving memory. WARNING: THIS OVERRIDES EVERY OTHER PARAMETER EXCEPT
'default'.
:return: The JSON string representation of the input pythonic data.
"""
if no_space:
json_data = json.dumps(
python_data,
default = default,
separators = (',', ':')
)
else:
json_data = json.dumps(
python_data,
indent = indent,
default = default,
separators = separators
)
return json_data
# ---------------------------------------------------------------------------------------------------------------------
def from_file(file):
"""
Reads a JSON file and returns it as a pythonic variable like a dict.
:param file: The path to the file on the disk or a file held in RAM as a BytesIO object.
:return: The decoded pythonic variable.
"""
if isinstance(file, io.BytesIO):
file.seek(0)
json_data = file.getvalue()
else: json_data = files.read_file(file)
python_data = from_string(json_data)
return python_data
# ---------------------------------------------------------------------------------------------------------------------
def to_file(
file,
python_data,
indent = 4,
default = None,
separators = None,
no_space = False
):
"""
:param file: Either a path to a file on disk, or a buffer in RAM in the form of a BytesIO object.
:param python_data: The pythonic data to be converted to the JSON string.
:param indent: The tab-width for pretty presentation.
:param default: The function to use on something that cannot be directly parsed into a JSON string.
:param separators: Custom separators to use.
:param no_space: If you want a dense JSON string that saves memory by not using spaces or tabs or line-breaks. Not
good for human readability, very good for saving memory. WARNING: THIS OVERRIDES EVERY OTHER PARAMETER EXCEPT
'default'.
:return: True/False if a path was given, else the same BytesIO object with the written JSON data.
"""
json_data = to_string(
python_data,
indent = indent,
default = default,
separators = separators,
no_space = no_space
)
if isinstance(file, io.BytesIO):
file.write(json_data.encode("utf-8"))
file.seek(0)
return file
else:
try:
files.write_file(file, json_data, mode = "w")
return True
except: return False
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
pass
+2 -2
View File
@@ -1255,8 +1255,8 @@ def handle_failed_request(cleanup_func = None, cleanup_coro = None):
except Exception as exception: except Exception as exception:
kwargs["decorator_count"] -= 1 kwargs["decorator_count"] -= 1
if hasattr(current_app, "printer"): getattr(current_app, "printer")(exception) if hasattr(current_app, "printer"): getattr(current_app, "printer")(exception)
if cleanup_func is not None: cleanup_func() if cleanup_func is not None: return cleanup_func()
if cleanup_coro is not None: await cleanup_coro() if cleanup_coro is not None: return await cleanup_coro()
raise exception raise exception
return wrapper return wrapper
View File
View File
@@ -0,0 +1,114 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authorization Cancelled</title>
<!-- Include Bootstrap CSS (make sure you have an internet connection) -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
/* General reset and basic styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: rgba(244, 241, 253, 1); /* Light purple */
background-image: radial-gradient(circle closest-side, rgba(0, 0, 0, 0.1) 1px, transparent 1px);
background-size: 20px 20px; /* Controls the size of the halftone dots */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 20px;
}
.message-container {
background-color: #fff; /* White background */
border-radius: 16px;
padding: 60px 35px 35px; /* Increased top padding to make space for the circle */
max-width: 400px;
text-align: center;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
color: #333; /* Dark text color */
position: relative; /* To position the circle above it */
}
h1 {
font-size: 24px;
color: #FF9800; /* Orange color for cancellation */
margin-bottom: 10px;
}
p {
font-size: 16px;
color: #132F41; /* Dark blue text */
margin-top: 20px; /* Space between the circle and paragraph */
}
/* Circle for exclamation icon */
.cancellation-circle {
width: 90px; /* Increased size by 50% */
height: 90px; /* Increased size by 50% */
border-radius: 50%; /* Makes it a circle */
background-color: #FF9800; /* Orange color for cancellation */
color: white;
font-size: 54px; /* Increased font size to fit inside the bigger circle */
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: -45px; /* Position the circle 45px above the container */
left: 50%;
transform: translateX(-50%); /* Center it horizontally */
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* Subtle shadow */
margin-bottom: 20px; /* Added margin to prevent overlap with the text */
}
/* Close button */
.btn-secondary {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* Subtle shadow for the button */
margin-top: 10px;
}
/* Responsive styles */
@media (max-width: 600px) {
.message-container {
padding: 45px 20px 35px; /* Adjust padding for small screens */
}
h1 {
font-size: 20px;
}
p {
font-size: 14px;
}
.cancellation-circle {
width: 75px;
height: 75px;
font-size: 45px;
top: -40px; /* Adjust top position for smaller screens */
}
}
</style>
</head>
<body>
<div class="message-container">
<div class="cancellation-circle"><b>!</b></div> <!-- Exclamation mark icon inside the circle -->
<h1><b>Authorization Cancelled</b></h1>
<p>It seems that the authorization for your <b>{{ mail_client }}</b> account was cancelled unexpectedly.
Please feel free to try again whenever you feel like it. You can close this tab at any time.</p>
<!-- Close button -->
<button class="btn btn-secondary" onclick="window.close();">Close</button>
</div>
<!-- Include Bootstrap JS (Optional for added interactivity, not needed for close functionality) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
@@ -0,0 +1,115 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authorization Failed</title>
<!-- Include Bootstrap CSS (make sure you have an internet connection) -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
/* General reset and basic styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: rgba(244, 241, 253, 1); /* Light purple */
background-image: radial-gradient(circle closest-side, rgba(0, 0, 0, 0.1) 1px, transparent 1px);
background-size: 20px 20px; /* Controls the size of the halftone dots */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 20px;
}
.message-container {
background-color: #fff; /* White background */
border-radius: 16px;
padding: 60px 35px 35px; /* Increased top padding to make space for the circle */
max-width: 400px;
text-align: center;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
color: #333; /* Dark text color */
position: relative; /* To position the circle above it */
}
h1 {
font-size: 24px;
color: #F44336; /* Red color for failure */
margin-bottom: 10px;
}
p {
font-size: 16px;
color: #132F41; /* Dark blue text */
margin-top: 20px; /* Space between the circle and paragraph */
}
/* Circle for failure icon */
.failure-circle {
width: 90px; /* Increased size by 50% */
height: 90px; /* Increased size by 50% */
border-radius: 50%; /* Makes it a circle */
background-color: #F44336; /* Red color */
color: white;
font-size: 54px; /* Increased font size to fit inside the bigger circle */
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: -45px; /* Position the circle 45px above the container */
left: 50%;
transform: translateX(-50%); /* Center it horizontally */
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* Subtle shadow */
margin-bottom: 20px; /* Added margin to prevent overlap with the text */
}
/* Close button */
.btn-secondary {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* Subtle shadow for the button */
margin-top: 10px;
}
/* Responsive styles */
@media (max-width: 600px) {
.message-container {
padding: 45px 20px 35px; /* Adjust padding for small screens */
}
h1 {
font-size: 20px;
}
p {
font-size: 14px;
}
.failure-circle {
width: 75px;
height: 75px;
font-size: 45px;
top: -40px; /* Adjust top position for smaller screens */
}
}
</style>
</head>
<body>
<div class="message-container">
<div class="failure-circle"></div> <!-- Red circle with failure icon -->
<h1><b>Authorization Failed</b></h1>
<p>Something went wrong in getting authorization from your <b>{{ mail_client }}</b> account.
<br><br><b>Hint:</b> {{ failure_hint }}<br><br>
Please feel free to try the same steps again. You can close this tab at any time.</p>
<!-- Close button -->
<button class="btn btn-secondary" onclick="window.close();">Close</button>
</div>
<!-- Include Bootstrap JS (Optional for added interactivity, not needed for close functionality) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
@@ -0,0 +1,113 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authorization Successful</title>
<!-- Include Bootstrap CSS (make sure you have an internet connection) -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
/* General reset and basic styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: rgba(244, 241, 253, 1); /* Light purple */
background-image: radial-gradient(circle closest-side, rgba(0, 0, 0, 0.1) 1px, transparent 1px);
background-size: 20px 20px; /* Controls the size of the halftone dots */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 20px;
}
.message-container {
background-color: #fff; /* White background */
border-radius: 16px;
padding: 60px 35px 35px; /* Increased top padding to make space for the circle */
max-width: 400px;
text-align: center;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
color: #333; /* Dark text color */
position: relative; /* To position the circle above it */
}
h1 {
font-size: 24px;
color: #4CAF50; /* Green color for failure */
margin-bottom: 10px;
}
p {
font-size: 16px;
color: #132F41; /* Dark blue text */
margin-top: 20px; /* Space between the circle and paragraph */
}
/* Circle for failure icon */
.success-circle {
width: 90px; /* Increased size by 50% */
height: 90px; /* Increased size by 50% */
border-radius: 50%; /* Makes it a circle */
background-color: #4CAF50; /* Green color */
color: white;
font-size: 54px; /* Increased font size to fit inside the bigger circle */
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: -45px; /* Position the circle 45px above the container */
left: 50%;
transform: translateX(-50%); /* Center it horizontally */
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* Subtle shadow */
margin-bottom: 20px; /* Added margin to prevent overlap with the text */
}
/* Close button */
.btn-secondary {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* Subtle shadow for the button */
margin-top: 10px;
}
/* Responsive styles */
@media (max-width: 600px) {
.message-container {
padding: 45px 20px 35px; /* Adjust padding for small screens */
}
h1 {
font-size: 20px;
}
p {
font-size: 14px;
}
.failure-circle {
width: 75px;
height: 75px;
font-size: 45px;
top: -40px; /* Adjust top position for smaller screens */
}
}
</style>
</head>
<body>
<div class="message-container">
<div class="success-circle"></div> <!-- Red circle with failure icon -->
<h1><b>Authorization Successful</b></h1>
<p>We have received authorization from your <b>{{ mail_client }}</b> account. You can close this tab at any time.</p>
<!-- Close button -->
<button class="btn btn-secondary" onclick="window.close();">Close</button>
</div>
<!-- Include Bootstrap JS (Optional for added interactivity, not needed for close functionality) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
View File
View File
@@ -0,0 +1,114 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authorization Cancelled</title>
<!-- Include Bootstrap CSS (make sure you have an internet connection) -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
/* General reset and basic styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: rgba(244, 241, 253, 1); /* Light purple */
background-image: radial-gradient(circle closest-side, rgba(0, 0, 0, 0.1) 1px, transparent 1px);
background-size: 20px 20px; /* Controls the size of the halftone dots */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 20px;
}
.message-container {
background-color: #fff; /* White background */
border-radius: 16px;
padding: 60px 35px 35px; /* Increased top padding to make space for the circle */
max-width: 400px;
text-align: center;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
color: #333; /* Dark text color */
position: relative; /* To position the circle above it */
}
h1 {
font-size: 24px;
color: #FF9800; /* Orange color for cancellation */
margin-bottom: 10px;
}
p {
font-size: 16px;
color: #132F41; /* Dark blue text */
margin-top: 20px; /* Space between the circle and paragraph */
}
/* Circle for exclamation icon */
.cancellation-circle {
width: 90px; /* Increased size by 50% */
height: 90px; /* Increased size by 50% */
border-radius: 50%; /* Makes it a circle */
background-color: #FF9800; /* Orange color for cancellation */
color: white;
font-size: 54px; /* Increased font size to fit inside the bigger circle */
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: -45px; /* Position the circle 45px above the container */
left: 50%;
transform: translateX(-50%); /* Center it horizontally */
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* Subtle shadow */
margin-bottom: 20px; /* Added margin to prevent overlap with the text */
}
/* Close button */
.btn-secondary {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* Subtle shadow for the button */
margin-top: 10px;
}
/* Responsive styles */
@media (max-width: 600px) {
.message-container {
padding: 45px 20px 35px; /* Adjust padding for small screens */
}
h1 {
font-size: 20px;
}
p {
font-size: 14px;
}
.cancellation-circle {
width: 75px;
height: 75px;
font-size: 45px;
top: -40px; /* Adjust top position for smaller screens */
}
}
</style>
</head>
<body>
<div class="message-container">
<div class="cancellation-circle"><b>!</b></div> <!-- Exclamation mark icon inside the circle -->
<h1><b>Authorization Cancelled</b></h1>
<p>It seems that the authorization for your <b>{{ client }}</b> account was cancelled unexpectedly.
Please feel free to try again whenever you feel like it. You can close this tab at any time.</p>
<!-- Close button -->
<button class="btn btn-secondary" onclick="window.close();">Close</button>
</div>
<!-- Include Bootstrap JS (Optional for added interactivity, not needed for close functionality) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
@@ -0,0 +1,115 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authorization Failed</title>
<!-- Include Bootstrap CSS (make sure you have an internet connection) -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
/* General reset and basic styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: rgba(244, 241, 253, 1); /* Light purple */
background-image: radial-gradient(circle closest-side, rgba(0, 0, 0, 0.1) 1px, transparent 1px);
background-size: 20px 20px; /* Controls the size of the halftone dots */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 20px;
}
.message-container {
background-color: #fff; /* White background */
border-radius: 16px;
padding: 60px 35px 35px; /* Increased top padding to make space for the circle */
max-width: 400px;
text-align: center;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
color: #333; /* Dark text color */
position: relative; /* To position the circle above it */
}
h1 {
font-size: 24px;
color: #F44336; /* Red color for failure */
margin-bottom: 10px;
}
p {
font-size: 16px;
color: #132F41; /* Dark blue text */
margin-top: 20px; /* Space between the circle and paragraph */
}
/* Circle for failure icon */
.failure-circle {
width: 90px; /* Increased size by 50% */
height: 90px; /* Increased size by 50% */
border-radius: 50%; /* Makes it a circle */
background-color: #F44336; /* Red color */
color: white;
font-size: 54px; /* Increased font size to fit inside the bigger circle */
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: -45px; /* Position the circle 45px above the container */
left: 50%;
transform: translateX(-50%); /* Center it horizontally */
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* Subtle shadow */
margin-bottom: 20px; /* Added margin to prevent overlap with the text */
}
/* Close button */
.btn-secondary {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* Subtle shadow for the button */
margin-top: 10px;
}
/* Responsive styles */
@media (max-width: 600px) {
.message-container {
padding: 45px 20px 35px; /* Adjust padding for small screens */
}
h1 {
font-size: 20px;
}
p {
font-size: 14px;
}
.failure-circle {
width: 75px;
height: 75px;
font-size: 45px;
top: -40px; /* Adjust top position for smaller screens */
}
}
</style>
</head>
<body>
<div class="message-container">
<div class="failure-circle"></div> <!-- Red circle with failure icon -->
<h1><b>Authorization Failed</b></h1>
<p>Something went wrong in getting authorization from your <b>{{ client }}</b> account.
<br><br><b>Hint:</b> {{ failure_hint|safe }}<br><br>
Please feel free to try the same steps again. You can close this tab at any time.</p>
<!-- Close button -->
<button class="btn btn-secondary" onclick="window.close();">Close</button>
</div>
<!-- Include Bootstrap JS (Optional for added interactivity, not needed for close functionality) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
@@ -0,0 +1,113 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Authorization Successful</title>
<!-- Include Bootstrap CSS (make sure you have an internet connection) -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet">
<style>
/* General reset and basic styles */
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Arial', sans-serif;
background-color: rgba(244, 241, 253, 1); /* Light purple */
background-image: radial-gradient(circle closest-side, rgba(0, 0, 0, 0.1) 1px, transparent 1px);
background-size: 20px 20px; /* Controls the size of the halftone dots */
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
padding: 20px;
}
.message-container {
background-color: #fff; /* White background */
border-radius: 16px;
padding: 60px 35px 35px; /* Increased top padding to make space for the circle */
max-width: 400px;
text-align: center;
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1);
color: #333; /* Dark text color */
position: relative; /* To position the circle above it */
}
h1 {
font-size: 24px;
color: #4CAF50; /* Green color for failure */
margin-bottom: 10px;
}
p {
font-size: 16px;
color: #132F41; /* Dark blue text */
margin-top: 20px; /* Space between the circle and paragraph */
}
/* Circle for failure icon */
.success-circle {
width: 90px; /* Increased size by 50% */
height: 90px; /* Increased size by 50% */
border-radius: 50%; /* Makes it a circle */
background-color: #4CAF50; /* Green color */
color: white;
font-size: 54px; /* Increased font size to fit inside the bigger circle */
display: flex;
justify-content: center;
align-items: center;
position: absolute;
top: -45px; /* Position the circle 45px above the container */
left: 50%;
transform: translateX(-50%); /* Center it horizontally */
box-shadow: 0 4px 10px rgba(0, 0, 0, 0.1); /* Subtle shadow */
margin-bottom: 20px; /* Added margin to prevent overlap with the text */
}
/* Close button */
.btn-secondary {
box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1); /* Subtle shadow for the button */
margin-top: 10px;
}
/* Responsive styles */
@media (max-width: 600px) {
.message-container {
padding: 45px 20px 35px; /* Adjust padding for small screens */
}
h1 {
font-size: 20px;
}
p {
font-size: 14px;
}
.failure-circle {
width: 75px;
height: 75px;
font-size: 45px;
top: -40px; /* Adjust top position for smaller screens */
}
}
</style>
</head>
<body>
<div class="message-container">
<div class="success-circle"></div> <!-- Red circle with failure icon -->
<h1><b>Authorization Successful</b></h1>
<p>We have received authorization from your <b>{{ client }}</b> account. You can close this tab at any time.</p>
<!-- Close button -->
<button class="btn btn-secondary" onclick="window.close();">Close</button>
</div>
<!-- Include Bootstrap JS (Optional for added interactivity, not needed for close functionality) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>
+140
View File
@@ -0,0 +1,140 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
monday, 23rd Dec., 2024
OBJECTIVE:
To provide a SocketIO app for socket-base communication with the front-end.
REFERENCES:
01. YouTube: https://www.youtube.com/watch?v=H1eLJMC5oTg&t=3s
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# System-level activities:
import io
import os
# my utils:
from utils_v2.string import json
# To work with SocketIO:
import socketio
from aiohttp import web
# For asynchronous activities:
import asyncio
# for debugging:
from icecream import IceCreamDebugger
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# The SocketIo server:
sio = socketio.AsyncServer(cors_allowed_origins = "*")
app = web.Application()
sio.attach(app)
# Debugging:
printer = IceCreamDebugger(prefix = "SocketIO | ", includeContext = True)
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
@sio.event
async def connect(sid, environ):
printer(sid)
# ---------------------------------------------------------------------------------------------------------------------
@sio.event
async def disconnect(sid):
printer(sid)
# ---------------------------------------------------------------------------------------------------------------------
async def init():
"""
Initialize stuff here.
:return: ?
"""
pass
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
async def main():
# Start receiving live market data in the background:
# asyncio.create_task(start_live_feed())
# Run the web server:
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, "0.0.0.0", 5214)
printer("Server running.")
await site.start()
# Keep the server running:
while True: await asyncio.sleep(3_600)
# Let's go:
asyncio.run(main())
+140
View File
@@ -0,0 +1,140 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
monday, 23rd Dec., 2024
OBJECTIVE:
To provide a SocketIO app for socket-base communication with the front-end.
REFERENCES:
01. YouTube: https://www.youtube.com/watch?v=H1eLJMC5oTg&t=3s
DOWNLOADS:
N/A
"""
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# System-level activities:
import io
import os
# my utils:
from utils_v2.string import json
# To work with SocketIO:
import socketio
from aiohttp import web
# For asynchronous activities:
import asyncio
# for debugging:
from icecream import IceCreamDebugger
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
# --- Nothing Yet
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# The SocketIo server:
sio = socketio.AsyncServer(cors_allowed_origins = "*")
app = web.Application()
sio.attach(app)
# Debugging:
printer = IceCreamDebugger(prefix = "SocketIO | ", includeContext = True)
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
@sio.event
async def connect(sid, environ):
printer(sid)
# ---------------------------------------------------------------------------------------------------------------------
@sio.event
async def disconnect(sid):
printer(sid)
# ---------------------------------------------------------------------------------------------------------------------
async def init():
"""
Initialize stuff here.
:return: ?
"""
pass
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
async def main():
# Start receiving live market data in the background:
# asyncio.create_task(start_live_feed())
# Run the web server:
runner = web.AppRunner(app)
await runner.setup()
site = web.TCPSite(runner, "0.0.0.0", 5214)
printer("Server running.")
await site.start()
# Keep the server running:
while True: await asyncio.sleep(3_600)
# Let's go:
asyncio.run(main())
+205
View File
@@ -0,0 +1,205 @@
"""
AUTHOR:
Khushal P Soonderji
DATE:
monday, 23rd Dec., 2024
OBJECTIVE:
To provide a SocketIO app for socket-base communication with the front-end.
REFERENCES:
01. YouTube: https://www.youtube.com/watch?v=H1eLJMC5oTg&t=3s
DOWNLOADS:
N/A
"""
import datetime
# *****************************************************************************************************************
# ***** ****
# *** IMPORT ***
# ***** ****
# *****************************************************************************************************************
# To make sibling directories accessible for imports:
import sys
sys.path.append(".")
sys.path.append("..")
# System-level activities:
import io
import os
# my utils:
from utils_v2.string import json
# To work with SocketIO:
import socketio
import eventlet
# For asynchronous activities:
import asyncio
# for debugging:
from icecream import IceCreamDebugger
# To work with date and time:
import time
# To work with Zerodha's Kite platform:
from kiteconnect import KiteConnect, KiteTicker
# *****************************************************************************************************************
# ***** ****
# *** MACROS / ONE-TIME INIT ***
# ***** ****
# *****************************************************************************************************************
INSTRUMENT_MAP = {
256265: "NIFTY 50",
260617: "NIFTY 100",
259849: "NIFTY IT",
341249: "HDFCBANK",
738561: "RELIANCE",
408065: "INFY",
2953217: "TCS",
356865: "HINDUNILVR",
1270529: "ICICIBANK",
492033: "KOTAKBANK",
110630919: "GOLD25JAN75800CE",
110050823: "SILVER25FEB76000CE",
10670594: "NIFTY24DEC23650PE",
17167874: "BANKNIFTY24DEC45000PE",
}
INSTRUMENT_TOKENS = list(INSTRUMENT_MAP.keys())
# *****************************************************************************************************************
# ***** ****
# *** VARIABLES ***
# ***** ****
# *****************************************************************************************************************
# The SocketIo server:
sio = socketio.Server(cors_allowed_origins = "*")
app = socketio.WSGIApp(sio)
# Debugging:
printer = IceCreamDebugger(prefix = "SocketIO | ", includeContext = True)
# *****************************************************************************************************************
# ***** ****
# *** FUNCTIONS ***
# ***** ****
# *****************************************************************************************************************
@sio.event
def connect(sid, environ):
printer(sid)
# ---------------------------------------------------------------------------------------------------------------------
@sio.event
def disconnect(sid):
printer(sid)
# ---------------------------------------------------------------------------------------------------------------------
def on_ticks(ws, ticks):
try:
# print(json.to_string(ticks[0], default=str))
printer(len(ticks))
now = datetime.datetime.now()
for t in ticks:
t["last_trade_time"] = t.get("last_trade_time", now).strftime("%Y-%m-%d %H:%M:%S")
t["exchange_timestamp"] = t.get("exchange_timestamp", now).strftime("%Y-%m-%d %H:%M:%S")
sio.emit("ticks", ticks)
sio.emit("ticks", {"name": "Bhopli"})
sio.emit("debug", {"name": "Debugger Bhopli"})
except Exception as exception:
printer(exception)
# ---------------------------------------------------------------------------------------------------------------------
def on_connect(ws, response):
ws.subscribe(INSTRUMENT_TOKENS)
ws.set_mode(ws.MODE_FULL, INSTRUMENT_TOKENS)
printer("Subscribed to token(s) in 'Full' mode", len(INSTRUMENT_TOKENS))
# ---------------------------------------------------------------------------------------------------------------------
def start_live_feed_input(
api_key: str,
access_token: str,
):
kite_ws = KiteTicker(
api_key = api_key,
access_token = access_token
)
# Assign the callbacks:
kite_ws.on_ticks = on_ticks
# kite_ws.on_close = on_close
# kite_ws.on_error = on_error
kite_ws.on_connect = on_connect
# kite_ws.on_reconnect = on_reconnect
# kite_ws.on_noreconnect = on_noreconnect
# If you choose to go threaded, you will need to work purely with callbacks.
# You will need to have an infinite loop in the main thread.
kite_ws.connect(threaded = True)
# ---------------------------------------------------------------------------------------------------------------------
@sio.event
def subscribe(sid, data):
printer(data)
sio.emit("echo", data)
# *****************************************************************************************************************
# ***** ****
# *** MAIN PROGRAM ***
# ***** ****
# *****************************************************************************************************************
if __name__ == "__main__":
# Connect to Zerodha:
creds = json.from_file(r"../creds/zerodha/api.json")
start_live_feed_input(
api_key = creds["apiKey"],
access_token = creds["accessToken"]
)
eventlet.wsgi.server(eventlet.listen(("0.0.0.0", 5214)), app)