public static void Unfriend(AbstractLogger logger, INotificationQueue notificationQueue, IMixWebCallFactory mixWebCallFactory, string friendSwid, Action successCallback, Action failureCallback)
 {
     try
     {
         RemoveFriendshipRequest removeFriendshipRequest = new RemoveFriendshipRequest();
         removeFriendshipRequest.FriendUserId = friendSwid;
         RemoveFriendshipRequest request = removeFriendshipRequest;
         IWebCall <RemoveFriendshipRequest, RemoveFriendshipResponse> webCall = mixWebCallFactory.FriendshipDeletePost(request);
         webCall.OnResponse += delegate(object sender, WebCallEventArgs <RemoveFriendshipResponse> e)
         {
             RemoveFriendshipResponse     response     = e.Response;
             RemoveFriendshipNotification notification = response.Notification;
             if (NotificationValidator.Validate(notification))
             {
                 notificationQueue.Dispatch(notification, successCallback, failureCallback);
             }
             else
             {
                 logger.Critical("Failed to validate remove friendship response: " + JsonParser.ToJson(response));
                 failureCallback();
             }
         };
         webCall.OnError += delegate
         {
             failureCallback();
         };
         webCall.Execute();
     }
     catch (Exception arg)
     {
         logger.Critical("Unhandled exception: " + arg);
         failureCallback();
     }
 }
        public void Setup()
        {
            _notificationSender = new Mock <INotificationSender>();
            _notificationSender.Setup(x => x.SendAll(It.IsAny <string>()))
            .Returns(Task.Factory.StartNew(() => { }));
            _notificationSender.Setup(x => x.SendGroup(It.IsAny <string>(), It.IsAny <string>()))
            .Returns(Task.Factory.StartNew(() => { }));

            _validator = new NotificationValidator();
            _facade    = new NotificationsFacade(_notificationSender.Object, _validator);
        }
示例#3
0
        private void ValidateNotificationProperties(IEnumerable <Notification> notifications)
        {
            if (notifications == null)
            {
                throw new ArgumentNullException(nameof(notifications));
            }
            var validator = new NotificationValidator();

            foreach (var notification in notifications)
            {
                validator.ValidateAndThrow(notification);
            }
        }
 public void SetUp()
 {
     _sut = new NotificationValidator();
 }
示例#5
0
        public async Task <string> AsyncExecute(System.IO.Stream input, ILambdaContext context)
        {
            _ = input;                                                                                                // discard
            var waqiProxy = Waqi.create(_token);
            var cities    = JsonSerializer.Deserialize <List <Latincoder.AirQuality.Model.Config.City> >(_citiesRaw); // default C# serialization
            // process multiple cities
            var stationFeedsByCity = from city in cities
                                     from station in city.Stations
                                     let feed = new { Uri = $"{city.Country}/{city.Name}/{station}", CityName = city.Name }
            group feed by $"{city.Country}-{city.Name}" into stationsByCity
            select stationsByCity;

            var cityFeedsDTO = new List <CityFeed>();

            foreach (var cityFeeds in stationFeedsByCity)
            {
                var feeds = from value in cityFeeds
                            select value.Uri;
                var stationFeedsObtained = new List <WaqiCityFeed>();
                foreach (var feed in feeds)
                {
                    System.Console.WriteLine($"Requesting feed for:{feed}");
                    string mutableString = string.Empty;
                    try {
                        mutableString = await waqiProxy.getCityFeed(feed);

                        if (mutableString != string.Empty)
                        {
                            System.Console.WriteLine("found this result:\n");
                            System.Console.WriteLine(mutableString);
                            var wcf = JsonSerializer.Deserialize <WaqiCityFeed>(mutableString);
                            stationFeedsObtained.Add(wcf);
                        }
                    } catch (Exception e) { System.Console.WriteLine(e); }
                }
                try {
                    cityFeedsDTO.Add(CityFeed.From(stationFeedsObtained));
                } catch (Exception e) { System.Console.WriteLine(e); }
            }

            var aqiTable             = Table.LoadTable(_dynamoClient, _AqiTableName);
            var notificationIsUrgent = NotificationValidator.CreateyValidator();

            notificationIsUrgent.AddRules(Rules.AqiAboveUnhealthyThreshold);
            // store in DynamoDB if meets notification criteria
            foreach (var feed in cityFeedsDTO)
            {
                // Gather previous and new feeds
                var prevFeedDoc = await aqiTable.GetItemAsync(CityFeedDocument.PartitionKeyName);

                var newFeed = prevFeedDoc != null?
                              CityFeedDocument.From(feed, prevFeedDoc[CityFeedDocument.FieldMaxAqi].AsInt()) :
                                  CityFeedDocument.From(feed);

                // if no previous stuff
                if (prevFeedDoc == null)
                {
                    Console.WriteLine($"FIRST TIME: Store in dynamo now for {feed.CityName}");
                    await aqiTable.PutItemAsync(newFeed);

                    continue;
                }

                var prevFeed = CityFeedDocument.ToDTO(prevFeedDoc);

                // each feed will has a custom validator with it's own criteria since this is done per city
                var notificationIsNotSpam = NotificationValidator.CreateyValidator();
                // spam means max aqi changed at least by 5 points, and is at least 20 minutes apart frm last notification
                notificationIsNotSpam.AddRules(
                    Rules.MinutesApartFrom(prevFeed, 30),
                    Rules.AbsoluteAqiChangedBy(prevFeed, 5));
                // urgent or meets criteria
                if (notificationIsUrgent.MeetsGlobalCriteria(feed) && notificationIsNotSpam.MeetsGlobalCriteria(feed))
                {
                    Console.WriteLine($"Store in dynamo now for {feed.CityName}");
                    await aqiTable.PutItemAsync(newFeed);
                }
                else
                {
                    Console.WriteLine("Does not meet rules...");
                }
            }

            return("done");
        }