I have an API where client can request additional data of a resource with a query param as
https://<host>/api/GetAppData?name=appname&getUsers=true&getversion=2.3
Above URL fetches me the appdetails by appname, along with that it fetches the Users & specific version of the app.
However, to get the additional info, I need to do two more calls (I know this is bad but we are limited with existing infra.)
So, I am trying to do a parallelfetch to the targetAPI with below code.
Task<AppData> appDataTask= myClient.GetAppDataAsync(identifierId, identifierName, version, token);
Task<Users> getUsers= (users.HasValue && users.Value) ?
FetchUsers(titleId: appId, token: token) : null;
Task<version> VersionTask= (version.HasValue && version.Value) ?
FetchVersionApp(titleId: TitleId, token: token) : null;
await Task.WhenAll(appDataTask, getUsers, VersionTask);
AppData app = appDataTask.Result;
AppResponse response = app.ConvertData();
if (getUsers != null) // this means users is requested.
{
DeployedUsers users= getUsers?.Result;
response.Users= users;
}
if (mos3AppAvailableToTask != null) // this means AvailableTo is requested.
{
VersionedApp versionedApp= VersionTask?.Result;
response.version= versionedApp;
}
Here, I am skeptical to assign null to a Task object. Not sure if I can do any better. I coulndt find any better way to do this.
How can I make this better? await tasks results and return.