Skip to content

Commit 5080767

Browse files
committed
Split batch handler into individual functions to appease flake8 complexity linter
1 parent c7274e8 commit 5080767

File tree

1 file changed

+59
-34
lines changed

1 file changed

+59
-34
lines changed

src/gcp_storage_emulator/handlers/objects.py

Lines changed: 59 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -565,40 +565,16 @@ def batch(request, response, storage, *args, **kwargs):
565565
bucket_name = item.get("bucket_name")
566566
object_id = item.get("object_id")
567567
meta = item.get("meta")
568-
if method == "PATCH":
569-
resp_data = _patch(storage, bucket_name, object_id, meta)
570-
if resp_data:
571-
response.write("HTTP/1.1 200 OK\r\n")
572-
response.write("Content-Type: application/json; charset=UTF-8\r\n")
573-
response.write(json.dumps(resp_data))
574-
response.write("\r\n\r\n")
575-
if method == "DELETE":
576-
if object_id:
577-
resp_data = _delete(storage, bucket_name, object_id)
578-
else:
579-
try:
580-
storage.delete_bucket(bucket_name)
581-
resp_data = True
582-
except (Conflict, NotFound):
583-
pass
584-
if resp_data:
585-
response.write("HTTP/1.1 204 No Content\r\n")
586-
response.write("Content-Type: application/json; charset=UTF-8\r\n")
587-
if method == "POST": # kludgy heuristics, currently only supports COPY
588-
if object_id:
589-
resp_data = _copy(
590-
request.base_url,
591-
storage,
592-
bucket_name,
593-
object_id,
594-
item["dest_bucket_name"],
595-
item["dest_object_id"],
596-
)
597-
if resp_data:
598-
response.write("HTTP/1.1 200 OK\r\n")
599-
response.write("Content-Type: application/json; charset=UTF-8\r\n")
600-
response.write(json.dumps(resp_data))
601-
response.write("\r\n\r\n")
568+
569+
handler = f"_batch_{method.lower()}"
570+
try:
571+
handler = globals()[handler]
572+
except KeyError:
573+
pass
574+
else:
575+
resp_data = handler(
576+
request, item, storage, bucket_name, object_id, meta, response
577+
)
602578

603579
if not resp_data:
604580
msg = "No such object: {}/{}".format(bucket_name, object_id)
@@ -611,3 +587,52 @@ def batch(request, response, storage, *args, **kwargs):
611587
response.write("\r\n\r\n")
612588

613589
response.write("--{}--".format(boundary))
590+
591+
592+
def _batch_patch(request, item, storage, bucket_name, object_id, meta, response):
593+
resp_data = _patch(storage, bucket_name, object_id, meta)
594+
if not resp_data:
595+
return None
596+
response.write("HTTP/1.1 200 OK\r\n")
597+
response.write("Content-Type: application/json; charset=UTF-8\r\n")
598+
response.write(json.dumps(resp_data))
599+
response.write("\r\n\r\n")
600+
return resp_data
601+
602+
603+
def _batch_delete(request, item, storage, bucket_name, object_id, meta, response):
604+
resp_data = None
605+
if object_id:
606+
resp_data = _delete(storage, bucket_name, object_id)
607+
else:
608+
try:
609+
storage.delete_bucket(bucket_name)
610+
resp_data = True
611+
except (Conflict, NotFound):
612+
pass
613+
if not resp_data:
614+
return None
615+
response.write("HTTP/1.1 204 No Content\r\n")
616+
response.write("Content-Type: application/json; charset=UTF-8\r\n")
617+
return resp_data
618+
619+
620+
# kludgy heuristics, currently only supports COPY
621+
def _batch_post(request, item, storage, bucket_name, object_id, meta, response):
622+
resp_data = None
623+
if object_id:
624+
resp_data = _copy(
625+
request.base_url,
626+
storage,
627+
bucket_name,
628+
object_id,
629+
item["dest_bucket_name"],
630+
item["dest_object_id"],
631+
)
632+
if not resp_data:
633+
return None
634+
response.write("HTTP/1.1 200 OK\r\n")
635+
response.write("Content-Type: application/json; charset=UTF-8\r\n")
636+
response.write(json.dumps(resp_data))
637+
response.write("\r\n\r\n")
638+
return resp_data

0 commit comments

Comments
 (0)