Esempio n. 1
0
        public async Task <List <PassInfo> > CallAPI(string accessCode)
        {
            // how to call rest API
            //https://stackoverflow.com/questions/22627296/how-to-call-rest-api-from-a-console-application/22627481
            //https://docs.microsoft.com/en-us/aspnet/web-api/overview/advanced/calling-a-web-api-from-a-net-client
            // best method
            //https://blog.jayway.com/2012/03/13/httpclient-makes-get-and-post-very-simple/
            try
            {
                // calls url builder utility which builds the URL to send via http client

                var        url      = URLBuilder.BuildURL(accessCode);
                HttpClient client   = new HttpClient();
                var        response = await client.GetAsync(url);

                response.EnsureSuccessStatusCode();
                string content = await response.Content.ReadAsStringAsync();

                //Console.WriteLine(content);
                // this works!!!
                List <PassInfo> pass = JsonConvert.DeserializeObject <List <PassInfo> >(content);

                // how to order model object in-place (which is more efficient since you don't have to
                // create a whole new object): https://stackoverflow.com/questions/3309188/how-to-sort-a-listt-by-a-property-in-the-object

                pass.Sort((x, y) => x.MountainPassId.CompareTo(y.MountainPassId));
                return(pass);
            }

            // catch block
            catch (HttpRequestException ex)
            {
                throw new HttpRequestException(ex.ToString());
            }
        }
Esempio n. 2
0
        public string PushURL()
        {
            URL = URLBuilder.BuildURL(parametersDictionary);
            WebClient webClient = new WebClient();
            string    json_data = String.Empty;

            try
            {
                json_data = webClient.DownloadString(URL);
            }
            catch (Exception ex)
            {
                throw new WebException(ex.Message);
            }
            return(json_data);
        }