public static async Task <bool> PublishContents(Input input) { var client = ServerlessHelper.GetDbContext(); foreach (var content in input.Contents) { var response = client.QueryAsync <ContentTable>(content.Key); var items = await response.GetRemainingAsync(); var message = new { content, message = input.Message }; var sqsClient = ServerlessHelper.GetAmazonSqsClient(); foreach (var item in items) { if (item.Key.Equals(content.Key, StringComparison.CurrentCultureIgnoreCase)) { if (CheckConditions(item.Condition, content.Value, item.Value)) { await sqsClient.SendMessageAsync(item.QueueUrl, JsonConvert.SerializeObject(message)); } } } } return(true); }
private static async Task <Subscriber> CreateQueue(string queueName) { var createQueueRequest = new CreateQueueRequest(); createQueueRequest.QueueName = queueName; var attrs = new Dictionary <string, string>(); var policy = new Policy { Statements = new List <Statement> { new Statement(Statement.StatementEffect.Allow) { Principals = new List <Principal>() { Principal.AllUsers }, Actions = new List <ActionIdentifier>() { SQSActionIdentifiers.AllSQSActions } } } }; attrs.Add(QueueAttributeName.VisibilityTimeout, "10"); attrs.Add(QueueAttributeName.Policy, policy.ToJson()); createQueueRequest.Attributes = attrs; var sqsClient = ServerlessHelper.GetAmazonSqsClient(); var response = await sqsClient.CreateQueueAsync(createQueueRequest); return(new Subscriber { SubscriberId = response.QueueUrl, QueueUrl = response.QueueUrl }); }
public static async Task PublishTopics(Input input) { var functionInvoked = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"); var client = ServerlessHelper.GetDbContext(); foreach (var topic in input.Topics) { var response = client.QueryAsync <TopicTable>(topic); var databaseAccessed = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss.fff"); var items = await response.GetRemainingAsync(); var sqsClient = ServerlessHelper.GetAmazonSqsClient(); foreach (var item in items) { var payload = new { topic, message = input.Message, fromPublisher = input.FromPublisher, functionInvoked = functionInvoked, databaseAccessed = databaseAccessed, queueUrl = item.QueueUrl }; //var content = JsonConvert.SerializeObject(payload); //var httpClient = new HttpClient { Timeout = TimeSpan.FromMinutes(1) }; //var resp = httpClient.PostAsync("https://0achmjvzf2.execute-api.eu-central-1.amazonaws.com/pubsub/publishmessage", new StringContent(content, System.Text.Encoding.UTF8, "application/json")); //LambdaContext.Logger.LogLine(resp.IsSuccessStatusCode.ToString()); //var err = await resp.Content.ReadAsStringAsync(); //LambdaContext.Logger.LogLine(err); //var credentials = new BasicAWSCredentials(Serverless.Common.Environment.AccessKey, Serverless.Common.Environment.SecretKey); //var client1 = new AmazonLambdaClient(credentials, RegionEndpoint.EUCentral1); //var request = new InvokeRequest() { FunctionName = "PublishMessage", Payload = JsonConvert.SerializeObject(payload) }; //await client1.InvokeAsync(request); //LambdaContext.Logger.LogLine(resp.StatusCode.ToString()); //LambdaContext.Logger.LogLine(resp.FunctionError.ToString()); //await ServerlessHelper.SendMessage(Newtonsoft.Json.JsonConvert.SerializeObject(payload)); await sqsClient.SendMessageAsync(item.QueueUrl, Newtonsoft.Json.JsonConvert.SerializeObject(payload)); } } }
public static async Task<bool> PublishFunctions(Input input) { var client = ServerlessHelper.GetDbContext(); var response = client.QueryAsync<FunctionsTable>(input.SubscriptionTopic); var items = await response.GetRemainingAsync(); var sqsClient = ServerlessHelper.GetAmazonSqsClient(); var httpClient = new HttpClient(); foreach (var item in items) { if (item.FunctionType == "url") { var resp = await httpClient.GetAsync(item.MatchingFunction); var responseString = await resp.Content.ReadAsStringAsync(); await sqsClient.SendMessageAsync(item.QueueUrl, JsonConvert.SerializeObject(Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(responseString))); } } return true; }
public static async Task PublishMessage(Input input) { var sqsClient = ServerlessHelper.GetAmazonSqsClient(); var message = JsonConvert.SerializeObject(input); await sqsClient.SendMessageAsync(input.QueueUrl, Newtonsoft.Json.JsonConvert.SerializeObject(message)); }