Spaces:
Paused
Paused
Commit ·
392ec31
1
Parent(s): d2a417a
-- Added heartbeat api call
Browse files
Common.py
ADDED
|
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import re
|
| 3 |
+
import json
|
| 4 |
+
import requests
|
| 5 |
+
from pathlib import Path
|
| 6 |
+
|
| 7 |
+
# Define Constants
|
| 8 |
+
IS_DEBUG = False
|
| 9 |
+
|
| 10 |
+
# Helper Functions
|
| 11 |
+
def debug_print(message):
|
| 12 |
+
if IS_DEBUG:
|
| 13 |
+
print(message)
|
| 14 |
+
|
| 15 |
+
# Public Functions
|
| 16 |
+
def to_camel_case(snake_str):
|
| 17 |
+
components = snake_str.split('_')
|
| 18 |
+
return ''.join(x.capitalize() for x in components)
|
| 19 |
+
|
| 20 |
+
def camel_to_snake(camel_case_string):
|
| 21 |
+
# 使用正则表达式将驼峰命名法转换为下划线命名法
|
| 22 |
+
snake_case_string = re.sub(r'(?<!^)(?=[A-Z])', '_', camel_case_string).lower()
|
| 23 |
+
return snake_case_string
|
| 24 |
+
|
| 25 |
+
def extract_class_name(file_relative_path):
|
| 26 |
+
file_name = os.path.basename(file_relative_path)
|
| 27 |
+
class_name = os.path.splitext(file_name)[0]
|
| 28 |
+
return to_camel_case(class_name)
|
| 29 |
+
|
| 30 |
+
def safe_get_value(dictionary, key):
|
| 31 |
+
value = None
|
| 32 |
+
if key in dictionary:
|
| 33 |
+
value = dictionary[key]
|
| 34 |
+
return value
|
| 35 |
+
|
| 36 |
+
def read_json_file(file_path):
|
| 37 |
+
"""
|
| 38 |
+
读取指定路径的 JSON 文件,并返回解析后的数据。
|
| 39 |
+
|
| 40 |
+
:param file_path: JSON 文件的路径
|
| 41 |
+
:return: 解析后的 JSON 数据
|
| 42 |
+
"""
|
| 43 |
+
try:
|
| 44 |
+
# 使用 pathlib 处理文件路径
|
| 45 |
+
path = Path(file_path)
|
| 46 |
+
|
| 47 |
+
# 确保文件存在
|
| 48 |
+
if not path.is_file():
|
| 49 |
+
raise FileNotFoundError(f"File not found: {file_path}")
|
| 50 |
+
|
| 51 |
+
# 读取文件内容
|
| 52 |
+
with path.open('r', encoding='utf-8') as file:
|
| 53 |
+
data = json.load(file)
|
| 54 |
+
|
| 55 |
+
return data
|
| 56 |
+
except Exception as e:
|
| 57 |
+
print(f"An error occurred: {e}")
|
| 58 |
+
return None
|
| 59 |
+
|
| 60 |
+
def call_api(url, data):
|
| 61 |
+
"""
|
| 62 |
+
Sends a POST request to the specified URL with the given data.
|
| 63 |
+
|
| 64 |
+
:param url: The URL to which the POST request is sent
|
| 65 |
+
:param data: A dictionary containing the data to be sent in the POST request
|
| 66 |
+
:return: Response object from the POST request
|
| 67 |
+
"""
|
| 68 |
+
print(f"Calling API: {url} data: {data}")
|
| 69 |
+
headers = {'Content-Type': 'application/json'}
|
| 70 |
+
response = requests.post(url, json=data, headers=headers)
|
| 71 |
+
return response.json()
|
| 72 |
+
|
| 73 |
+
def update_google_sheet(api_key, spreadsheet_id, range_name, values):
|
| 74 |
+
url = f'https://sheets.googleapis.com/v4/spreadsheets/{spreadsheet_id}/values/{range_name}?key={api_key}'
|
| 75 |
+
headers = {
|
| 76 |
+
'Content-Type': 'application/json'
|
| 77 |
+
}
|
| 78 |
+
params = {
|
| 79 |
+
'valueInputOption': 'RAW'
|
| 80 |
+
}
|
| 81 |
+
data = {
|
| 82 |
+
'range': range_name,
|
| 83 |
+
'majorDimension': 'ROWS',
|
| 84 |
+
'values': values
|
| 85 |
+
}
|
| 86 |
+
|
| 87 |
+
response = requests.put(url, headers=headers, params=params, data=json.dumps(data))
|
| 88 |
+
|
| 89 |
+
if response.status_code == 200:
|
| 90 |
+
print('Update successful')
|
| 91 |
+
else:
|
| 92 |
+
print(f'Failed to update sheet: {response.status_code}, {response.text}')
|
app.py
CHANGED
|
@@ -1,15 +1,15 @@
|
|
|
|
|
| 1 |
from fastapi import FastAPI
|
| 2 |
|
|
|
|
|
|
|
| 3 |
app = FastAPI()
|
| 4 |
|
| 5 |
@app.get("/")
|
| 6 |
def greet_json():
|
| 7 |
return {"Hello": "World!"}
|
| 8 |
|
| 9 |
-
@app.get("/
|
| 10 |
-
def
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
@app.get("/new")
|
| 14 |
-
def new_json():
|
| 15 |
-
return {"New": "New Value"}
|
|
|
|
| 1 |
+
import Common
|
| 2 |
from fastapi import FastAPI
|
| 3 |
|
| 4 |
+
DEFALUT_N8N_HEARTBEAT_URL = "https://bbqdennisex-n8n-free.hf.space/webhook/heartbeat"
|
| 5 |
+
|
| 6 |
app = FastAPI()
|
| 7 |
|
| 8 |
@app.get("/")
|
| 9 |
def greet_json():
|
| 10 |
return {"Hello": "World!"}
|
| 11 |
|
| 12 |
+
@app.get("/heartbeat")
|
| 13 |
+
def heartbeat_json():
|
| 14 |
+
Common.call_api(DEFALUT_N8N_HEARTBEAT_URL, {})
|
| 15 |
+
return {"status": "success"}
|
|
|
|
|
|
|
|
|