"""Code generated by Speakeasy (https://speakeasyapi.dev). DO NOT EDIT."""

from .basesdk import BaseSDK
from dub._hooks import HookContext
from dub.models import components, errors, operations
from dub.types import BaseModel
import dub.utils as utils
from typing import Optional, Union

class Workspaces(BaseSDK):
    
    
    def get(
        self, *,
        request: Union[operations.GetWorkspaceRequest, operations.GetWorkspaceRequestTypedDict],
        server_url: Optional[str] = None,
    ) -> components.WorkspaceSchema:
        r"""Retrieve a workspace

        Retrieve a workspace for the authenticated user.

        :param request: The request object to send.
        :param server_url: Override the default server URL for this method
        """
        base_url = None
        url_variables = None
        if server_url is not None:
            base_url = server_url
        
        if not isinstance(request, BaseModel):
            request = utils.unmarshal(request, operations.GetWorkspaceRequest)
        
        req = self.build_request(
            method="GET",
            path="/workspaces/{idOrSlug}",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=False,
            request_has_path_params=True,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            security=self.sdk_configuration.security,
        )
        
        http_res = self.do_request(
            hook_ctx=HookContext(operation_id="getWorkspace", oauth2_scopes=[], security_source=self.sdk_configuration.security),
            request=req,
            error_status_codes=["400","401","403","404","409","410","422","429","4XX","500","5XX"],
        )
        
        
        if utils.match_response(http_res, "200", "application/json"):
            return utils.unmarshal_json(http_res.text, Optional[components.WorkspaceSchema])
        if utils.match_response(http_res, "400", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.BadRequestData)
            raise errors.BadRequest(data=data)
        if utils.match_response(http_res, "401", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.UnauthorizedData)
            raise errors.Unauthorized(data=data)
        if utils.match_response(http_res, "403", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.ForbiddenData)
            raise errors.Forbidden(data=data)
        if utils.match_response(http_res, "404", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.NotFoundData)
            raise errors.NotFound(data=data)
        if utils.match_response(http_res, "409", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.ConflictData)
            raise errors.Conflict(data=data)
        if utils.match_response(http_res, "410", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.InviteExpiredData)
            raise errors.InviteExpired(data=data)
        if utils.match_response(http_res, "422", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.UnprocessableEntityData)
            raise errors.UnprocessableEntity(data=data)
        if utils.match_response(http_res, "429", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.RateLimitExceededData)
            raise errors.RateLimitExceeded(data=data)
        if utils.match_response(http_res, "500", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.InternalServerErrorData)
            raise errors.InternalServerError(data=data)
        if utils.match_response(http_res, ["4XX","5XX"], "*"):
            raise errors.SDKError("API error occurred", http_res.status_code, http_res.text, http_res)
        
        content_type = http_res.headers.get("Content-Type")
        raise errors.SDKError(f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", http_res.status_code, http_res.text, http_res)
    
    
    async def get_async(
        self, *,
        request: Union[operations.GetWorkspaceRequest, operations.GetWorkspaceRequestTypedDict],
        server_url: Optional[str] = None,
    ) -> components.WorkspaceSchema:
        r"""Retrieve a workspace

        Retrieve a workspace for the authenticated user.

        :param request: The request object to send.
        :param server_url: Override the default server URL for this method
        """
        base_url = None
        url_variables = None
        if server_url is not None:
            base_url = server_url
        
        if not isinstance(request, BaseModel):
            request = utils.unmarshal(request, operations.GetWorkspaceRequest)
        
        req = self.build_request(
            method="GET",
            path="/workspaces/{idOrSlug}",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=False,
            request_has_path_params=True,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            security=self.sdk_configuration.security,
        )
        
        http_res = await self.do_request_async(
            hook_ctx=HookContext(operation_id="getWorkspace", oauth2_scopes=[], security_source=self.sdk_configuration.security),
            request=req,
            error_status_codes=["400","401","403","404","409","410","422","429","4XX","500","5XX"],
        )
        
        
        if utils.match_response(http_res, "200", "application/json"):
            return utils.unmarshal_json(http_res.text, Optional[components.WorkspaceSchema])
        if utils.match_response(http_res, "400", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.BadRequestData)
            raise errors.BadRequest(data=data)
        if utils.match_response(http_res, "401", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.UnauthorizedData)
            raise errors.Unauthorized(data=data)
        if utils.match_response(http_res, "403", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.ForbiddenData)
            raise errors.Forbidden(data=data)
        if utils.match_response(http_res, "404", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.NotFoundData)
            raise errors.NotFound(data=data)
        if utils.match_response(http_res, "409", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.ConflictData)
            raise errors.Conflict(data=data)
        if utils.match_response(http_res, "410", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.InviteExpiredData)
            raise errors.InviteExpired(data=data)
        if utils.match_response(http_res, "422", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.UnprocessableEntityData)
            raise errors.UnprocessableEntity(data=data)
        if utils.match_response(http_res, "429", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.RateLimitExceededData)
            raise errors.RateLimitExceeded(data=data)
        if utils.match_response(http_res, "500", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.InternalServerErrorData)
            raise errors.InternalServerError(data=data)
        if utils.match_response(http_res, ["4XX","5XX"], "*"):
            raise errors.SDKError("API error occurred", http_res.status_code, http_res.text, http_res)
        
        content_type = http_res.headers.get("Content-Type")
        raise errors.SDKError(f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", http_res.status_code, http_res.text, http_res)
    
    
    def update(
        self, *,
        id_or_slug: str,
        request_body: Optional[Union[operations.UpdateWorkspaceRequestBody, operations.UpdateWorkspaceRequestBodyTypedDict]] = None,
        server_url: Optional[str] = None,
    ) -> components.WorkspaceSchema:
        r"""Update a workspace

        Update a workspace by ID or slug.

        :param id_or_slug: The ID or slug of the workspace to update.
        :param request_body: 
        :param server_url: Override the default server URL for this method
        """
        base_url = None
        url_variables = None
        if server_url is not None:
            base_url = server_url
        
        request = operations.UpdateWorkspaceRequest(
            id_or_slug=id_or_slug,
            request_body=utils.unmarshal(request_body, operations.UpdateWorkspaceRequestBody) if not isinstance(request_body, BaseModel) and request_body is not None else request_body,
        )
        
        req = self.build_request(
            method="PATCH",
            path="/workspaces/{idOrSlug}",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=False,
            request_has_path_params=True,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(request.request_body, False, True, "json", Optional[operations.UpdateWorkspaceRequestBody]),
        )
        
        http_res = self.do_request(
            hook_ctx=HookContext(operation_id="updateWorkspace", oauth2_scopes=[], security_source=self.sdk_configuration.security),
            request=req,
            error_status_codes=["400","401","403","404","409","410","422","429","4XX","500","5XX"],
        )
        
        
        if utils.match_response(http_res, "200", "application/json"):
            return utils.unmarshal_json(http_res.text, Optional[components.WorkspaceSchema])
        if utils.match_response(http_res, "400", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.BadRequestData)
            raise errors.BadRequest(data=data)
        if utils.match_response(http_res, "401", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.UnauthorizedData)
            raise errors.Unauthorized(data=data)
        if utils.match_response(http_res, "403", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.ForbiddenData)
            raise errors.Forbidden(data=data)
        if utils.match_response(http_res, "404", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.NotFoundData)
            raise errors.NotFound(data=data)
        if utils.match_response(http_res, "409", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.ConflictData)
            raise errors.Conflict(data=data)
        if utils.match_response(http_res, "410", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.InviteExpiredData)
            raise errors.InviteExpired(data=data)
        if utils.match_response(http_res, "422", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.UnprocessableEntityData)
            raise errors.UnprocessableEntity(data=data)
        if utils.match_response(http_res, "429", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.RateLimitExceededData)
            raise errors.RateLimitExceeded(data=data)
        if utils.match_response(http_res, "500", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.InternalServerErrorData)
            raise errors.InternalServerError(data=data)
        if utils.match_response(http_res, ["4XX","5XX"], "*"):
            raise errors.SDKError("API error occurred", http_res.status_code, http_res.text, http_res)
        
        content_type = http_res.headers.get("Content-Type")
        raise errors.SDKError(f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", http_res.status_code, http_res.text, http_res)
    
    
    async def update_async(
        self, *,
        id_or_slug: str,
        request_body: Optional[Union[operations.UpdateWorkspaceRequestBody, operations.UpdateWorkspaceRequestBodyTypedDict]] = None,
        server_url: Optional[str] = None,
    ) -> components.WorkspaceSchema:
        r"""Update a workspace

        Update a workspace by ID or slug.

        :param id_or_slug: The ID or slug of the workspace to update.
        :param request_body: 
        :param server_url: Override the default server URL for this method
        """
        base_url = None
        url_variables = None
        if server_url is not None:
            base_url = server_url
        
        request = operations.UpdateWorkspaceRequest(
            id_or_slug=id_or_slug,
            request_body=utils.unmarshal(request_body, operations.UpdateWorkspaceRequestBody) if not isinstance(request_body, BaseModel) and request_body is not None else request_body,
        )
        
        req = self.build_request(
            method="PATCH",
            path="/workspaces/{idOrSlug}",
            base_url=base_url,
            url_variables=url_variables,
            request=request,
            request_body_required=False,
            request_has_path_params=True,
            request_has_query_params=True,
            user_agent_header="user-agent",
            accept_header_value="application/json",
            security=self.sdk_configuration.security,
            get_serialized_body=lambda: utils.serialize_request_body(request.request_body, False, True, "json", Optional[operations.UpdateWorkspaceRequestBody]),
        )
        
        http_res = await self.do_request_async(
            hook_ctx=HookContext(operation_id="updateWorkspace", oauth2_scopes=[], security_source=self.sdk_configuration.security),
            request=req,
            error_status_codes=["400","401","403","404","409","410","422","429","4XX","500","5XX"],
        )
        
        
        if utils.match_response(http_res, "200", "application/json"):
            return utils.unmarshal_json(http_res.text, Optional[components.WorkspaceSchema])
        if utils.match_response(http_res, "400", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.BadRequestData)
            raise errors.BadRequest(data=data)
        if utils.match_response(http_res, "401", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.UnauthorizedData)
            raise errors.Unauthorized(data=data)
        if utils.match_response(http_res, "403", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.ForbiddenData)
            raise errors.Forbidden(data=data)
        if utils.match_response(http_res, "404", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.NotFoundData)
            raise errors.NotFound(data=data)
        if utils.match_response(http_res, "409", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.ConflictData)
            raise errors.Conflict(data=data)
        if utils.match_response(http_res, "410", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.InviteExpiredData)
            raise errors.InviteExpired(data=data)
        if utils.match_response(http_res, "422", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.UnprocessableEntityData)
            raise errors.UnprocessableEntity(data=data)
        if utils.match_response(http_res, "429", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.RateLimitExceededData)
            raise errors.RateLimitExceeded(data=data)
        if utils.match_response(http_res, "500", "application/json"):
            data = utils.unmarshal_json(http_res.text, errors.InternalServerErrorData)
            raise errors.InternalServerError(data=data)
        if utils.match_response(http_res, ["4XX","5XX"], "*"):
            raise errors.SDKError("API error occurred", http_res.status_code, http_res.text, http_res)
        
        content_type = http_res.headers.get("Content-Type")
        raise errors.SDKError(f"Unexpected response received (code: {http_res.status_code}, type: {content_type})", http_res.status_code, http_res.text, http_res)
    
