コード例 #1
0
        public static async Task <ComicModel> LoadComic(int comicNumber = 0)
        {
            string url = "";

            if (comicNumber > 0)
            {
                url = $"http://xkcd.com/{ comicNumber }/info.0.json";
            }
            else
            {
                url = $"http://xkcd.com/info.0.json";
            }

            using (HttpResponseMessage resonse = await ApiHelper.ApiClient.GetAsync(url))
            {
                if (resonse.IsSuccessStatusCode)
                {
                    ComicModel comic = await resonse.Content.ReadAsAsync <ComicModel>();

                    return(comic);
                }
                else
                {
                    throw new Exception(resonse.ReasonPhrase);
                }
            }
        }
        // call to api
        // public int MaxComicNumber { get; set; }


        public static async Task <ComicModel> LoadComic(int comicNumber = 0)  // make it async so it doesn't lock up.  It might take a while to get the info
        {
            string url = "";

            if (comicNumber > 0)
            {
                url = $"https://xkcd.com/{ comicNumber }/info.0.json";
            }
            else
            {
                url = $"https://xkcd.com/info.0.json";
            }

            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))      // open new request and wait for response.
            {
                if (response.IsSuccessStatusCode)                                               // if succesful then.
                {
                    // read the data that comes back
                    ComicModel comic = await response.Content.ReadAsAsync <ComicModel>();        // tries to map over the data to our model

                    return(comic);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                    //return null;
                }
            }   // at end it will close out all thing related to this request.
        }
コード例 #3
0
        public static async Task <ComicModel> LoadComic(int comicNumber = 0)
        {
            string url = "";

            if (comicNumber > 0)
            {
                url = $"https://xkcd.com/{comicNumber}/info.0.json";
            }
            else
            {
                url = "https://xkcd.com/info.0.json";
            }

            // this helps us dispose of things properly
            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
            {
                // when finished, it will close everything down related to "response"
                // because we dont want to leave ports open, and open up ports all the time

                if (response.IsSuccessStatusCode)
                {
                    ComicModel comic = await response.Content.ReadAsAsync <ComicModel>(); // ReadAsAsync uses the NewtonSoft Json converter which will try and map to our model

                    return(comic);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }
コード例 #4
0
        //task that returns a comicmodel and takes in a default number of 0
        // 0 is current or whatever number you're looking for
        public static async Task <ComicModel> LoadComic(int comicNumber = 0)
        {
            string url = "";

            if (comicNumber > 0)
            {
                url = $"https://xkcd.com/{ comicNumber}/info.0.json";
            }
            else
            {
                url = "https://xkcd.com/info.0.json";
            }


            //open call to web browser aka client
            //open up new request off our new api client and wait for the response

            //one client and make sure its disposed
            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    //if successful -> read the data
                    ComicModel comic = await response.Content.ReadAsAsync <ComicModel>();

                    return(comic);
                }
                else
                {
                    // if not successful
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }
コード例 #5
0
        public static async Task <ComicModel> LoadComic(int comicNumber = 0)
        {
            string url;

            if (comicNumber > 0)
            {
                url = $"http://xkcd.com/{comicNumber}/info.0.json";
            }
            else
            {
                url = "http://xkcd.com/info.0.json";
            }

            // fais une req sur l'url et attend la réponse
            using (HttpResponseMessage response = await ApiHelper.ApiClient.GetAsync(url))
            {
                if (response.IsSuccessStatusCode)
                {
                    // map le json lu dans la req http dans le model
                    ComicModel comic = await response.Content.ReadAsAsync <ComicModel>();

                    return(comic);
                }
                else
                {
                    throw new Exception(response.ReasonPhrase);
                }
            }
        }