fix(workers): correctly use multipart while uploading scripts (#2661)

* fix(workers): correctly use multipart while uploading scripts

* fix(examples): use correct json serialising method

---------

Co-authored-by: Robert Craigie <robert@craigie.dev>
This commit is contained in:
stainless-app[bot] 2025-06-03 19:22:09 +01:00 committed by GitHub
parent 420b061e23
commit 756dc87dde
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 21 additions and 2 deletions

View file

@ -25,7 +25,6 @@ Then, define a "dispatch_namespace_name" variable and add a
"""
import os
import json
from cloudflare import Cloudflare, BadRequestError
@ -88,7 +87,7 @@ def main() -> None:
},
)
print("Script Upload success!")
print(json.dumps(script, indent=2))
print(script.to_json(indent=2))
except BadRequestError as err:
print("Script Upload failure!")
print(err)

View file

@ -205,6 +205,10 @@ class ScriptsResource(SyncAPIResource):
raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
if not script_name:
raise ValueError(f"Expected a non-empty value for `script_name` but received {script_name!r}")
# It should be noted that the actual Content-Type header that will be
# sent to the server will contain a `boundary` parameter, e.g.
# multipart/form-data; boundary=---abc--
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
return self._put(
f"/accounts/{account_id}/workers/scripts/{script_name}",
body=maybe_transform({"metadata": metadata}, script_update_params.ScriptUpdateParams),
@ -214,6 +218,7 @@ class ScriptsResource(SyncAPIResource):
extra_query=extra_query,
extra_body=extra_body,
timeout=timeout,
multipart_syntax='json',
post_parser=ResultWrapper[ScriptUpdateResponse]._unwrapper,
),
cast_to=cast(Type[ScriptUpdateResponse], ResultWrapper[ScriptUpdateResponse]),
@ -449,6 +454,10 @@ class AsyncScriptsResource(AsyncAPIResource):
raise ValueError(f"Expected a non-empty value for `account_id` but received {account_id!r}")
if not script_name:
raise ValueError(f"Expected a non-empty value for `script_name` but received {script_name!r}")
# It should be noted that the actual Content-Type header that will be
# sent to the server will contain a `boundary` parameter, e.g.
# multipart/form-data; boundary=---abc--
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
return await self._put(
f"/accounts/{account_id}/workers/scripts/{script_name}",
body=await async_maybe_transform({"metadata": metadata}, script_update_params.ScriptUpdateParams),
@ -459,6 +468,7 @@ class AsyncScriptsResource(AsyncAPIResource):
extra_body=extra_body,
timeout=timeout,
post_parser=ResultWrapper[ScriptUpdateResponse]._unwrapper,
multipart_syntax='json',
),
cast_to=cast(Type[ScriptUpdateResponse], ResultWrapper[ScriptUpdateResponse]),
)

View file

@ -161,6 +161,10 @@ class ScriptsResource(SyncAPIResource):
raise ValueError(f"Expected a non-empty value for `dispatch_namespace` but received {dispatch_namespace!r}")
if not script_name:
raise ValueError(f"Expected a non-empty value for `script_name` but received {script_name!r}")
# It should be noted that the actual Content-Type header that will be
# sent to the server will contain a `boundary` parameter, e.g.
# multipart/form-data; boundary=---abc--
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
return self._put(
f"/accounts/{account_id}/workers/dispatch/namespaces/{dispatch_namespace}/scripts/{script_name}",
body=maybe_transform({"metadata": metadata}, script_update_params.ScriptUpdateParams),
@ -170,6 +174,7 @@ class ScriptsResource(SyncAPIResource):
extra_query=extra_query,
extra_body=extra_body,
timeout=timeout,
multipart_syntax='json',
post_parser=ResultWrapper[ScriptUpdateResponse]._unwrapper,
),
cast_to=cast(Type[ScriptUpdateResponse], ResultWrapper[ScriptUpdateResponse]),
@ -370,6 +375,10 @@ class AsyncScriptsResource(AsyncAPIResource):
raise ValueError(f"Expected a non-empty value for `dispatch_namespace` but received {dispatch_namespace!r}")
if not script_name:
raise ValueError(f"Expected a non-empty value for `script_name` but received {script_name!r}")
# It should be noted that the actual Content-Type header that will be
# sent to the server will contain a `boundary` parameter, e.g.
# multipart/form-data; boundary=---abc--
extra_headers = {"Content-Type": "multipart/form-data", **(extra_headers or {})}
return await self._put(
f"/accounts/{account_id}/workers/dispatch/namespaces/{dispatch_namespace}/scripts/{script_name}",
body=await async_maybe_transform({"metadata": metadata}, script_update_params.ScriptUpdateParams),
@ -379,6 +388,7 @@ class AsyncScriptsResource(AsyncAPIResource):
extra_query=extra_query,
extra_body=extra_body,
timeout=timeout,
multipart_syntax='json',
post_parser=ResultWrapper[ScriptUpdateResponse]._unwrapper,
),
cast_to=cast(Type[ScriptUpdateResponse], ResultWrapper[ScriptUpdateResponse]),