private void EnsureETag(BlockchainExplorer explorer)
 {
     if (string.IsNullOrEmpty(explorer.ETag))
     {
         explorer.ETag = DateTime.UtcNow.ToString();
     }
 }
        public Task CreateAsync(BlockchainExplorer explorer)
        {
            EnsureETag(explorer);
            Explorers.Add(explorer);

            return(Task.CompletedTask);
        }
        /// <inheritdoc/>
        public async Task UpdateAsync(BlockchainExplorer settings)
        {
            await _blockchainExplorersService.UpdateAsync(settings);

            await _distributedCache.RemoveAsync(_explorersCacheKey);

            await _distributedCache.RemoveAsync(GetCacheKey(settings.BlockchainType));
        }
        /// <inheritdoc />
        public async Task CreateAsync(BlockchainExplorer explorer)
        {
            var existing = await GetBlockchainExplorerEntity(explorer.BlockchainType, explorer.RecordId);

            if (existing != null)
                throw new AlreadyExistsException($"Setting with type {explorer.BlockchainType} is already exists");

            BlockchainExplorerEntity entity = BlockchainExplorerEntity.FromDomain(explorer);

            await _table.InsertAsync(entity);
        }
コード例 #5
0
 private BlockchainExplorerResponse MapToResponse(BlockchainExplorer domainModel)
 {
     return(new BlockchainExplorerResponse()
     {
         ETag = domainModel.ETag,
         BlockchainType = domainModel.BlockchainType,
         ExplorerUrlTemplate = domainModel.ExplorerUrlTemplate,
         RecordId = domainModel.RecordId,
         Name = domainModel.Name
     });
 }
コード例 #6
0
 public static BlockchainExplorerEntity FromDomain(BlockchainExplorer explorer)
 {
     return(new BlockchainExplorerEntity()
     {
         RecordId = explorer.RecordId,
         BlockchainType = explorer.BlockchainType,
         ExplorerUrlTemplate = explorer.ExplorerUrlTemplate,
         ETag = explorer.ETag,
         PartitionKey = GetPartitionKey(explorer.BlockchainType),
         RowKey = GetRowKey(explorer.RecordId),
         Name = explorer.Name
     });
 }
        public Task UpdateAsync(BlockchainExplorer explorer)
        {
            EnsureETag(explorer);
            var existingExplorer = Explorers.FirstOrDefault(x => x.BlockchainType == explorer.BlockchainType &&
                                                            x.RecordId == explorer.RecordId);

            if (explorer == null)
            {
                throw new DoesNotExistException($"");
            }

            Explorers.Remove(existingExplorer);
            Explorers.Add(explorer);

            return(Task.CompletedTask);
        }
コード例 #8
0
        public static void ReInitBlockchainRepository()
        {
            var blockchainExplorer = new BlockchainExplorer()
            {
                BlockchainType      = "EthereumClassic",
                ETag                = DateTime.UtcNow.ToString(),
                RecordId            = Guid.NewGuid().ToString(),
                ExplorerUrlTemplate = "https://some-blockchain-explorer.bit/{tx-hash}",
                Name                = "Ropsten"
            };

            var explorers = new List <BlockchainExplorer>()
            {
                blockchainExplorer
            };

            _blockchainExplorersRepository.Explorers.Clear();
            _blockchainExplorersRepository.Explorers.AddRange(explorers);
        }
コード例 #9
0
        public async Task <IActionResult> CreateAsync([FromBody] BlockchainExplorerCreateRequest request)
        {
            BlockchainExplorer explorer = MapToDomain(request);

            try
            {
                await _blockchainExplorersServiceCached.CreateAsync(explorer);
            }
            catch (NotValidException e)
            {
                return(CreateContentResult(StatusCodes.Status400BadRequest, e.Message));
            }
            catch (AlreadyExistsException e)
            {
                return(CreateContentResult(StatusCodes.Status409Conflict, e.Message));
            }

            return(Ok());
        }
        /// <inheritdoc />
        public async Task UpdateAsync(BlockchainExplorer explorer)
        {
            BlockchainExplorerEntity entity = BlockchainExplorerEntity.FromDomain(explorer);

            string partitionKey = BlockchainExplorerEntity.GetPartitionKey(explorer.BlockchainType);
            string rowKey = BlockchainExplorerEntity.GetRowKey(explorer.RecordId);
            string errorMessage = null;
            bool isUpdated = false;

            try
            {
                isUpdated = await _table.InsertOrModifyAsync(partitionKey, rowKey, () => entity, model =>
                {
                    if (model.ETag != entity.ETag)
                    {
                        errorMessage = $"Entity with type {model.BlockchainType} has eTag == {model.ETag}, eTag in update request is {entity.ETag}";

                        return false;
                    }

                    model.BlockchainType = entity.BlockchainType;
                    model.ExplorerUrlTemplate = entity.ExplorerUrlTemplate;
                    model.RecordId = entity.RecordId;
                    model.ETag = entity.ETag;
                    model.PartitionKey = entity.PartitionKey;
                    model.RowKey = entity.RowKey;
                    model.Name = entity.Name;

                    return true;
                });
            }
            catch (Exception e)
            {
                throw new DoesNotExistException(e.Message);
            }

            if (!isUpdated)
                throw new AlreadyUpdatedException(errorMessage);
        }
        /*
         *  One url pattern per line
         *  Absolute and valid url should be specified for each pattern
         *  {tx-hash} placeholder is required for each url
         *  Zero number of patterns are allowed
         *  ?Empty lines are ignored
         *  ?Starting/trailing whitespaces in the line are ignored
         */
        private void ThrowOnNotValidBlockchainExplorer(BlockchainExplorer explorer)
        {
            if (explorer == null)
            {
                throw new NotValidException($"Explorer should not be null");
            }

            bool isValidUrl = Uri.TryCreate(explorer.ExplorerUrlTemplate, UriKind.Absolute, out _);

            if (!isValidUrl)
            {
                throw new NotValidException($"{nameof(explorer.ExplorerUrlTemplate)} is not valid Url template");
            }

            var matches = _templateRegex.Matches(explorer.ExplorerUrlTemplate);

            if (matches.Count != 1)
            {
                throw new NotValidException($"{nameof(explorer.ExplorerUrlTemplate)} should contain one " +
                                            $"{Lykke.Service.BlockchainSettings.Contract.Constants.TxHashTemplate}");
            }

            //var urlWithoutTempolate = explorer.ExplorerUrlTemplate.Replace(_txHashTemplate, "");
        }
 ///<inheritdoc/>
 public async Task UpdateAsync(BlockchainExplorer explorer)
 {
     Trim(explorer);
     ThrowOnNotValidBlockchainExplorer(explorer);
     await _blockchainExplorersRepository.UpdateAsync(explorer);
 }
 private void Trim(BlockchainExplorer explorer)
 {
     explorer.ExplorerUrlTemplate = explorer.ExplorerUrlTemplate.Trim();
 }