private static SimplePublisher GetGoogleBusWriter(Microsoft.Azure.WebJobs.ExecutionContext context) { string topicId = ConfigurationManager.AppSettings["GoogleTopicId"]; string projectId = ConfigurationManager.AppSettings["GoogleProjectId"]; //Google credential expected to be a local file called googleCredential.json, copied to output TopicName topicName = new TopicName(projectId, topicId); string credentialPath = Path.Combine(context.FunctionDirectory, "..\\googleCredential.json"); GoogleCredential credential = ReadGoogleCredentialFile(credentialPath); credential = credential.CreateScoped(PublisherClient.DefaultScopes); ClientCreationSettings clientSettings = new ClientCreationSettings(credentials: credential.ToChannelCredentials()); Channel channel = new Channel(PublisherClient.DefaultEndpoint.Host, PublisherClient.DefaultEndpoint.Port, credential.ToChannelCredentials()); PublisherClient client = PublisherClient.Create(channel); return(SimplePublisher.Create(topicName, new[] { client })); }
public async Task SendEvent(IHiarcEvent theEvent) { try { var serializedEvent = JsonSerializer.Serialize(theEvent); var topicName = new TopicName(_pubSubSettings.ProjectId, _pubSubSettings.Topic); var sacByteArray = Encoding.UTF8.GetBytes(_pubSubSettings.ServiceAccountCredential); var sacStream = new MemoryStream(sacByteArray); var credential = GoogleCredential.FromServiceAccountCredential(ServiceAccountCredential.FromServiceAccountData(sacStream)); var createSettings = new ClientCreationSettings(credentials: credential.ToChannelCredentials()); var publisher = await CreateAsync(topicName, clientCreationSettings : createSettings); var messageId = await publisher.PublishAsync(serializedEvent); await publisher.ShutdownAsync(TimeSpan.FromSeconds(10)); _logger.LogDebug($"Successfully sent event '{theEvent.Event}' to '{this.Name}'. Payload: {serializedEvent}"); } catch (Exception ex) { _logger.LogError($"Failed to send event '{theEvent.Event}' to '{this.Name}'. Exception: {ex.Message}"); } }