2

I have a task to retrieve data from endpoint webapi and display it to View. The external website developers provided me with 2 endpoints.

The 1st endpoint is a list of data and only has got : id, name and year; the second one is per item (fetch by its Id) and has more and more data details that I need to use.

So far, I'm able to call the 1st endpoint and retrieve the all list and pass it on to view... but after a while I realized that I need to also call each endpoint item by its id to retrieve the whole details for each item... and to accomplish the later I need to call a second webapi by its Id.

So my idea so far is : I need to call two webapi. The first one to get the list of id, then call the second webapi and use the already retrieved list of id from the first webapi call to get every single item and their details; cause I cannot directly call the webapi by id as the list is dynamic from external site and can change anytime from the external web server. So I need first to retrieve the list.

I just need idea from you please and possibly sample code about what to do !!!

or If it's a good idea to call a second webapi.

-- I also notice a latency while calling webapi, also learn that jquery is fast.--

The App workflow is as follow : In WebSite A, when a user click a button named "Vehicle" it must call webapi from website B to get data, and display those data in a page in website A. So there's no user interaction for each item from website A. I need to retrieve the all >list + details.

Below is my endpoint call to get the whole list.

            HttpClient client = new HttpClient();

            string APIdatas = null;

            HttpResponseMessage response = await client.GetAsync("https://jsonplaceholder.typicode.com/todos/");

            if (response.IsSuccessStatusCode)
            {
                APIdatas = await response.Content.ReadAsStringAsync();
            }

            var stringJson = JsonConvert.DeserializeObject<List<CrowdfundingViewModel>>(APIdatas);

            return View(stringJson);
Chloe
  • 73
  • 9
  • 1
    You should avoid this. You should display the list of names you got from the first API call and then when user select a one name you should make second API call for the selected id and display the details in the UI. – Chetan Oct 16 '19 at 14:51
  • At what point do you need the detailed list of data from the second endpoint? Is it possible to use the data from the first endpoint and only fetch the second endpoint when it's needed for a specific item instead of for each item? – Jimenemex Oct 16 '19 at 14:53
  • I suppose it depends on your use-case: if the requirement is to display all the data and thus makes the 2nd api call necessary, OR on demand as @ChetanRanpariya says. On demand is better for performance as you only `get` what is required when you need it. – Ric Oct 16 '19 at 14:55
  • What if I call the API from a previous page and store the data somewhere. and whenever the user request that page I just pass it to the page. Is it possible? – Chloe Oct 17 '19 at 15:17

1 Answers1

0

I think basically you should only display the data that you get from the first API call and display them in a view(in a grid or something), and provide each row with a details button. Only when the user clicks the Details button you should make a second API call to get the further details of that record and display this in a separate view. Otherwise you will need to call the second API for each item which could have a performance penalty.

Muhamed Krasniqi
  • 1,134
  • 1
  • 13
  • 20
  • Yes I could do it but... the first API endpoint only has 3 items (id, name and year)... and the second endpoint which has more details (id, name, year, many url for pictures, Company, Location, Month, Map and more details) that I want to display on the view page. – Chloe Oct 16 '19 at 15:07
  • What if I call the API from a previous page and store the data somewhere. and whenever the user request that page I just pass it to the page. – Chloe Oct 17 '19 at 15:11
  • yes you can do maybe something in the background in a separate thread in the previous page to fetch the needed data and cache it somewhere otherwise this would block or slow down the previous page loading or interaction. Your have to do a workaround which best suits your use case to surpass this limitation. – Muhamed Krasniqi Oct 17 '19 at 20:37
  • Okay thanks. I tried it and used TempData to cache... but I got null when trying to read TempData !!! What is the best way to cache data in ASP.Net MVC? – Chloe Oct 18 '19 at 06:04
  • Reference the System.Web dll in your model and use System.Web.Caching.Cache. See this post: https://stackoverflow.com/questions/343899/how-to-cache-data-in-a-mvc-application – Muhamed Krasniqi Oct 19 '19 at 16:39