예제 #1
0
    def __init__(self, logger=None, config=None, connection=None):
        # logger

        self.logger = logger if logger is not None else get_logger()
        # configurations
        self.config = config if config is not None else get_config()
        # database connection

        if self.NOSQL:
            self.connection = connection if connection is not None else nosql_get_connection(
            )
            if self.DEBUG:
                self._repository = self.NOSQL_REPOSITORY(
                    self.connection, self.config.DYNAMODB_TABLE_NAME, logger)
            else:
                self._repository = self.NOSQL_REPOSITORY(
                    self.connection, self.config.DYNAMODB_TABLE_NAME)
        else:
            self.connection = connection if connection is not None else get_connection(
            )
            if self.DEBUG:
                self._repository = self.REPOSITORY(self.connection,
                                                   self.TABLE_NAME, logger)
            else:
                self._repository = self.REPOSITORY(self.connection,
                                                   self.TABLE_NAME)
    def setUpClass(cls):
        cls.CONFIG = get_config()
        cls.CONFIG.SQS_ENDPOINT = cls.SQS_LOCALSTACK

        # fixture
        if cls.EXECUTE_FIXTURE:
            logger = get_logger()

            queue_url = cls.CONFIG.APP_QUEUE
            queue_name = SQSHelper.get_queue_name(queue_url)
            deleted = SQSHelper.delete_queue(queue_url)
            if deleted:
                logger.info(f'Deleting queue name: {queue_name}')

            attributes = {'DelaySeconds': '1'}
            result = SQSHelper.create_queue(queue_url, attributes)
            if result is not None:
                logger.info(f'queue {queue_name} created')
            else:
                logger.error(f'queue {queue_name} not created')

            event = get_sqs_event_sample()[0]

            SQSHelper.create_message(event)
            json_list = get_json_sqs_event_exemplo()[0]

            for json_exemplo in json_list:
                SQSHelper.create_message(json.loads(json_exemplo))

            logger.info('created message: {}'.format(event))
 def __init__(self, logger=None, config=None):
     # logger
     self.logger = logger if logger is not None else get_logger()
     # configurations
     self.config = config if config is not None else get_config()
     # last_exception
     self.exception = None
def connect():
    from chalicelib.config import get_config
    config = get_config()
    endpoint_url = config.SQS_ENDPOINT
    session = session_mock
    connection = session.resource('sqs',
                                  endpoint_url=endpoint_url,
                                  region_name=config.REGION_NAME)

    return connection
예제 #5
0
    def test_connection(self):
        self.logger.info('Running test: %s', get_function_name(__name__))

        config = get_config()
        self.logger.info('REDIS_HOST: {}'.format(config.REDIS_HOST))
        self.logger.info('REDIS_PORT: {}'.format(config.REDIS_PORT))

        connection = get_connection()

        self.assertIsNotNone(connection)
    def setUp(self):
        super().setUp()
        # sobrescreve o mock
        self.connection = get_connection()
        self.config = get_config()

        data = {
            'Items': [
                {
                    "uuid": "067d914c-cb19-4f3f-8755-584e0eafe344",
                    "identification": "ISO 9001:2015",
                    "publication_date": "2015-09-30",
                    "title": "Sistemas de gestão da qualidade - Requisitos",
                    "description":
                    "This document reached stage 30.60 on 2021-02-26, TC/SC: ISO/TC 42, ICS: 37.040.20",
                    "link":
                    "http://www.iso.org/cms/render/live/en/sites/isoorg/contents/data/standard/03/19/31944.html",
                    "synchronized": True
                },
                {
                    "uuid": "fd214a67-1ad9-4152-87b8-b1e0f7d12cb9",
                    "identification": "ISO/DTS 18950.2",
                    "publication_date": "2021-02-26",
                    "title":
                    "ISO/DTS 18950.2 - Imaging materials — Reflection colour photographic images — Indoor light stability specifications for museum display",
                    "description":
                    "This document reached stage 30.60 on 2021-02-26, TC/SC: ISO/TC 42, ICS: 37.040.20",
                    "link":
                    "http://www.iso.org/cms/render/live/en/sites/isoorg/contents/data/standard/03/19/31944.html",
                    "synchronized": False
                },
            ],
            'Count':
            2,
            'ScannedCount':
            2,
            'ResponseMetadata': {
                'RequestId': 'ebe11a2f-1b8f-472b-8507-c9aadeb3a379',
                'HTTPStatusCode': 200,
                'HTTPHeaders': {
                    'date': 'Sun, 28 Feb 2021 03:06:49 GMT',
                    'content-type': 'application/x-amz-json-1.0',
                    'x-amz-crc32': '2132082689',
                    'x-amzn-requestid': 'ebe11a2f-1b8f-472b-8507-c9aadeb3a379',
                    'content-length': '1130',
                    'server': 'Jetty(9.4.18.v20190429)'
                },
                'RetryAttempts': 0
            }
        }
        # Mock returns
        table_mock.scan.side_effect = lambda: data
        table_mock.item_count = data['Count']
    def setUpClass(cls):
        cls.CONFIG = get_config()

        # fixture
        if cls.EXECUTE_FIXTURE:
            logger = get_logger()
            table_name = get_config().DYNAMODB_TABLE_NAME

            DynamoDBHelper.drop_table(connection=get_connection(), table_name=table_name)
            logger.info(f'table {table_name}')

            file_name = ROOT_DIR + '/tests/datasets/nosql/create.table.standard-update-checker-dynamodb-StandardUpdatesTable.json'
            DynamoDBHelper.create_table(connection=get_connection(),
                                        table_name=table_name,
                                        file_name=file_name)
            logger.info(f'table {table_name} created')

            file_name = ROOT_DIR + '/tests/datasets/seeders/seeder.standard-update-checker-dynamodb-StandardUpdatesTable.json'
            DynamoDBHelper.sow_table(connection=get_connection(), table_name=table_name,
                                     file_name=file_name)
            logger.info(f'table {table_name} populated')
예제 #8
0
    def test_connection_error(self):
        self.logger.info('Running test: %s', get_function_name(__name__))

        config = get_config()
        # forca os parametros
        config.REDIS_HOST = 'localhost'
        config.REDIS_PORT = '1111'

        self.logger.info('REDIS_HOST: {}'.format(config.REDIS_HOST))
        self.logger.info('REDIS_PORT: {}'.format(config.REDIS_PORT))
        connection = get_connection(config)

        self.assertIsNone(connection)
예제 #9
0
def get_connection(config=None, retry=False):
    global _CONNECTION, _RETRY_COUNT, _MAX_RETRY_ATTEMPTS
    if not _CONNECTION:
        connection = None

        if config is None:
            config = get_config()

        try:
            host = config.REDIS_HOST
            port = config.REDIS_PORT
            test = False
            try:
                connection = redis.Redis(host=host, port=port)
                test = connection.set('connection', 'true')
            except Exception as err:
                logger.error(err)
                # docker
                if host == 'redis':
                    # localhost
                    host = 'localhost'
                    connection = redis.Redis(host=host, port=port)
                    test = connection.set('connection', 'true')

            if not test:
                raise Exception('Redis - Unable to connect')
            else:
                _CONNECTION = connection
                _RETRY_COUNT = 0
                logger.info('Redis - Connected')
        except Exception as err:
            if _RETRY_COUNT == _MAX_RETRY_ATTEMPTS:
                _RETRY_COUNT = 0
                logger.error(err)
                connection = None
                return connection
            else:
                logger.error(err)
                logger.info(
                    'Redis - Trying to reconnect... {}'.format(_RETRY_COUNT))

                sleep(0.1)
                # retry
                if not retry:
                    _RETRY_COUNT += 1
                    return get_connection(config)
    else:
        connection = _CONNECTION

    return connection
예제 #10
0
def get_connection(config=None, connect=True, retry=False):
    global _CONNECTION, _RETRY_COUNT, _MAX_RETRY_ATTEMPTS
    if not _CONNECTION:
        connection = None
        if config is None:
            config = get_config()
        try:
            params = {
                'host': config.DB_HOST,
                'user': config.DB_USER,
                'password': config.DB_PASSWORD,
                'db': config.DB
            }

            # connection = create_engine('sqlite:///:memory:', echo=True)
            # utf8mb4
            connection = create_engine(
                "mysql+pymysql://%s:%s@%s/%s?charset=utf8" %
                (params['user'], params['password'], params['host'],
                 params['db']),
                echo=True)

            if connect:
                connection.connect()
            _CONNECTION = connection
            _RETRY_COUNT = 0
            logger.info('MySQL - Connected')
        except Exception as err:
            if _RETRY_COUNT == _MAX_RETRY_ATTEMPTS:
                _RETRY_COUNT = 0
                logger.error(err)
                connection = None
                return connection
            else:
                logger.error(err)
                logger.info(
                    'MySQL - Trying to reconnect... {}'.format(_RETRY_COUNT))

                sleep(0.1)
                # retry
                if not retry:
                    _RETRY_COUNT += 1
                    return get_connection(config, True)
    else:
        connection = _CONNECTION

    return connection
    def setUpClass(cls):
        cls.CONFIG = get_config()
        cls.CONFIG.SQS_ENDPOINT = cls.SQS_LOCALSTACK

        # fixture
        if cls.EXECUTE_FIXTURE:
            logger = get_logger()

            queue_url = cls.CONFIG.APP_QUEUE
            queue_name = SQSHelper.get_queue_name(queue_url)
            deleted = SQSHelper.delete_queue(queue_url)
            if deleted:
                logger.info(f'Deleting queue name: {queue_name}')

            result = SQSHelper.create_queue(queue_url)
            if result is not None:
                logger.info(f'queue {queue_name} created')
            else:
                logger.error(f'queue {queue_name} not created')
예제 #12
0
    def __init__(self, logger=None, config=None, connection=None):
        # logger
        self.logger = logger if logger is not None else get_logger()
        # configurations
        self.config = config if config is not None else get_config()
        # database connection
        self.connection = connection if connection is not None else get_connection(
        )

        if self.DEBUG:
            self._repository = self.REPOSITORY(self.connection,
                                               self.config.DYNAMODB_TABLE_NAME,
                                               logger)
        else:
            self._repository = self.REPOSITORY(self.connection,
                                               self.config.DYNAMODB_TABLE_NAME)

        self.data_list = []
        self.updates = []
        self.queue = Queue()
        self.responses = []
예제 #13
0
def get_connection(config=None, connect=True, retry=False):
    global _CONNECTION, _RETRY_COUNT, _MAX_RETRY_ATTEMPTS
    if not _CONNECTION:
        connection = None
        if config is None:
            config = get_config()
        try:

            connection_string = 'sqlite:///:memory:'
            if config.DB:
                connection_string = 'sqlite:///%s.db' % config.DB

            connection = create_engine(connection_string, echo=True)
            if connect:
                connection.connect()
            _CONNECTION = connection
            _RETRY_COUNT = 0
            logger.info('SQLite - Connected')
        except Exception as err:
            if _RETRY_COUNT == _MAX_RETRY_ATTEMPTS:
                _RETRY_COUNT = 0
                logger.error(err)
                connection = None
                return connection
            else:
                logger.error(err)
                logger.info('SQLite - Trying to reconnect... {}'.format(_RETRY_COUNT))

                sleep(0.1)
                # retry
                if not retry:
                    _RETRY_COUNT += 1
                    return get_connection(config, True)
    else:
        connection = _CONNECTION

    return connection
register_vendor()

# registra a pasta vendor (antes de tudo)
from chalicelib.enums.messages import MessagesEnum
from chalicelib.exceptions import ApiException
from chalicelib.http_resources.request import ApiRequest
from chalicelib.http_resources.response import ApiResponse
from chalicelib.services.v1.update_checker_service import UpdateCheckerService

from chalicelib.config import get_config
from chalicelib.logging import get_logger, get_log_level
from chalicelib import APP_NAME, helper, http_helper, APP_VERSION
from chalice import Chalice

# config
config = get_config()
# debug
debug = helper.debug_mode()
# logger
logger = get_logger()
# chalice app
app = Chalice(app_name=APP_NAME, debug=debug)
# override the log configs
if not debug:
    # override to the level desired
    logger.level = get_log_level()
# override the log instance
app.log = logger


@app.route('/', cors=True)
from chalicelib import APP_NAME, APP_VERSION
# Create an APISpec
from chalicelib.config import get_config
from chalicelib.helper import open_vendor_file
from chalicelib.openapi import api_schemas
from chalicelib.openapi.schemas import PingSchema

servers = [{
    "url":
    os.environ["API_SERVER"] if "API_SERVER" in os.environ else None,
    "description":
    os.environ["API_SERVER_DESCRIPTION"]
    if "API_SERVER_DESCRIPTION" in os.environ else None
}]

if get_config().APP_ENV == "development":
    servers.append({
        "url": "http://localhost:8001",
        "description": "Development server"
    })

spec = APISpec(title=APP_NAME,
               openapi_version='3.0.2',
               version=APP_VERSION,
               plugins=[MarshmallowPlugin()],
               servers=servers)


def generate_openapi_yml(spec_object, logger, force=False):
    openapi_data = spec_object.to_yaml()
    def setUp(self):
        super().setUp()
        # sobrescreve o mock
        self.connection = get_connection()
        self.config = get_config()

        data = {
            'Items': [{
                'comite': 'ABNT',
                'language': 'Português',
                'title': 'ISO 5000',
                'uuid': '3fa85f64-5717-4562-b3fc-2c963f66afa6',
                'url': '',
                'objective': '',
                'identification': 'ISO 5000:2000',
                'pages': 10,
                'file': '',
                'price': '10.00',
                'validity_start': '2021-01-18',
                'title_global_language': 'ISO 5000',
                'organization': 'ABNT',
                'publication_date': '2021-01-18',
                'currency': 'Real',
                'status': 'Em Vigor'
            }, {
                'comite': '',
                'language': 'English',
                'title': 'Sistemas de gestão da qualidade - Requisitos',
                'uuid': '067d914c-cb19-4f3f-8755-584e0eafe344',
                'url':
                'http://paginapessoal.utfpr.edu.br/canabarro/iso%209000-2015.pdf/at_download/file',
                'objective': None,
                'identification': 'ISO 9001:2015',
                'pages': 30,
                'file': 'NORMA%20ISO%2090002015.pdf',
                'price': '170.00',
                'validity_start': '2015-10-30',
                'title_global_language': '',
                'organization':
                'ABNT - Associação Brasileira de Normas Técnicas',
                'publication_date': '2015-09-30',
                'currency': 'BRL',
                'status': 'Arquivado'
            }],
            'Count':
            2,
            'ScannedCount':
            2,
            'ResponseMetadata': {
                'RequestId': 'ebe11a2f-1b8f-472b-8507-c9aadeb3a379',
                'HTTPStatusCode': 200,
                'HTTPHeaders': {
                    'date': 'Sun, 28 Feb 2021 03:06:49 GMT',
                    'content-type': 'application/x-amz-json-1.0',
                    'x-amz-crc32': '2132082689',
                    'x-amzn-requestid': 'ebe11a2f-1b8f-472b-8507-c9aadeb3a379',
                    'content-length': '1130',
                    'server': 'Jetty(9.4.18.v20190429)'
                },
                'RetryAttempts': 0
            }
        }
        # Mock returns
        table_mock.scan.side_effect = lambda: data
        table_mock.item_count = data['Count']