def get_database_info(self) -> DatabaseInfoDto:

        try:
            url_withaction = self.__url + "get_database_info"
            result = self.__aceQLHttpApi.call_with_get_url(url_withaction)

            result_analyzer = ResultAnalyzer(
                result, self.__aceQLHttpApi.get_http_status_code())
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__aceQLHttpApi.get_http_status_code())

            if AceQLMetadataApi.__debug:
                print(result)

            holder_database_info_dto_schema = marshmallow_dataclass.class_schema(
                DatabaseInfoDto)
            database_info_dto: DatabaseInfoDto = holder_database_info_dto_schema(
            ).loads(result)

            if AceQLMetadataApi.__debug:
                print(database_info_dto)

            print(database_info_dto)
            return database_info_dto

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())
    def get_table_names(self, table_type: str) -> TableNamesDto:
        try:
            url_withaction = self.__url + "metadata_query/get_table_names"

            if table_type is not None:
                url_withaction += "?table_type=" + table_type

            result = self.__aceQLHttpApi.call_with_get_url(url_withaction)

            result_analyzer = ResultAnalyzer(
                result, self.__aceQLHttpApi.get_http_status_code())
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__aceQLHttpApi.get_http_status_code())

            if AceQLMetadataApi.__debug:
                print(result)

            table_names_dto_schema = marshmallow_dataclass.class_schema(
                TableNamesDto)
            table_names_dto: TableNamesDto = table_names_dto_schema().loads(
                result)

            if AceQLMetadataApi.__debug:
                print(table_names_dto)

            return table_names_dto

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())
    def get_db_metadata(self) -> JdbcDatabaseMetaDataDto:
        try:
            url_withaction = self.__url + "metadata_query/get_db_metadata"
            result = self.__aceQLHttpApi.call_with_get_url(url_withaction)

            result_analyzer = ResultAnalyzer(
                result, self.__aceQLHttpApi.get_http_status_code())
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__aceQLHttpApi.get_http_status_code())

            if AceQLMetadataApi.__debug:
                print(result)

            holder_jdbc_database_meta_data_schema = marshmallow_dataclass.class_schema(
                JdbcDatabaseMetaDataDto)
            jdbc_database_meta_data_holder: JdbcDatabaseMetaDataDto = holder_jdbc_database_meta_data_schema(
            ).loads(result)

            if AceQLMetadataApi.__debug:
                print(jdbc_database_meta_data_holder)

            return jdbc_database_meta_data_holder

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())
예제 #4
0
    def test_A(self):
        result = '{"status" : "OK","session_id" : "mn7andp2tt049iaeaskr28j9ch"}'
        analyzer = ResultAnalyzer(result, 200)
        status_ok = analyzer.is_status_ok()

        self.assertEqual(status_ok, True)
        session_id = analyzer.get_value("session_id")
        self.assertEqual(session_id, "mn7andp2tt049iaeaskr28j9ch")

        print("ResultAnalyzerTest Passed!")
    def get_blob_length(self, blob_id: str) -> int:
        """ Gets the blob length. """
        try:

            if blob_id is None:
                raise TypeError("blob_id is null!")

            action = "get_blob_length"

            dict_params: dict = {"blob_id": blob_id}

            url_withaction = self._url + action

            AceQLDebug.debug("urlWithaction: " + url_withaction)
            AceQLDebug.debug("dictParams   : " + str(dict_params))

            # r = requests.post('http://httpbin.org/post', data = {'key':'value'})

            if self.__timeout is None:
                response = requests.post(url_withaction,
                                         headers=self.__headers,
                                         data=dict_params,
                                         proxies=self.__proxies,
                                         auth=self.__auth)
            else:
                response = requests.post(url_withaction,
                                         headers=self.__headers,
                                         data=dict_params,
                                         proxies=self.__proxies,
                                         auth=self.__auth,
                                         timeout=self.__timeout)

            self.__http_status_code = response.status_code
            result = response.text

            AceQLDebug.debug("result: " + result)

            result_analyzer = ResultAnalyzer(result, self.__http_status_code)
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__http_status_code)

            length_str = result_analyzer.get_value("length")
            AceQLDebug.debug("result: " + length_str + ":")
            return int(length_str)

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None, self.__http_status_code)
    def call_api_no_result(self, command_name: str, command_option: str):
        if command_name is None:
            raise TypeError("command_name is null!")

        try:
            result = self.call_with_get_action(command_name, command_option)

            result_analyzer = ResultAnalyzer(result, self.__http_status_code)
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__http_status_code)

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None, self.__http_status_code)
    def __init__(self,
                 *,
                 url: str,
                 username: str,
                 password: str,
                 database: str,
                 connection_options: ConnectionOptions = None):

        if url is None:
            raise TypeError("url is null!")
        if database is None:
            raise TypeError("database is null!")
        if username is None:
            raise TypeError("username is null!")

        session_id: str = None
        proxies: dict = None
        auth: ProxyAuth = None
        gzip_result: bool = True
        timeout = None
        request_headers: dict = {}

        if connection_options is not None:
            if connection_options.session_id is not None:
                session_id = connection_options.session_id
                password = None
            proxies = connection_options.proxies
            auth = connection_options.auth
            gzip_result = connection_options.gzip_result
            timeout = connection_options.timeout
            request_headers = connection_options.request_headers

        self.__url = url
        self.__database = database
        self.__username = username
        self.__password = password
        self.__proxies = proxies
        self.__auth = auth
        self.__gzip_result = gzip_result
        self.__timeout = timeout
        self.__headers = request_headers
        self.__http_status_code = requests.codes.ok

        # Other self for other methods
        self.__pretty_printing = True
        #self.__temp_length = 0  ==> Done in AceQLBlobUploadApi
        #self.__total_length = 0 ==> ==> Done in AceQLBlobUploadApi
        self.__progress_indicator = None

        # url = c + "/database/" + database + "/username/" \
        #       + username + "/connect" + "?password="******"/session/" + session_id + "/get_connection"

                result = self.call_with_get_url(the_url)

                result_analyzer = ResultAnalyzer(result,
                                                 self.__http_status_code)
                if not result_analyzer.is_status_ok():
                    raise Error(result_analyzer.get_error_message(),
                                result_analyzer.get_error_type(), None, None,
                                self.__http_status_code)

                connection_id = result_analyzer.get_value("connection_id")
                self.__url = url + "/session/" + session_id + "/connection/" + connection_id + "/"

            else:
                url = url + "/database/" + database + "/username/" \
                      + username + "/login"

                dict_params = {
                    "password": password,
                    "client_version": str(VersionValues.VERSION)
                }

                result = self.call_with_post_url(url, dict_params)

                result_analyzer = ResultAnalyzer(result,
                                                 self.__http_status_code)
                if not result_analyzer.is_status_ok():
                    raise Error(result_analyzer.get_error_message(),
                                result_analyzer.get_error_type(), None, None,
                                self.__http_status_code)

                session_id = result_analyzer.get_value("session_id")
                connection_id = result_analyzer.get_value("connection_id")
                self.__url = url + "/session/" + session_id + "/connection/" + connection_id + "/"

                user_login_store.set_session_id(session_id)
        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None, self.__http_status_code)
    def execute_update(self, sql: str, is_prepared_statement: bool,
                       statement_parameters: dict):
        """Calls /execute_update API"""

        try:
            action = "execute_update"
            AceQLExecutionUtil.check_values(is_prepared_statement, sql)
            dict_params: dict = {"sql": sql}

            AceQLExecutionUtil.set_is_prepared_statement(
                dict_params, is_prepared_statement)
            url_withaction = self.__url + action

            AceQLDebug.debug("url_withaction: " + url_withaction)
            AceQLDebug.debug("dict_params 1: " + str(dict_params))

            if statement_parameters is not None:
                if not isinstance(statement_parameters, dict):
                    raise TypeError(
                        "statement_parameters is not a dictionary!")

                dict_params.update(statement_parameters)

            AceQLDebug.debug("dictParams 2: " + str(dict_params))

            # r = requests.post('http://httpbin.org/post', data = {'key':'value'})
            # print("Before update request")

            if self.__aceQLHttpApi.get_timeout() is None:
                AceQLDebug.debug("UPDATE HERE 1")
                response: Request = requests.post(
                    url_withaction,
                    headers=self.__aceQLHttpApi.get_headers(),
                    data=dict_params,
                    proxies=self.__aceQLHttpApi.get_proxies(),
                    auth=self.__aceQLHttpApi.get_auth())
            else:
                AceQLDebug.debug("UPDATE HERE 2")
                response: Request = requests.post(
                    url_withaction,
                    headers=self.__aceQLHttpApi.get_headers(),
                    data=dict_params,
                    proxies=self.__aceQLHttpApi.get_proxies(),
                    auth=self.__aceQLHttpApi.get_auth(),
                    timeout=self.__aceQLHttpApi.get_timeout())

            self.__aceQLHttpApi.set_http_status_code(response.status_code)
            result = response.text

            # print("self.__http_status_code: " + str(self.__http_status_code ))
            # print("result                 : " + str(result))
            AceQLDebug.debug("result: " + result)

            result_analyzer = ResultAnalyzer(
                result, self.__aceQLHttpApi.get_http_status_code())
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None,
                            self.__aceQLHttpApi.get_http_status_code())

            row_count = result_analyzer.get_int_value("row_count")
            return row_count

        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None,
                            self.__aceQLHttpApi.get_http_status_code())
    def execute_batch(self, sql: str, batch_file_parameters: str):

        try:
            action = "prepared_statement_execute_batch"
            AceQLExecutionUtil.check_values(True, sql)
            url_withaction = self.__url + action

            AceQLDebug.debug("url_withaction: " + url_withaction)

            blob_id: str = os.path.basename(batch_file_parameters)
            length: int = os.path.getsize(batch_file_parameters)

            with open(batch_file_parameters, "rb") as fd:
                self.__aceQLHttpApi.blob_upload(blob_id, fd, length)

            dict_params: dict = {"sql": sql, "blob_id": blob_id}
            AceQLDebug.debug("dict_params: " + str(dict_params))

            # r = requests.post('http://httpbin.org/post', data = {'key':'value'})
            # print("Before update request")

            # if self.__timeout is None:
            #     response: Request = requests.post(url_withaction, headers=self.__headers, data=dict_params,
            #                                       proxies=self.__proxies, auth=self.__auth)
            # else:
            #     response: Request = requests.post(url_withaction, headers=self.__headers, data=dict_params,
            #                                       proxies=self.__proxies, auth=self.__auth,
            #                                       timeout=self.__timeout)

            if self.__aceQLHttpApi.get_timeout() is None:
                response: Request = requests.post(url_withaction, headers=self.__aceQLHttpApi.get_headers(), data=dict_params,
                                                  proxies=self.__aceQLHttpApi.get_proxies(),
                                                  auth=self.__aceQLHttpApi.get_auth())
            else:
                response: Request = requests.post(url_withaction, headers=self.__aceQLHttpApi.get_headers(), data=dict_params,
                                                  proxies=self.__aceQLHttpApi.get_proxies(),
                                                  auth=self.__aceQLHttpApi.get_auth(),
                                                  timeout=self.__aceQLHttpApi.get_timeout())

            self.__aceQLHttpApi.set_http_status_code(response.status_code)
            result = response.text

            # print("self.__http_status_code: " + str(self.__http_status_code ))
            # print("result                 : " + str(result))
            AceQLDebug.debug("result: " + result)

            result_analyzer = ResultAnalyzer(result, self.__aceQLHttpApi.get_http_status_code())
            if not result_analyzer.is_status_ok():
                raise Error(result_analyzer.get_error_message(),
                            result_analyzer.get_error_type(), None, None, self.__aceQLHttpApi.get_http_status_code())

            update_counts_array_dto_schema = marshmallow_dataclass.class_schema(UpdateCountsArrayDto)
            update_counts_array_dto_back: UpdateCountsArrayDto = update_counts_array_dto_schema().loads(
                result)

            update_counts_array: List[int] = update_counts_array_dto_back.updateCountsArray
            return update_counts_array
        except Exception as e:
            if isinstance(e, Error):
                raise
            else:
                raise Error(str(e), 0, e, None, self.__aceQLHttpApi.get_http_status_code())