Exemplo n.º 1
0
        public async Task GetCapacity_Should_ReturnValidCapacity(CourseInfo course)
        {
            CourseCapacity capacity = await requests.GetCapacity(course);

            capacity.CurrentCapacity.Should().BeGreaterOrEqualTo(0);
            capacity.TotalCapacity.Should().BeGreaterThan(0);
        }
Exemplo n.º 2
0
        // This method is called for each NotificationRequests on each iteration of the polling loop.
        // It checks the capacity of the course in the request, and notifies the user if space is found.
        public async Task NotifyIfSpaceFound(NotificationRequest notificationRequest,
                                             SynchronizedCollection <NotificationRequest> notificationRequests)
        {
            // Fetch and log the capacity for the course.
            CourseCapacity capacity = await requests.GetCapacity(notificationRequest.RequestedCourse);

            logger.LogInformation($"Capacity for {notificationRequest.RequestedCourse.Term}|{notificationRequest.RequestedCourse.Subject}|{notificationRequest.RequestedCourse.Code}|{notificationRequest.RequestedCourse.Section} is {capacity.CurrentCapacity}");

            // If we find capacity in the course, notify the user using their supplied contact method(s), logging any errors.
            if (capacity.CurrentCapacity > 0)
            {
                if (alertContact.SendNotification(notificationRequest))
                {
                    logger.LogInformation($"Space found in course {notificationRequest.RequestedCourse.Subject} {notificationRequest.RequestedCourse.Code} for user: {notificationRequest.Email} | {notificationRequest.Phone}");

                    // Remove the NotificationRequest from the in-memory collection, and repository.
                    notificationRequests.Remove(notificationRequest);
                    if (!repository.RemoveRequest(notificationRequest))
                    {
                        logger.LogError($"Did not remove request from db: {notificationRequest.Email}|{notificationRequest.Phone}|{notificationRequest.RequestedCourse.Term}|{notificationRequest.RequestedCourse.Subject}|{notificationRequest.RequestedCourse.Code}|{notificationRequest.RequestedCourse.Section}");
                    }
                }
                else
                {
                    logger.LogError($"Failed to send notification to user: {notificationRequest.Email}|{notificationRequest.Phone}|{notificationRequest.RequestedCourse.Term}|{notificationRequest.RequestedCourse.Subject}|{notificationRequest.RequestedCourse.Code}|{notificationRequest.RequestedCourse.Section}");
                }
            }
        }
        // This method will get the capacity of the input course.
        // This method makes a series of HTTP requests to gain authorization, then get the required course info.
        public async Task <CourseCapacity> GetCapacity(CourseInfo course)
        {
            try
            {
                HttpRequestMessage  request  = requestsHelper.CreateHttpRequestMessage(HttpMethod.Get, Constants.WebAdvisorInitialConnectionUrl);
                HttpResponseMessage response = await httpClient.SendAsync(request);

                string token = requestsHelper.GetTokenFromResponse(response);

                request  = requestsHelper.CreateHttpRequestMessage(HttpMethod.Get, Constants.WebAdvisorInitialConnectionUrl + token);
                response = await httpClient.SendAsync(request);

                token = requestsHelper.GetTokenFromResponse(response);

                string postUrl = requestsHelper.CreatePostUrl(token);
                request         = requestsHelper.CreateHttpRequestMessage(HttpMethod.Post, postUrl);
                request.Content = requestsHelper.CreateFormData(course);
                response        = await httpClient.SendAsync(request);

                string responseHtml = await response.Content.ReadAsStringAsync();

                HtmlDocument htmlDoc = new HtmlDocument();
                htmlDoc.LoadHtml(responseHtml);

                HtmlNode       capacityNode = htmlDoc.GetElementbyId(Constants.CapacityNodeID);
                CourseCapacity capacity     = requestsHelper.GetCourseCapacity(capacityNode);

                return(capacity);
            }
            catch (Exception e)
            {
                logger.LogInformation($"An exception occured in GetCapacity request: {e.Message}");
                throw;
            }
        }