bbqdennisEX commited on
Commit
0ce5073
·
1 Parent(s): 29e8370

-- Added call_get_api and call_post_api to Common

Browse files
Files changed (2) hide show
  1. Common.py +24 -5
  2. app.py +1 -1
Common.py CHANGED
@@ -57,7 +57,19 @@ def read_json_file(file_path):
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
 
@@ -65,10 +77,17 @@ def call_api(url, data):
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}'
 
57
  print(f"An error occurred: {e}")
58
  return None
59
 
60
+ def call_api(url, data, is_post=True):
61
+ print(f"Calling API: {url} data: {data}")
62
+ headers = {'Content-Type': 'application/json'}
63
+
64
+ if is_post:
65
+ requests_method = requests.post
66
+ else:
67
+ requests_method = requests.get
68
+
69
+ response = requests_method(url, json=data, headers=headers)
70
+ return response.json()
71
+
72
+ def call_post_api(url, data):
73
  """
74
  Sends a POST request to the specified URL with the given data.
75
 
 
77
  :param data: A dictionary containing the data to be sent in the POST request
78
  :return: Response object from the POST request
79
  """
80
+ return call_api(url, data, is_post=True)
81
+
82
+ def call_get_api(url, data):
83
+ """
84
+ Sends a GET request to the specified URL with the given data.
85
+
86
+ :param url: The URL to which the GET request is sent
87
+ :param data: A dictionary containing the data to be sent in the GET request
88
+ :return: Response object from the GET request
89
+ """
90
+ return call_api(url, data, is_post=False)
91
 
92
  def update_google_sheet(api_key, spreadsheet_id, range_name, values):
93
  url = f'https://sheets.googleapis.com/v4/spreadsheets/{spreadsheet_id}/values/{range_name}?key={api_key}'
app.py CHANGED
@@ -11,4 +11,4 @@ def greet_json():
11
 
12
  @app.get("/heartbeat")
13
  def heartbeat_json():
14
- return Common.call_api(DEFALUT_N8N_HEARTBEAT_URL, {})
 
11
 
12
  @app.get("/heartbeat")
13
  def heartbeat_json():
14
+ return Common.call_get_api(DEFALUT_N8N_HEARTBEAT_URL, {})