| | | 1 | | using Google.Cloud.PubSub.V1; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | |
| | | 4 | | namespace AsyncResponse.Transports.GooglePubSub; |
| | | 5 | | |
| | 3 | 6 | | internal sealed class GooglePubSubReplyTargetProvider( |
| | 3 | 7 | | IOptions<GooglePubSubAsyncResponseOptions> _options) : IAsyncResponseReplyTargetProvider |
| | | 8 | | { |
| | | 9 | | /// <summary>Gets the configured reply target.</summary> |
| | | 10 | | public AsyncResponseReplyTarget GetReplyTarget(string? name = null) |
| | | 11 | | { |
| | 3 | 12 | | var options = _options.Value; |
| | 3 | 13 | | var targetName = string.IsNullOrWhiteSpace(name) |
| | 3 | 14 | | ? options.DefaultReplyTargetName |
| | 3 | 15 | | : name; |
| | | 16 | | |
| | 3 | 17 | | var target = ResolveTarget(options, targetName); |
| | 3 | 18 | | var projectId = GooglePubSubOptionsValidator.Required( |
| | 3 | 19 | | target.ProjectId ?? options.ProjectId, |
| | 3 | 20 | | $"{nameof(GooglePubSubReplyTargetOptions)}.{nameof(GooglePubSubReplyTargetOptions.ProjectId)}"); |
| | 3 | 21 | | var topicId = GooglePubSubOptionsValidator.Required( |
| | 3 | 22 | | target.TopicId, |
| | 3 | 23 | | $"{nameof(GooglePubSubReplyTargetOptions)}.{nameof(GooglePubSubReplyTargetOptions.TopicId)}"); |
| | | 24 | | |
| | 3 | 25 | | var properties = new Dictionary<string, string>(target.Properties, StringComparer.Ordinal) |
| | 3 | 26 | | { |
| | 3 | 27 | | ["projectId"] = projectId, |
| | 3 | 28 | | ["topicId"] = topicId |
| | 3 | 29 | | }; |
| | | 30 | | |
| | 3 | 31 | | return new AsyncResponseReplyTarget |
| | 3 | 32 | | { |
| | 3 | 33 | | Name = targetName, |
| | 3 | 34 | | Transport = GooglePubSubAsyncResponseOptions.TransportName, |
| | 3 | 35 | | Address = TopicName.FromProjectTopic(projectId, topicId).ToString(), |
| | 3 | 36 | | Properties = properties |
| | 3 | 37 | | }; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | private static GooglePubSubReplyTargetOptions ResolveTarget( |
| | | 41 | | GooglePubSubAsyncResponseOptions options, |
| | | 42 | | string targetName) |
| | | 43 | | { |
| | 3 | 44 | | if (options.ReplyTargets.TryGetValue(targetName, out var configured)) |
| | 3 | 45 | | return configured; |
| | | 46 | | |
| | 3 | 47 | | if (StringComparer.Ordinal.Equals(targetName, options.DefaultReplyTargetName) |
| | 3 | 48 | | && !string.IsNullOrWhiteSpace(options.ResponseTopicId)) |
| | | 49 | | { |
| | 3 | 50 | | return new GooglePubSubReplyTargetOptions |
| | 3 | 51 | | { |
| | 3 | 52 | | ProjectId = options.ProjectId, |
| | 3 | 53 | | TopicId = options.ResponseTopicId |
| | 3 | 54 | | }; |
| | | 55 | | } |
| | | 56 | | |
| | 2 | 57 | | throw new InvalidOperationException( |
| | 2 | 58 | | $"Google Pub/Sub async-response reply target '{targetName}' is not configured. " + |
| | 2 | 59 | | $"Configure {nameof(GooglePubSubAsyncResponseOptions.ResponseTopicId)} for the default target " + |
| | 2 | 60 | | $"or add a named target with {nameof(GooglePubSubAsyncResponseOptions.AddReplyTarget)}."); |
| | | 61 | | } |
| | | 62 | | } |