Esempio n. 1
0
        private static async Task RunAsync(WebClient webClient)
        {
            //get list of questions with specified properties
            var           displayType  = "MultilineText";
            List <string> questionTags = new List <string>()
            {
                "Comments", "User Comments"
            };
            //returns list of question ids
            List <string> Ids = await webClient.GetQuestions("api/Questions/Active", displayType, questionTags);

            //
            //get list of answers from first page with matching questionIds
            int        pages      = 1;
            int        rows       = 1000;
            DateFilter dateFilter = new DateFilter(new DateTime(2019, 01, 19), new DateTime(2019, 07, 20));

            //returns list of answers
            finalAnswers = await webClient.GetAnswers("api/Answers/Page/", pages, rows, Ids, dateFilter);
        }
Esempio n. 2
0
        //get the list of all answers whic match the criteria and return the required fields for further processing
        public async Task <List <RequiredAnswer> > GetAnswers(string absoluteUri, int pageRequested, int rows, List <string> Ids, DateFilter dateFilter)
        {
            var requiredAnswers = new List <RequiredAnswer>();
            HttpResponseMessage response;

            try
            {
                //get the data
                using (HttpClient client = new HttpClient())
                {
                    string json        = JsonConvert.SerializeObject(dateFilter);
                    var    httpContent = new StringContent(json, Encoding.UTF8, "application/json");

                    client.BaseAddress = new Uri(BaseUri);
                    client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", BearerToken);
                    response = await client.PostAsync(BaseUri + absoluteUri + pageRequested + "/" + rows, httpContent);
                }

                //if success
                if (response.IsSuccessStatusCode)
                {
                    var result = response.Content.ReadAsStringAsync().Result;

                    //convert json objects into class objects
                    var answers = Newtonsoft.Json.JsonConvert.DeserializeObject <Answers>(result);

                    foreach (var answer in answers.Responses)
                    {
                        //get each response from the list of all
                        var answerResponse = answer.Responses;
                        foreach (var responseFromUser in answerResponse)
                        {
                            if (Ids.Contains(responseFromUser.QuestionId))
                            {
                                requiredAnswers.Add(new RequiredAnswer(responseFromUser.QuestionId, responseFromUser.QuestionText, responseFromUser.TextInput));
                            }
                        }
                    }
                }
                else
                {
                    Console.WriteLine($"Error in getting Answers: {response.StatusCode}");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception in getting answers: " + e);
            }
            return(requiredAnswers);
        }