Skip to content

Commit

Permalink
Split batch handler into individual functions to appease flake8 compl…
Browse files Browse the repository at this point in the history
…exity linter
  • Loading branch information
wosc committed Aug 7, 2024
1 parent d6016d0 commit 98d2c6e
Showing 1 changed file with 57 additions and 34 deletions.
91 changes: 57 additions & 34 deletions src/gcp_storage_emulator/handlers/objects.py
Original file line number Diff line number Diff line change
Expand Up @@ -565,40 +565,14 @@ def batch(request, response, storage, *args, **kwargs):
bucket_name = item.get("bucket_name")
object_id = item.get("object_id")
meta = item.get("meta")
if method == "PATCH":
resp_data = _patch(storage, bucket_name, object_id, meta)
if resp_data:
response.write("HTTP/1.1 200 OK\r\n")
response.write("Content-Type: application/json; charset=UTF-8\r\n")
response.write(json.dumps(resp_data))
response.write("\r\n\r\n")
if method == "DELETE":
if object_id:
resp_data = _delete(storage, bucket_name, object_id)
else:
try:
storage.delete_bucket(bucket_name)
resp_data = True
except (Conflict, NotFound):
pass
if resp_data:
response.write("HTTP/1.1 204 No Content\r\n")
response.write("Content-Type: application/json; charset=UTF-8\r\n")
if method == "POST": # kludgy heuristics, currently only supports COPY
if object_id:
resp_data = _copy(
request.base_url,
storage,
bucket_name,
object_id,
item["dest_bucket_name"],
item["dest_object_id"],
)
if resp_data:
response.write("HTTP/1.1 200 OK\r\n")
response.write("Content-Type: application/json; charset=UTF-8\r\n")
response.write(json.dumps(resp_data))
response.write("\r\n\r\n")

handler = f"_batch_{method.lower()}"
try:
handler = globals()[handler]
except KeyError:
pass
else:
resp_data = handler(request, item, storage, bucket_name, object_id, meta, response)

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

response.write("--{}--".format(boundary))


def _batch_patch(request, item, storage, bucket_name, object_id, meta, response):
resp_data = _patch(storage, bucket_name, object_id, meta)
if not resp_data:
return None
response.write("HTTP/1.1 200 OK\r\n")
response.write("Content-Type: application/json; charset=UTF-8\r\n")
response.write(json.dumps(resp_data))
response.write("\r\n\r\n")
return resp_data


def _batch_delete(request, item, storage, bucket_name, object_id, meta, response):
resp_data = None
if object_id:
resp_data = _delete(storage, bucket_name, object_id)
else:
try:
storage.delete_bucket(bucket_name)
resp_data = True
except (Conflict, NotFound):
pass
if not resp_data:
return None
response.write("HTTP/1.1 204 No Content\r\n")
response.write("Content-Type: application/json; charset=UTF-8\r\n")
return resp_data


# kludgy heuristics, currently only supports COPY
def _batch_post(request, item, storage, bucket_name, object_id, meta, response):
resp_data = None
if object_id:
resp_data = _copy(
request.base_url,
storage,
bucket_name,
object_id,
item["dest_bucket_name"],
item["dest_object_id"],
)
if not resp_data:
return None
response.write("HTTP/1.1 200 OK\r\n")
response.write("Content-Type: application/json; charset=UTF-8\r\n")
response.write(json.dumps(resp_data))
response.write("\r\n\r\n")
return resp_data

0 comments on commit 98d2c6e

Please sign in to comment.