C
C#14mo ago
MileHDev

✅ Getting data from api on app startup?

I am experimenting with a very simple app. I want to keep a list of pokemon that I get from an api in a variable so that when a user searches in the front end for a name, the dotnet api will search do a string search on this list for similar names and return to the client to be rendered in a typeahead. Not sure how to make my api make an "automatic" http request for data without user interaction
12 Replies
MileHDev
MileHDevOP14mo ago
So the flow is, my front end <> my dotnet api <> external api
friedice
friedice14mo ago
what's your frontend?
MileHDev
MileHDevOP14mo ago
React
mtreit
mtreit14mo ago
If the goal is to fetch something once and then keep re-using it in your program / API, I think a good approach is to wrap it in a Lazy<T>
MileHDev
MileHDevOP14mo ago
I kind of know how to do this. I could just make it so that the first request made to the api will get the pokemon list and store it, and then subsequent requests wont need to request it anymore. But im curious if theres some kind of pattern for getting data asynchronously on start up and store in some kind of service for later use?
mtreit
mtreit14mo ago
This is a console program but the basic idea should work fine in your API / service:
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
static Lazy<Task<string>> externalApiData = new Lazy<Task<string>>(async () =>
{
var httpClient = new HttpClient();
var resp = await httpClient.GetAsync("https://treit.github.io/programming,/performance,/c%23/2021/12/10/FakeOptimization.html");
var result = await resp.Content.ReadAsStringAsync();
return result;
});

static void Main()
{
var data = externalApiData.Value.Result;
Console.WriteLine(data);
}
}
using System;
using System.Net.Http;
using System.Threading.Tasks;

class Program
{
static Lazy<Task<string>> externalApiData = new Lazy<Task<string>>(async () =>
{
var httpClient = new HttpClient();
var resp = await httpClient.GetAsync("https://treit.github.io/programming,/performance,/c%23/2021/12/10/FakeOptimization.html");
var result = await resp.Content.ReadAsStringAsync();
return result;
});

static void Main()
{
var data = externalApiData.Value.Result;
Console.WriteLine(data);
}
}
If you don't want to lazily fetch it you can just put the code to populate it inline in your services startup code. I think Lazy is better in most cases.
MileHDev
MileHDevOP14mo ago
When does this lazy do the api call?. I dont need to await it in Main?
friedice
friedice14mo ago
your front end would make a GET call on initialize and probably would want store that list in the front end
MileHDev
MileHDevOP14mo ago
I had thought of that but for learning purposes I wanted to know how to keep all of this server side and just return the list of matches
mtreit
mtreit14mo ago
The first time you try to access the data it will go fetch it. Subsequently it will be cached and re-used.
MileHDev
MileHDevOP14mo ago
Ahh got it Ty
Accord
Accord14mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server