"""
BackendMarketplaceAppTag automatically generated by SDKgen please do not edit this file manually
https://sdkgen.app
"""

import requests
import sdkgen
from requests import RequestException
from typing import List

from .marketplace_app import MarketplaceApp
from .marketplace_app_collection import MarketplaceAppCollection
from .marketplace_install import MarketplaceInstall
from .marketplace_message import MarketplaceMessage
from .marketplace_message_exception import MarketplaceMessageException

class BackendMarketplaceAppTag(sdkgen.TagAbstract):
    def __init__(self, http_client: requests.Session, parser: sdkgen.Parser):
        super().__init__(http_client, parser)


    def upgrade(self, user: str, name: str) -> MarketplaceMessage:
        try:
            path_params = {}
            path_params["user"] = user
            path_params["name"] = name

            query_params = {}

            query_struct_names = []

            url = self.parser.url("/backend/marketplace/app/:user/:name", path_params)

            headers = {}

            response = self.http_client.put(url, headers=headers, params=self.parser.query(query_params, query_struct_names))

            if response.status_code >= 200 and response.status_code < 300:
                return MarketplaceMessage.model_validate_json(json_data=response.content)

            if response.status_code == 400:
                raise MarketplaceMessageException(response.content)
            if response.status_code == 401:
                raise MarketplaceMessageException(response.content)
            if response.status_code == 404:
                raise MarketplaceMessageException(response.content)
            if response.status_code == 410:
                raise MarketplaceMessageException(response.content)
            if response.status_code == 500:
                raise MarketplaceMessageException(response.content)

            raise sdkgen.UnknownStatusCodeException("The server returned an unknown status code")
        except RequestException as e:
            raise sdkgen.ClientException("An unknown error occurred: " + str(e))

    def get(self, user: str, name: str) -> MarketplaceApp:
        try:
            path_params = {}
            path_params["user"] = user
            path_params["name"] = name

            query_params = {}

            query_struct_names = []

            url = self.parser.url("/backend/marketplace/app/:user/:name", path_params)

            headers = {}

            response = self.http_client.get(url, headers=headers, params=self.parser.query(query_params, query_struct_names))

            if response.status_code >= 200 and response.status_code < 300:
                return MarketplaceApp.model_validate_json(json_data=response.content)

            if response.status_code == 401:
                raise MarketplaceMessageException(response.content)
            if response.status_code == 404:
                raise MarketplaceMessageException(response.content)
            if response.status_code == 410:
                raise MarketplaceMessageException(response.content)
            if response.status_code == 500:
                raise MarketplaceMessageException(response.content)

            raise sdkgen.UnknownStatusCodeException("The server returned an unknown status code")
        except RequestException as e:
            raise sdkgen.ClientException("An unknown error occurred: " + str(e))

    def install(self, payload: MarketplaceInstall) -> MarketplaceMessage:
        try:
            path_params = {}

            query_params = {}

            query_struct_names = []

            url = self.parser.url("/backend/marketplace/app", path_params)

            headers = {}
            headers["Content-Type"] = "application/json"

            response = self.http_client.post(url, headers=headers, params=self.parser.query(query_params, query_struct_names), json=payload.model_dump(by_alias=True))

            if response.status_code >= 200 and response.status_code < 300:
                return MarketplaceMessage.model_validate_json(json_data=response.content)

            if response.status_code == 400:
                raise MarketplaceMessageException(response.content)
            if response.status_code == 401:
                raise MarketplaceMessageException(response.content)
            if response.status_code == 500:
                raise MarketplaceMessageException(response.content)

            raise sdkgen.UnknownStatusCodeException("The server returned an unknown status code")
        except RequestException as e:
            raise sdkgen.ClientException("An unknown error occurred: " + str(e))

    def get_all(self, start_index: int, query: str) -> MarketplaceAppCollection:
        try:
            path_params = {}

            query_params = {}
            query_params["startIndex"] = start_index
            query_params["query"] = query

            query_struct_names = []

            url = self.parser.url("/backend/marketplace/app", path_params)

            headers = {}

            response = self.http_client.get(url, headers=headers, params=self.parser.query(query_params, query_struct_names))

            if response.status_code >= 200 and response.status_code < 300:
                return MarketplaceAppCollection.model_validate_json(json_data=response.content)

            if response.status_code == 401:
                raise MarketplaceMessageException(response.content)
            if response.status_code == 500:
                raise MarketplaceMessageException(response.content)

            raise sdkgen.UnknownStatusCodeException("The server returned an unknown status code")
        except RequestException as e:
            raise sdkgen.ClientException("An unknown error occurred: " + str(e))


