示例#1
0
    def test_next_long(self):
        id1 = IdGenerator.next_long()
        assert id1 != None
        assert len(id1) == 32

        id2 = IdGenerator.next_long()
        assert id2 != None
        assert len(id2) == 32
        assert id1 != id2
示例#2
0
    def test_next_short(self):
        id1 = IdGenerator.next_short()
        assert id1 != None
        assert len(id1) >= 9

        id2 = IdGenerator.next_short()
        assert id2 != None
        assert len(id2) >= 9
        assert id1 != id2
 def __get_log_item(self) -> Any:
     if self.__include_type_name:
         return {
             '_index': self.__current_index,
             '_type': "log_message",
             '_id': IdGenerator.next_long()
         }  # ElasticSearch 6.x
     else:
         return {
             '_index': self.__current_index,
             '_id': IdGenerator.next_long()
         }  # ElasticSearch 7.x
    def set(self, correlation_id, item):
        """
        Sets a data item. If the data item exists it updates it, otherwise it create a new data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param item: an item to be set.

        :return: an updated item
        """
        item = self._convert_from_public(item)
        new_item = dict(item)

        # Replace _id or generate a new one
        new_item.pop('_id', None)
        new_item['_id'] = item['id'] if 'id' in item and item[
            'id'] != None else IdGenerator.next_long()
        id = new_item['_id']

        item = self._collection.find_one_and_update( \
            { '_id': id }, { '$set': new_item }, \
            return_document = pymongo.ReturnDocument.AFTER, \
            upsert = True \
        )

        item = self._convert_to_public(item)
        return item
    def set(self, correlation_id: Optional[str], item: T) -> T:
        """
        Sets a data item. If the data item exists it updates it, otherwise it create a new data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param item: an item to be set.

        :return: an updated item
        """
        item = self.__convert_to_obj(item)

        if not hasattr(item, 'id') or item.id is None:
            item.id = IdGenerator.next_long()

        with self._lock:
            old_item = self._find_one(item.id)
            if old_item is None:
                self._items.append(item)
            else:
                index = self._items.index(old_item)
                if index < 0:
                    self._items.append(item)
                else:
                    self._items[index] = item

        self._logger.trace(correlation_id, "Set " + str(item))

        # Avoid reentry
        self.save(correlation_id)
        return item
    def create_beacon(self, correlation_id: Optional[str], beacon: BeaconV1) -> BeaconV1:
        beacon.id = beacon.id or IdGenerator.next_long()
        beacon.type = beacon.type or 'unknown'

        self.__beacons.append(beacon)

        return beacon
    def set(self, correlation_id, item):
        """
        Sets a data item. If the data item exists it updates it, otherwise it create a new data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param item: an item to be set.

        :return: an updated item
        """
        if 'id' not in item or item['id'] == None:
            item['id'] = IdGenerator.next_long()

        self._lock.acquire()
        try:
            old_item = self._find_one(item['id'])
            if old_item == None:
                self._items.append(item)
            else:
                index = self._items.index(old_item)
                if index < 0:
                    self._items.append(item)
                else:
                    self._items[index] = item
        finally:
            self._lock.release()

        self._logger.trace(correlation_id, "Set " + str(item))

        # Avoid reentry
        self.save(correlation_id)
        return item
    def set(self, correlation_id: Optional[str], item: T) -> T:
        """
        Sets a data item. If the data item exists it updates it, otherwise it create a new data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param item: an item to be set.

        :return: an updated item
        """
        if item is None:
            return

        item = self._convert_from_public(item)
        new_item = dict(item)

        # Replace _id or generate a new one
        if new_item['_id'] is None and self._auto_generate_id:
            new_item['_id'] = IdGenerator.next_long()

        new_item = self._convert_from_public(new_item)

        item = self._collection.find_one_and_replace(
            {'_id': new_item['_id']},
            new_item,
            return_document=pymongo.ReturnDocument.AFTER,
            upsert=True)

        item = self._convert_to_public(item)

        if item:
            self._logger.trace(correlation_id, "Set in %s with id = %s",
                               self._collection_name, item.id)

        return item
    def create_beacon(self, correlation_id, beacon):
        if beacon is None:
            return

        beacon = copy.deepcopy(beacon)
        beacon.id = beacon.id or IdGenerator.next_long()
        self._items.append(beacon)
        return beacon
    def create_site(self, correlation_id: Optional[str], site: SiteV1) -> SiteV1:
        site.id = site.id or IdGenerator.next_long()
        site.create_time = datetime.datetime.now()
        site.active = site.active is not None or True

        self.__sites.append(site)

        return site
    def __init__(self):
        super().__init__()

        self.__connection_resolver: ConnectionResolver = ConnectionResolver()
        self.__credential_resolver: CredentialResolver = CredentialResolver()

        self.__lock: str = IdGenerator.next_long()
        self.__timeout: int = 30000
        self.__retries: int = 3
        self.__client: redis.Redis = None
示例#12
0
    def create(self, correlation_id: Optional[str], item: Dummy) -> Dummy:
        self.__lock.acquire()
        try:
            if item.id is None:
                item.id = IdGenerator.next_long()

            self.__items.append(item)
        finally:
            self.__lock.release()

        return item
示例#13
0
    def create(self, correlation_id, item):
        self._lock.acquire()
        try:
            if 'id' not in item or item['id'] == '' or item['id'] is None:
                item['id'] = IdGenerator.next_long()

            self._items.append(item)
        finally:
            self._lock.release()

        return item
示例#14
0
    def __init__(self, correlation_id=None, message_type=None, message=None):
        """
        Creates a new MessageEnvelope, which adds a correlation id, message id, and a type to the
        data being sent/received.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param message_type: a string value that defines the message's type.

        :param message: the data being sent/received.
        """
        self.correlation_id = correlation_id
        self.message_type = message_type
        self.message = message
        self.message_id = IdGenerator.next_long()
    def create(self, correlation_id: Optional[str], item: T) -> T:
        """
        Creates a data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param item: an item to be created.

        :return: a created item
        """
        item = self.__convert_to_obj(item)

        if not hasattr(item, 'id') or item.id is None:
            item.id = IdGenerator.next_long()

        return super().create(correlation_id, item)
示例#16
0
    def create_account(self, correlation_id: Optional[str],
                       account: AccountV1) -> Optional[AccountV1]:
        if account is None:
            return

        accounts = list(
            filter(lambda x: x.id == account.id or x.login == account.login,
                   self.__accounts))
        if len(accounts) > 0:
            raise BadRequestException(correlation_id, 'DUPLICATE_LOGIN',
                                      'Found account with duplicate login')

        account = deepcopy(account)
        account.id = account.id or IdGenerator.next_long()

        self.__accounts.append(account)

        return account
示例#17
0
    def create(self, correlation_id: Optional[str], item: T) -> Optional[T]:
        """
        Creates a data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.
        :param item: an item to be created.
        :return: created item
        """
        if item is None:
            return

        # Assign unique id
        new_item = deepcopy(item)

        if new_item.id is None and self._auto_generate_id:
            new_item = deepcopy(new_item)
            new_item.id = item.id or IdGenerator.next_long()

        return super().create(correlation_id, new_item)
    def test_get_open_api_spec_from_file(self):
        self.service.close(None)

        open_api_content = 'swagger yaml content from file'
        filename = 'dummy_' + IdGenerator.next_long() + '.tmp'

        # create temp file
        with open(filename, 'w') as f:
            f.write(open_api_content)

        # recreate service with new configuration
        service_config = ConfigParams.from_tuples(
            "connection.protocol",
            "http",
            "connection.host",
            "localhost",
            "connection.port",
            3001,
            "swagger.path",
            filename,  # for test only
            "swagger.enable",
            "true")
        ctrl = DummyController()

        self.service = DummyRestService()
        self.service.configure(service_config)

        references = References.from_tuples(
            Descriptor('pip-services-dummies', 'controller', 'default',
                       'default', '1.0'), ctrl,
            Descriptor('pip-services-dummies', 'service', 'rest', 'default',
                       '1.0'), self.service)
        self.service.set_references(references)

        self.service.open(None)

        response = requests.request('GET', 'http://localhost:3001/swagger')
        assert response.text == open_api_content

        # delete temp file
        os.remove(filename)
示例#19
0
    def set(self, correlation_id: Optional[str], item: T) -> Optional[T]:
        """
        Sets a data item. If the data item exists it updates it,
        otherwise it create a new data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.
        :param item: a item to be set.
        :return: updated item
        """
        if item is None:
            return

        # Assign unique id
        if item.get('id') is None and self._auto_generate_id:
            item = deepcopy(item)
            item['id'] = item['id'] or IdGenerator.next_long()

        row = self._convert_from_public_partial(item)
        columns = self._generate_columns(row)
        params = self._generate_parameters(row)
        set_params = self._generate_set_parameters(row)
        values = self._generate_values(row)
        values += deepcopy(values)
        values.append(item['id'])

        query = "INSERT INTO " + self._quoted_table_name(
        ) + " (" + columns + ") VALUES (" + params + ")"
        query += " ON DUPLICATE KEY UPDATE " + set_params
        query += "; SELECT * FROM " + self._quoted_table_name(
        ) + " WHERE id=%s"

        result = self._request(query, values)

        new_item = self._convert_to_public(
            result['items'][0]) if result['items'] and len(
                result['items']) == 1 else None

        self._logger.trace(correlation_id, "Set in %s with id = %s",
                           self._quoted_table_name(), item.id)

        return new_item
    def create(self, correlation_id: Optional[str], item: T) -> T:
        """
        Creates a data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param item: an item to be created.

        :return: a created item
        """
        if item is None:
            return

        item = self._convert_from_public(item)
        new_item = deepcopy(item)

        # Replace _id or generate a new one
        if new_item['_id'] is None and self._auto_generate_id:
            new_item['_id'] = IdGenerator.next_long()

        return super().create(correlation_id, new_item)
    def __init__(self,
                 id: str,
                 user_id: str,
                 user_name: str = None,
                 address: str = None,
                 client: str = None):

        # Identification
        self.id = id or IdGenerator.next_long()
        self.user_id = user_id
        self.user_name = user_name
        # Session info
        self.active = True
        self.open_time = datetime.datetime.now()
        self.close_time: datetime.datetime = None
        self.request_time = datetime.datetime.now()
        self.address = address
        self.client = client

        # Cached content
        self.user: Any = None
        self.data: Any = None
    def create(self, correlation_id, item):
        """
        Creates a data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param item: an item to be created.

        :return: a created item
        """
        item = self._convert_from_public(item)
        new_item = dict(item)

        # Replace _id or generate a new one
        new_item.pop('_id', None)
        new_item['_id'] = item['id'] if 'id' in item and item[
            'id'] != None else IdGenerator.next_long()

        result = self._collection.insert_one(new_item)
        item = self._collection.find_one({'_id': result.inserted_id})

        item = self._convert_to_public(item)
        return item
    def create(self, correlation_id, item):
        """
        Creates a data item.

        :param correlation_id: (optional) transaction id to trace execution through call chain.

        :param item: an item to be created.

        :return: a created item
        """
        if 'id' not in item or item['id'] == None:
            item['id'] = IdGenerator.next_long()

        self._lock.acquire()
        try:
            self._items.append(item)
        finally:
            self._lock.release()

        self._logger.trace(correlation_id, "Created " + str(item))

        # Avoid reentry
        self.save(correlation_id)
        return item
    def add_correlation_id(self, correlation_id: Optional[str] = None):
        # Automatically generate short ids for now
        if correlation_id is None:
            correlation_id = IdGenerator.next_short()

        self['correlation_id'] = correlation_id