Skip to content

Commit 6b35388

Browse files
committed
fix: avoid an inessential http call for the api as batch mode
1 parent 3303735 commit 6b35388

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

slo_generator/api/main.py

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,7 @@
2424
import os
2525
import pprint
2626

27-
import requests
28-
from flask import jsonify, make_response
27+
from flask import Request, jsonify, make_response
2928

3029
from slo_generator.compute import compute, export
3130
from slo_generator.utils import get_exporters, load_config, setup_logging
@@ -34,6 +33,7 @@
3433
LOGGER = logging.getLogger(__name__)
3534
TIME_FORMAT = "%Y-%m-%dT%H:%M:%SZ"
3635
API_SIGNATURE_TYPE = os.environ["GOOGLE_FUNCTION_SIGNATURE_TYPE"]
36+
BATCH_REQUEST_PARAMETER = "batch"
3737
setup_logging()
3838

3939

@@ -53,7 +53,7 @@ def run_compute(request):
5353

5454
# Process request
5555
data = process_req(request)
56-
batch_mode = request.args.get("batch", False)
56+
batch_mode = request.args.get(BATCH_REQUEST_PARAMETER, False)
5757
if batch_mode:
5858
if not API_SIGNATURE_TYPE == "http":
5959
raise ValueError(
@@ -203,11 +203,6 @@ def process_batch_req(request, data, config):
203203
"requests separately."
204204
)
205205
urls = data.split(";")
206-
service_url = request.base_url
207-
headers = {"User-Agent": "slo-generator"}
208-
if "Authorization" in request.headers:
209-
headers["Authorization"] = request.headers["Authorization"]
210-
service_url = service_url.replace("http:", "https:") # force HTTPS auth
211206
for url in urls:
212207
if "pubsub_batch_handler" in config:
213208
LOGGER.info(f"Sending {url} to pubsub batch handler.")
@@ -228,4 +223,8 @@ def process_batch_req(request, data, config):
228223
client.publish(topic_path, data=data).result()
229224
else: # http
230225
LOGGER.info(f"Sending {url} to HTTP batch handler.")
231-
requests.post(service_url, headers=headers, data=url, timeout=10)
226+
args = request.args.copy()
227+
args[BATCH_REQUEST_PARAMETER] = False
228+
req = Request.from_values(data=url)
229+
req.args = args
230+
run_compute(req)

0 commit comments

Comments
 (0)