mirror of
https://github.com/LukeHagar/comfy-deploy-python.git
synced 2025-12-06 20:37:48 +00:00
90 lines
4.3 KiB
Python
90 lines
4.3 KiB
Python
"""Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT."""
|
|
|
|
import requests as requests_http
|
|
from .sdkconfiguration import SDKConfiguration
|
|
from comfydeploy import utils
|
|
from comfydeploy._hooks import AfterErrorContext, AfterSuccessContext, BeforeRequestContext, HookContext
|
|
from comfydeploy.models import components, errors, operations
|
|
from typing import Optional
|
|
|
|
class Files:
|
|
sdk_configuration: SDKConfiguration
|
|
|
|
def __init__(self, sdk_config: SDKConfiguration) -> None:
|
|
self.sdk_configuration = sdk_config
|
|
|
|
|
|
|
|
def get_upload_url(self, type_: operations.Type, file_size: str) -> operations.GetUploadURLResponse:
|
|
r"""Upload any files to the storage
|
|
Usually when you run something, you want to upload a file, image etc.
|
|
"""
|
|
hook_ctx = HookContext(operation_id='get_/upload-url', oauth2_scopes=[], security_source=self.sdk_configuration.security)
|
|
request = operations.GetUploadURLRequest(
|
|
type=type_,
|
|
file_size=file_size,
|
|
)
|
|
|
|
base_url = utils.template_url(*self.sdk_configuration.get_server_details())
|
|
|
|
url = base_url + '/upload-url'
|
|
|
|
if callable(self.sdk_configuration.security):
|
|
headers, query_params = utils.get_security(self.sdk_configuration.security())
|
|
else:
|
|
headers, query_params = utils.get_security(self.sdk_configuration.security)
|
|
|
|
query_params = { **utils.get_query_params(request), **query_params }
|
|
headers['Accept'] = 'application/json'
|
|
headers['user-agent'] = self.sdk_configuration.user_agent
|
|
client = self.sdk_configuration.client
|
|
|
|
try:
|
|
req = client.prepare_request(requests_http.Request('GET', url, params=query_params, headers=headers))
|
|
req = self.sdk_configuration.get_hooks().before_request(BeforeRequestContext(hook_ctx), req)
|
|
http_res = client.send(req)
|
|
except Exception as e:
|
|
_, e = self.sdk_configuration.get_hooks().after_error(AfterErrorContext(hook_ctx), None, e)
|
|
if e is not None:
|
|
raise e
|
|
|
|
if utils.match_status_codes(['401','4XX','500','5XX'], http_res.status_code):
|
|
result, e = self.sdk_configuration.get_hooks().after_error(AfterErrorContext(hook_ctx), http_res, None)
|
|
if e is not None:
|
|
raise e
|
|
if result is not None:
|
|
http_res = result
|
|
else:
|
|
http_res = self.sdk_configuration.get_hooks().after_success(AfterSuccessContext(hook_ctx), http_res)
|
|
|
|
|
|
|
|
res = operations.GetUploadURLResponse(http_meta=components.HTTPMetadata(request=req, response=http_res))
|
|
|
|
if http_res.status_code == 200:
|
|
# pylint: disable=no-else-return
|
|
if utils.match_content_type(http_res.headers.get('Content-Type') or '', 'application/json'):
|
|
out = utils.unmarshal_json(http_res.text, Optional[operations.GetUploadURLResponseBody])
|
|
res.object = out
|
|
else:
|
|
content_type = http_res.headers.get('Content-Type')
|
|
raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
|
|
elif http_res.status_code == 401 or http_res.status_code >= 400 and http_res.status_code < 500 or http_res.status_code >= 500 and http_res.status_code < 600:
|
|
raise errors.SDKError('API error occurred', http_res.status_code, http_res.text, http_res)
|
|
elif http_res.status_code == 500:
|
|
# pylint: disable=no-else-return
|
|
if utils.match_content_type(http_res.headers.get('Content-Type') or '', 'application/json'):
|
|
out = utils.unmarshal_json(http_res.text, errors.GetUploadURLResponseBody, infer_missing=True)
|
|
out.http_meta = components.HTTPMetadata(request=req, response=http_res)
|
|
raise out
|
|
else:
|
|
content_type = http_res.headers.get('Content-Type')
|
|
raise errors.SDKError(f'unknown content-type received: {content_type}', http_res.status_code, http_res.text, http_res)
|
|
else:
|
|
raise errors.SDKError('unknown status code received', http_res.status_code, http_res.text, http_res)
|
|
|
|
return res
|
|
|
|
|
|
|