예제 #1
0
        public void Create(ApiResponse coordinates)
        {
            JsonStringContent geolocation = new JsonStringContent(coordinates.ToString());
            var key = Convert.ToString(geolocation.Value("area"));

            _database.Add(key, coordinates);
        }
예제 #2
0
        public override void Execute(JsonStringContent feedbackContent)
        {
            Subscription subscription = unitOfWork.GetByID(Convert.ToString(feedbackContent.Value("ID")));

            subscription.Active    = false;
            subscription.Status    = "Stoped";
            subscription.ExpiredAt = DateTime.UtcNow.Ticks;
            unitOfWork.Remove(Convert.ToString(feedbackContent.Value("ID")));
            unitOfWork.Add(subscription);
            unitOfWork.Commit();
            subject.Detach(observer);
        }
예제 #3
0
        public void ApiRequestTest()
        {
            var test     = "40.7484284;-73.9856546198733";
            var request  = "Empire State Building";
            var response = client.ApiRequest(request);
            var compare  = new JsonStringContent(response.ToString());

            Assert.AreEqual(Convert.ToString(compare.Value("geolocation")), test);
            Assert.AreEqual(Convert.ToString(compare.Value("area")), "New York:New York County:USA");

            var         negativeRequest = "0123456789qwerasdf";
            ApiResponse negativeResponse;

            Assert.Throws <ArgumentException>(() => negativeResponse = client.ApiRequest(negativeRequest), $"Unknown location { negativeRequest }.");
        }
예제 #4
0
        /// <summary>
        /// Updates an existing loan by modifying the values of the loan data elements passed with the optionally specified <paramref name="updateLoanOptions"/>.
        /// </summary>
        /// <param name="loan">The loan to update.</param>
        /// <param name="updateLoanOptions">The loan update options.</param>
        /// <param name="cancellationToken">The token to monitor for cancellation requests. The default value is <see cref="CancellationToken.None"/>.</param>
        /// <returns></returns>
        public async Task UpdateLoanAsync(Loan loan, UpdateLoanOptions updateLoanOptions, CancellationToken cancellationToken = default)
        {
            Preconditions.NotNull(loan, nameof(loan));
            Preconditions.NotNullOrEmpty(loan.EncompassId, $"{nameof(loan)}.{nameof(loan.EncompassId)}");

            loan.Initialize(Client, loan.EncompassId);
            HttpContent content;

            if (updateLoanOptions?.Persistent == false)
            {
                var body = loan.ToJson();
                content = new JsonStringContent(body);
            }
            else
            {
                var transientLoanUpdates = loan.TransientLoanUpdates;
                if (transientLoanUpdates?.Count > 0)
                {
                    foreach (var transientLoanUpdate in transientLoanUpdates)
                    {
                        await PatchAsync(loan.EncompassId, transientLoanUpdate.QueryString, new JsonStringContent(transientLoanUpdate.Body), nameof(UpdateLoanAsync), loan.EncompassId, cancellationToken).ConfigureAwait(false);
                    }
                    transientLoanUpdates.Clear();
                }
                content = JsonStreamContent.Create(loan);
            }
            await PatchAsync(loan.EncompassId, updateLoanOptions?.ToQueryParameters().ToString(), content, nameof(UpdateLoanAsync), loan.EncompassId, cancellationToken, async (HttpResponseMessage response) =>
            {
                if (updateLoanOptions?.Populate == true)
                {
                    await response.Content.PopulateAsync(loan).ConfigureAwait(false);
                }
                loan.Dirty = false;
                if (updateLoanOptions?.Persistent == false)
                {
                    var transientLoanUpdates = loan.TransientLoanUpdates;
                    if (transientLoanUpdates == null)
                    {
                        transientLoanUpdates      = new List <Loan.TransientLoanUpdate>();
                        loan.TransientLoanUpdates = transientLoanUpdates;
                    }
                    transientLoanUpdates.Add(new Loan.TransientLoanUpdate {
                        Body = ((JsonStringContent)content).Json, QueryString = updateLoanOptions.ToQueryParameters(true).ToString()
                    });
                }
                return(string.Empty);
            }).ConfigureAwait(false);
        }
예제 #5
0
        public override void Execute(JsonStringContent feedbackContent)
        {
            subject.Attach(observer);
            Subscription subscription = new Subscription();

            subscription.ID              = Convert.ToString(feedbackContent.Value("ID"));
            subscription.UserID          = Convert.ToString(feedbackContent.Value("UserID"));
            subscription.Location        = Convert.ToString(feedbackContent.Value("Location"));
            subscription.RequestsPerHour = Convert.ToInt32(feedbackContent.Value("ResponsesPerHour"));
            subscription.Active          = true;
            subscription.Status          = "Started";
            subscription.CreatedAt       = DateTime.UtcNow.Ticks;
            subscription.ExpiredAt       = 0;
            subscription.LastSent        = DateTime.UtcNow.Ticks;
            unitOfWork.Commit();
            unitOfWork.Add(subscription);
        }
        private void ControlSubscriptions()
        {
            string            rabbitFeedback;
            JsonStringContent feedbackContent;
            Observer          observer;

            while (true)
            {
                rabbitFeedback = _consumer.ReceiveQueue(_subscriptionQueueKey);
                if (rabbitFeedback == null)
                {
                    Thread.Sleep(100);
                    continue;
                }

                feedbackContent = new JsonStringContent(rabbitFeedback);
                string key = Convert.ToString(feedbackContent.Value("Subscription"));
                observer = new Observer(_configPath,
                                        Convert.ToString(feedbackContent.Value("UserID")),
                                        Convert.ToString(feedbackContent.Value("Location")),
                                        Convert.ToInt32(feedbackContent.Value("ResponsesPerHour")),
                                        _consumer,
                                        _publisher);

                SubscriptionStrategy strategy;

                if (key.Equals(_subscriptionKey))
                {
                    strategy = new SubscribeStrategy(_unit, _subject, observer);
                    strategy.Execute(feedbackContent);
                    continue;
                }
                if (key.Equals(_cancelSubscriptionKey))
                {
                    strategy = new AbortedSubscriptionStrategy(_unit, _subject, observer);
                    strategy.Execute(feedbackContent);
                    continue;
                }
                throw new ArgumentException($"Invalid value of {key}. Only {_subscriptionKey} and {_cancelSubscriptionKey} are excepted.");
            }
        }
예제 #7
0
        public void ExecuteTest()
        {
            var positive = new Dictionary <string, string>()
            {
                { "area", "Empire State Building:New York:USA" },
                { "created", "637399363231592838" },
                { "geolocation", "Empire State Building:New York:USA" },
                { "status", "Published" },
                { "finished", "637399363231723630" }
            };
            string positiveTest = "Empire State Building:New York:USA";

            taskManager.Execute(positiveTest);
            var positiveEtalon   = new ApiResponse(positive);
            var positiveFeedback = new JsonStringContent(consumer.Receive(positiveTest));

            Assert.AreEqual(positiveFeedback.Value("area"), positiveEtalon.Value("area"));
            Assert.AreEqual(positiveFeedback.Value("geolocation"), positiveEtalon.Value("geolocation"));
            Assert.AreEqual(positiveFeedback.Value("status"), positiveEtalon.Value("status"));

            var negative = new Dictionary <string, string>()
            {
                { "area", "1234567890qwerasdf" },
                { "created", "637399363231592838" },
                { "geolocation", "1234567890qwerasdf" },
                { "status", "Published" },
                { "finished", "637399363231723630" }
            };
            string negativeTest = "1234567890qwerasdf";

            taskManager.Execute(negativeTest);
            var negativeEtalon   = new ApiResponse(negative);
            var negativeFeedback = new JsonStringContent(consumer.Receive(negativeTest));

            Assert.AreEqual(negativeFeedback.Value("area"), negativeEtalon.Value("area"));
            Assert.AreEqual(negativeFeedback.Value("geolocation"), negativeEtalon.Value("geolocation"));
            Assert.AreEqual(negativeFeedback.Value("status"), negativeEtalon.Value("status"));
        }
예제 #8
0
 public abstract void Execute(JsonStringContent feedbackContent);