コード例 #1
0
        public async Task <SubscriptionDto> SubscribeToAsync(string subject, CancellationToken cancellationToken = default)
        {
            var channel                   = Environment.GetEnvironmentVariable("CHANNEL");
            var podName                   = Environment.GetEnvironmentVariable("POD_NAME");
            var podNamespace              = Environment.GetEnvironmentVariable("POD_NAMESPACE");
            var subscriberUri             = new Uri($"http://{podName}.{podNamespace}.svc.cluster.local/events");
            var createSubscriptionCommand = new CreateSubscriptionCommandDto
            {
                Subject     = subject,
                Channel     = channel,
                Subscribers = new List <Uri>()
                {
                    subscriberUri
                }
            };
            SubscriptionDto subscription;

            using (HttpResponseMessage response = await this.HttpClient.PostAsJsonAsync("sub", createSubscriptionCommand))
            {
                if (!response.IsSuccessStatusCode)
                {
                    var content = await response?.Content.ReadAsStringAsync();

                    throw new OperationException($"The remote server responded with a non-success status code '{response.StatusCode}'.{Environment.NewLine}Details: {content}");
                }
                subscription = JsonConvert.DeserializeObject <SubscriptionDto>(await response?.Content.ReadAsStringAsync());
                this.Logger.LogInformation("A cloud event subscription to subject '{subject}' has been successfully created", subject);
            }
            return(subscription);
        }
コード例 #2
0
 public async Task <IActionResult> Sub([FromBody] CreateSubscriptionCommandDto command)
 {
     if (!this.ModelState.IsValid)
     {
         return(this.BadRequest(this.ModelState));
     }
     return(this.Process(await this.Mediator.Send(this.Mapper.Map <CreateSubscriptionCommand>(command)), (int)HttpStatusCode.Created));
 }