Example #1
0
        static void Main(string[] args)
        {
            string projectId   = "bookshelf-dotnet";
            string topicName   = "book-process-queue";
            string topicPath   = $"projects/{projectId}/topics/{topicName}";
            var    db          = DatastoreDb.Create(projectId);
            var    credentials = Google.Apis.Auth.OAuth2.GoogleCredential.
                                 GetApplicationDefaultAsync().Result;

            if (credentials.IsCreateScopedRequired)
            {
                credentials = credentials.CreateScoped(new[] { PubsubService.Scope.Pubsub });
            }
            PubsubService pubsub = new PubsubService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credentials,
            });

            try
            {
                pubsub.Projects.Topics.Create(new Topic()
                {
                    Name = topicPath
                }, topicPath)
                .Execute();
                Console.WriteLine($"Created topic {topicPath}.");
            }
            catch (Google.GoogleApiException e) when(e.Error.Code == 409)
            {
                Console.WriteLine($"The topic {topicPath} already exists.");
            }
        }
 public BookDetailLookup(string projectId, ILoggerFactory loggerFactory,
     Options options = null)
 {
     options = options ?? new Options();
     _loggerFactory = loggerFactory;
     _topicPath = $"projects/{projectId}/topics/{options.TopicName}";
     _subscriptionPath = $"projects/{projectId}/subscriptions/{options.SubscriptionName}";
     var credentials = Google.Apis.Auth.OAuth2.GoogleCredential.GetApplicationDefaultAsync()
         .Result;
     credentials = credentials.CreateScoped(new[] { PubsubService.Scope.Pubsub });
     _pubsub = new PubsubService(new BaseClientService.Initializer()
     {
         HttpClientInitializer = credentials,
     });
 }
Example #3
0
        public Test()
        {
            _httpMessageHandler = new MockMessageHandler();
            _logStream          = new MemoryStream();
            var init = Shouter.Initializer.CreateDefault();

            init.LogWriter         = CreateLogWriter(_logStream);
            init.HttpClient        = new HttpClient(_httpMessageHandler);
            _pubsub                = init.PubsubService;
            init.SubscriptionName += "-test";
            init.ProjectId         = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
            _topicPath             = $"projects/{init.ProjectId}/topics/{init.SubscriptionName}-topic";
            _subscriptionPath      = $"projects/{init.ProjectId}/subscriptions/{init.SubscriptionName}";
            CreateTopicAndSubscriptionOnce();
            ClearSubscription();
            _shouter = new Shouter(init);
        }
Example #4
0
        public async Task TestDeleteSubscriptionAsyncSuccess()
        {
            var           responses = new[] { new Empty() };
            PubsubService service   = GetMockedService(
                (PubsubService s) => s.Projects,
                p => p.Subscriptions,
                s => s.Delete(It.IsAny <string>()),
                responses);
            var sourceUnderTest = new PubsubDataSource(service, ProjectName);

            await sourceUnderTest.DeleteSubscriptionAsync(SubscriptionFullName);

            var subscriptionsMock = Mock.Get(service.Projects.Subscriptions);

            subscriptionsMock.Verify(s => s.Delete(SubscriptionFullName), Times.Once);
            subscriptionsMock.Verify(s => s.Delete(It.IsNotIn(SubscriptionFullName)), Times.Never);
        }
Example #5
0
        public async Task TestDeleteTopicAsyncSuccess()
        {
            var           responses = new[] { new Empty() };
            PubsubService service   = GetMockedService(
                (PubsubService s) => s.Projects,
                p => p.Topics,
                t => t.Delete(It.IsAny <string>()),
                responses);
            var sourceUnderTest = new PubsubDataSource(service, ProjectName);

            await sourceUnderTest.DeleteTopicAsync(TopicFullName);

            var topicMock = Mock.Get(service.Projects.Topics);

            topicMock.Verify(t => t.Delete(TopicFullName), Times.Once);
            topicMock.Verify(t => t.Delete(It.IsNotIn(TopicFullName)), Times.Never);
        }
 public BookDetailLookup(string projectId, Options options = null)
 {
     options = options ?? new Options();
     // [START pubsubpaths]
     _topicPath = $"projects/{projectId}/topics/{options.TopicName}";
     _subscriptionPath = $"projects/{projectId}/subscriptions/{options.SubscriptionName}";
     // [END pubsubpaths]
     var credentials = Google.Apis.Auth.OAuth2.GoogleCredential.GetApplicationDefaultAsync()
         .Result;
     if (credentials.IsCreateScopedRequired)
     {
         credentials = credentials.CreateScoped(new[] { PubsubService.Scope.Pubsub });
     }
     _pubsub = new PubsubService(new BaseClientService.Initializer()
     {
         HttpClientInitializer = credentials,
     });
 }
Example #7
0
        public async Task TestNewTopicAsyncSuccess()
        {
            var responses = new[] { new Topic {
                                        Name = MockedTopicName
                                    } };
            PubsubService service = GetMockedService(
                (PubsubService s) => s.Projects,
                p => p.Topics,
                t => t.Create(It.IsAny <Topic>(), It.IsAny <string>()),
                responses);
            var sourceUnderTest = new PubsubDataSource(service, ProjectName);

            var topic = await sourceUnderTest.NewTopicAsync(TopicName);

            Assert.AreEqual(MockedTopicName, topic.Name);
            var topicMock = Mock.Get(service.Projects.Topics);

            topicMock.Verify(t => t.Create(It.IsAny <Topic>(), TopicFullName), Times.Once);
            topicMock.Verify(t => t.Create(It.IsAny <Topic>(), It.IsNotIn(TopicFullName)), Times.Never);
        }
Example #8
0
        public BookDetailLookup(string projectId, Options options = null, ISimpleLogger logger = null)
        {
            options = options ?? new Options();
            _logger = logger ?? new DebugLogger();
            // [START pubsubpaths]
            _topicPath        = $"projects/{projectId}/topics/{options.TopicName}";
            _subscriptionPath = $"projects/{projectId}/subscriptions/{options.SubscriptionName}";
            // [END pubsubpaths]
            var credentials = Google.Apis.Auth.OAuth2.GoogleCredential.GetApplicationDefaultAsync()
                              .Result;

            if (credentials.IsCreateScopedRequired)
            {
                credentials = credentials.CreateScoped(new[] { PubsubService.Scope.Pubsub });
            }
            _pubsub = new PubsubService(new BaseClientService.Initializer()
            {
                HttpClientInitializer = credentials,
            });
        }
Example #9
0
        public async Task TestNewSubscriptionAsyncSuccess()
        {
            var responses = new[] { new Subscription {
                                        Name = MockedSubscriptionName, Topic = MockedTopicName
                                    } };
            PubsubService service = GetMockedService(
                (PubsubService s) => s.Projects,
                p => p.Subscriptions,
                s => s.Create(It.IsAny <Subscription>(), It.IsAny <string>()),
                responses);
            var sourceUnderTest = new PubsubDataSource(service, ProjectName);

            var subscription = await sourceUnderTest.NewSubscriptionAsync(s_newSubscription);

            Assert.AreEqual(MockedSubscriptionName, subscription.Name);
            Assert.AreEqual(MockedTopicName, subscription.Topic);
            var subscriptionMock = Mock.Get(service.Projects.Subscriptions);

            subscriptionMock.Verify(s => s.Create(It.IsAny <Subscription>(), SubscriptionFullName), Times.Once);
            subscriptionMock.Verify(s => s.Create(It.IsAny <Subscription>(), It.IsNotIn(SubscriptionFullName)), Times.Never);
        }
Example #10
0
        public async Task TestGetTopicListAsyncMultiPage()
        {
            var responses = new[]
            {
                new ListTopicsResponse
                {
                    NextPageToken = "Token",
                    Topics        = new List <Topic> {
                        new Topic {
                            Name = FirstName
                        }
                    }
                },
                new ListTopicsResponse
                {
                    Topics = new List <Topic> {
                        new Topic {
                            Name = SecondName
                        }
                    }
                }
            };
            PubsubService service = GetMockedService(
                (PubsubService s) => s.Projects,
                p => p.Topics,
                t => t.List(It.IsAny <string>()),
                responses);

            var           sourceUnderTest = new PubsubDataSource(service, ProjectName);
            IList <Topic> topics          = await sourceUnderTest.GetTopicListAsync();

            Assert.AreEqual(2, topics.Count);
            Assert.AreEqual(FirstName, topics[0].Name);
            Assert.AreEqual(SecondName, topics[1].Name);
            var topicsMock = Mock.Get(service.Projects.Topics);

            topicsMock.Verify(t => t.List(ProjectResourceName), Times.AtLeastOnce);
            topicsMock.Verify(t => t.List(It.IsNotIn(ProjectResourceName)), Times.Never);
        }
Example #11
0
        /// <summary>
        /// Adds one or more messages to the topic. Returns NOT_FOUND if the topic doesnot exist.
        /// Documentation https://developers.google.com/pubsub/v1beta1a/reference/topics/publishBatch
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Pubsub service.</param>
        /// <param name="body">A valid Pubsub v1beta1a body.</param>
        /// <returns>PublishBatchResponseResponse</returns>
        public static PublishBatchResponse PublishBatch(PubsubService service, PublishBatchRequest body)
        {
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }
                if (body == null)
                {
                    throw new ArgumentNullException("body");
                }

                // Make the request.
                return(service.Topics.PublishBatch(body).Execute());
            }
            catch (Exception ex)
            {
                throw new Exception("Request Topics.PublishBatch failed.", ex);
            }
        }
Example #12
0
        /// <summary>
        /// Deletes the topic with the given name. Returns NOT_FOUND if the topic doesnot exist. After a topic is deleted, a new topic may be created with thesame name.
        /// Documentation https://developers.google.com/pubsub/v1beta1a/reference/topics/delete
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Pubsub service.</param>
        /// <param name="topic">Name of the topic to delete.</param>
        /// <returns>EmptyResponse</returns>
        public static Empty Delete(PubsubService service, string topic)
        {
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }
                if (topic == null)
                {
                    throw new ArgumentNullException(topic);
                }

                // Make the request.
                return(service.Topics.Delete(topic).Execute());
            }
            catch (Exception ex)
            {
                throw new Exception("Request Topics.Delete failed.", ex);
            }
        }
Example #13
0
        /// <summary>
        /// Modifies the Ack deadline for a message received from a pull request.
        /// Documentation https://developers.google.com/pubsub/v1beta1a/reference/subscriptions/modifyAckDeadline
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Pubsub service.</param>
        /// <param name="body">A valid Pubsub v1beta1a body.</param>
        /// <returns>EmptyResponse</returns>
        public static Empty ModifyAckDeadline(PubsubService service, ModifyAckDeadlineRequest body)
        {
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }
                if (body == null)
                {
                    throw new ArgumentNullException("body");
                }

                // Make the request.
                return(service.Subscriptions.ModifyAckDeadline(body).Execute());
            }
            catch (Exception ex)
            {
                throw new Exception("Request Subscriptions.ModifyAckDeadline failed.", ex);
            }
        }
Example #14
0
        /// <summary>
        /// Gets the configuration details of a subscription.
        /// Documentation https://developers.google.com/pubsub/v1beta1a/reference/subscriptions/get
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Pubsub service.</param>
        /// <param name="subscription">The name of the subscription to get.</param>
        /// <returns>SubscriptionResponse</returns>
        public static Subscription Get(PubsubService service, string subscription)
        {
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }
                if (subscription == null)
                {
                    throw new ArgumentNullException(subscription);
                }

                // Make the request.
                return(service.Subscriptions.Get(subscription).Execute());
            }
            catch (Exception ex)
            {
                throw new Exception("Request Subscriptions.Get failed.", ex);
            }
        }
Example #15
0
        public async Task TestGetSubscriptionListAsyncPaged()
        {
            var responses = new[]
            {
                new ListSubscriptionsResponse
                {
                    NextPageToken = "Token",
                    Subscriptions = new List <Subscription> {
                        new Subscription {
                            Name = FirstName
                        }
                    }
                },
                new ListSubscriptionsResponse
                {
                    Subscriptions = new List <Subscription> {
                        new Subscription {
                            Name = SecondName
                        }
                    }
                }
            };
            PubsubService service = GetMockedService(
                (PubsubService s) => s.Projects,
                p => p.Subscriptions,
                s => s.List(It.IsAny <string>()),
                responses);
            var sourceUnderTest = new PubsubDataSource(service, ProjectName);

            IList <Subscription> subscriptions = await sourceUnderTest.GetSubscriptionListAsync();

            Assert.AreEqual(2, subscriptions.Count);
            Assert.AreEqual(FirstName, subscriptions[0].Name);
            Assert.AreEqual(SecondName, subscriptions[1].Name);
            var subscriptionsMock = Mock.Get(service.Projects.Subscriptions);

            subscriptionsMock.Verify(s => s.List(ProjectResourceName), Times.AtLeastOnce);
            subscriptionsMock.Verify(s => s.List(It.IsNotIn(ProjectResourceName)), Times.Never);
        }
Example #16
0
        public async Task TestDeleteSubscriptionAsyncException()
        {
            var           responses = new Empty[0];
            PubsubService service   = GetMockedService(
                (PubsubService s) => s.Projects,
                p => p.Subscriptions,
                s => s.Delete(It.IsAny <string>()),
                responses);
            var sourceUnderTest = new PubsubDataSource(service, ProjectName);

            try
            {
                await sourceUnderTest.DeleteSubscriptionAsync(SubscriptionFullName);

                Assert.Fail();
            }
            finally
            {
                var subscriptionMock = Mock.Get(service.Projects.Subscriptions);
                subscriptionMock.Verify(s => s.Delete(SubscriptionFullName), Times.Once);
                subscriptionMock.Verify(s => s.Delete(It.IsNotIn(SubscriptionFullName)), Times.Never);
            }
        }
Example #17
0
        public async Task TestGetSubscriptionListAsyncException()
        {
            var           responses = new ListSubscriptionsResponse[0];
            PubsubService service   = GetMockedService(
                (PubsubService s) => s.Projects,
                p => p.Subscriptions,
                s => s.List(It.IsAny <string>()),
                responses);
            var sourceUnderTest = new PubsubDataSource(service, ProjectName);

            try
            {
                await sourceUnderTest.GetSubscriptionListAsync();

                Assert.Fail();
            }
            finally
            {
                var subscriptionsMock = Mock.Get(service.Projects.Subscriptions);
                subscriptionsMock.Verify(s => s.List(ProjectResourceName), Times.AtLeastOnce);
                subscriptionsMock.Verify(s => s.List(It.IsNotIn(ProjectResourceName)), Times.Never);
            }
        }
Example #18
0
        public async Task TestNewTopicAsyncException()
        {
            var           responses = new Topic[0];
            PubsubService service   = GetMockedService(
                (PubsubService s) => s.Projects,
                p => p.Topics,
                t => t.Create(It.IsAny <Topic>(), It.IsAny <string>()),
                responses);
            var sourceUnderTest = new PubsubDataSource(service, ProjectName);

            try
            {
                await sourceUnderTest.NewTopicAsync(TopicName);

                Assert.Fail();
            }
            finally
            {
                var topicMock = Mock.Get(service.Projects.Topics);
                topicMock.Verify(t => t.Create(It.IsAny <Topic>(), TopicFullName), Times.Once);
                topicMock.Verify(t => t.Create(It.IsAny <Topic>(), It.IsNotIn(TopicFullName)), Times.Never);
            }
        }
Example #19
0
        /// <summary>
        /// Lists matching topics.
        /// Documentation https://developers.google.com/pubsub/v1beta1a/reference/topics/list
        /// Generation Note: This does not always build corectly.  Google needs to standardise things I need to figuer out which ones are wrong.
        /// </summary>
        /// <param name="service">Authenticated Pubsub service.</param>
        /// <param name="optional">Optional paramaters.</param>
        /// <returns>ListTopicsResponseResponse</returns>
        public static ListTopicsResponse List(PubsubService service, TopicsListOptionalParms optional = null)
        {
            try
            {
                // Initial validation.
                if (service == null)
                {
                    throw new ArgumentNullException("service");
                }

                // Building the initial request.
                var request = service.Topics.List();

                // Applying optional parameters to the request.
                request = (TopicsResource.ListRequest)SampleHelpers.ApplyOptionalParms(request, optional);

                // Requesting data.
                return(request.Execute());
            }
            catch (Exception ex)
            {
                throw new Exception("Request Topics.List failed.", ex);
            }
        }
 public Test()
 {
     _httpMessageHandler = new MockMessageHandler();
     _logStream = new MemoryStream();
     var init = Shouter.Initializer.CreateDefault();
     init.LogWriter = CreateLogWriter(_logStream);
     init.HttpClient = new HttpClient(_httpMessageHandler);
     _pubsub = init.PubsubService;
     init.SubscriptionName += "-test";
     init.ProjectId = Environment.GetEnvironmentVariable("GOOGLE_PROJECT_ID");
     _topicPath = $"projects/{init.ProjectId}/topics/{init.SubscriptionName}-topic";
     _subscriptionPath = $"projects/{init.ProjectId}/subscriptions/{init.SubscriptionName}";
     CreateTopicAndSubscriptionOnce();
     ClearSubscription();
     _shouter = new Shouter(init);
 }
Example #21
0
 /// <summary>
 /// Constructor.
 /// </summary>
 public PullWatchedCreativesSubscription()
 {
     pubSubService = Utilities.GetPubSubService();
 }
 public GcpsCmdlet()
 {
     Service = new PubsubService(GetBaseClientServiceInitializer());
 }
Example #23
0
 public GcpsCmdlet()
 {
     Service = new PubsubService(GetBaseClientServiceInitializer());
 }