Exemplo n.º 1
0
        public Task SendMessage <T>(string queueName, T message)
            where T : class
        {
            var client = new QueueClient(connectionString, queueName);

            return(client.SendAsync(new Message(Encoding.UTF8.GetBytes(json.Dump(message)))));
        }
Exemplo n.º 2
0
        public Task CreateItem <T>(string key, T item, TimeSpan ttl)
            where T : class
        {
            var cache = client.GetDatabase();

            return(cache.StringSetAsync(key, json.Dump(item), ttl));
        }
        public async Task SendMessages <T>(IEnumerable <T> messages, CancellationToken cancellationToken)
            where T : class
        {
            var batch = await client.CreateBatchAsync();

            foreach (var message in messages)
            {
                var outputMessage = json.Dump <T>(message);

                if (!batch.TryAdd(new EventData(Encoding.UTF8.GetBytes(outputMessage))))
                {
                    throw new InvalidOperationException($"Unable to add message {outputMessage} to batch");
                }
            }

            await client.SendAsync(batch, cancellationToken);
        }
Exemplo n.º 4
0
        private async Task <IReadOnlyCollection <T> > Delete <T>(string userId, IReadOnlyCollection <T> items, CancellationToken cancellationToken)
            where T : class, IHasId
        {
            if (items.Count == 0)
            {
                return(Array.Empty <T>());
            }

            var bundle = new Bundle <T>
            {
                ResourceType = "Bundle",
                Type         = "batch",
                Entries      = items.Select(item => new BundleEntry <T>
                {
                    Request = new BundleRequest
                    {
                        Method = "DELETE",
                        Url    = $"{item.ResourceType}/{item.Id}?hardDelete=true",
                    },
                }).ToArray(),
            };

            var request = new HttpRequestMessage
            {
                RequestUri = new Uri($"{fhirUrl}/"),
                Method     = HttpMethod.Post,
                Content    = new StringContent(json.Dump(bundle), Encoding.UTF8, "application/json"),
            };

            await AddHeaders(request, userId, cancellationToken);

            log.LogInformation("Deleting bundle with {count} items for user {userId}", bundle.Entries.Length, userId);

            var deletedBundle = await http.Send <Bundle <T>, FhirApiException>(request, cancellationToken);

            var deletedItems = deletedBundle.Entries.Zip(items)
                               .Where(t => t.First.Response.Status == "204" && t.First.Response.ETag != null)
                               .Select(t => t.Second)
                               .ToArray();

            log.LogInformation("Deleted {count} items in bundle for user {userId}", deletedItems.Length, userId);

            return(deletedItems);
        }