示例#1
0
        public MonthViewModel(Models.Month month, int year)
        {
            Month = month;

            Days = new ObservableCollection <DayViewModel>();
            var monthIndex = (int)Month;
            var totalDays  = DateTime.DaysInMonth(year, monthIndex);

            for (int dayIndex = 0; dayIndex < totalDays; dayIndex++)
            {
                var date = new DateTime(year, monthIndex, dayIndex);
                var day  = new DayViewModel(date);
                Days.Add(day);
            }
        }
        /// <summary>
        /// Get the spending insights grouped by counter party
        /// </summary>
        /// <param name="starlingClient">The starling client.</param>
        /// <param name="accountUid">Required parameter: Account uid</param>
        /// <param name="year">Required parameter: Year</param>
        /// <param name="month">Required parameter: Month</param>
        /// <returns>Task&lt;Models.SpendingCounterPartySummary&gt;.</returns>
        /// <exception cref="IBaseServices baseServices)">Failed to parse the response: " + ex.Message</exception>
        /// <return>Returns the Models.SpendingCounterPartySummary response from the API call</return>
        public async Task <Models.SpendingCounterPartySummary> QuerySpendingInsightsByCounterpartyAsync(StarlingClient starlingClient, Guid accountUid,
                                                                                                        string year, Models.Month month)
        {
            //prepare query string for API call
            var queryBuilder = new StringBuilder();

            queryBuilder.Append("api/v2/accounts/{accountUid}/spending-insights/counter-party");
            //process optional template parameters
            APIHelper.AppendUrlWithTemplateParameters(queryBuilder, new Dictionary <string, object>()
            {
                { "accountUid", accountUid }
            });
            //process optional query parameters
            APIHelper.AppendUrlWithQueryParameters(queryBuilder,
                                                   new Dictionary <string, object>()
            {
                { "year", year }, { "month", Models.MonthHelper.ToValue(month) }
            }, _arrayDeserializationFormat);
            //validate and preprocess url
            var queryUrl = APIHelper.GetUrl(starlingClient, queryBuilder);
            //append request with appropriate headers and parameters
            Dictionary <string, string> headers = APIHelper.GetRequestHeaders(starlingClient);
            var request = new HttpRequestMessage(HttpMethod.Get, queryUrl);

            foreach (KeyValuePair <string, string> header in headers)
            {
                request.Headers.Add(header.Key, header.Value);
            }

            //prepare the API call request to fetch the response
            HttpClient client = _clientFactory.CreateClient("StarlingBank");
            //invoke request and get response
            HttpResponseMessage response = await client.SendAsync(request);

            //handle errors defined at the API level
            await _baseServices.ValidateResponse(request, response);

            try
            {
                var content = await response.Content.ReadAsStringAsync();

                return(APIHelper.JsonDeserialize <Models.SpendingCounterPartySummary>(content));
            }
            catch (Exception ex)
            {
                throw new APIException("Failed to parse the response: " + ex.Message, request, response);
            }
        }