In the second part of this series of articles, we talked about how to structure your project using Refit.
In this article, we are going to talk about how to structure your project to consume multiple APIs.
Let’s start understanding the issue
In the previous article we were consuming the MakeUp API, in this sample beside this API, we are going to consume the Reddit API to show a TimeLine.
The endpoint we are going to use is :
GET/ http://www.reddit.com/r/subreddit/new.json?sort=top&limit=20 which will return the latest 20 Reddit posts.
At this moment maybe you could think that we have to rewrite all the classes/methods we did before for to adapt it to this new API, but not, actually we can reuse all we did.
Expected result:
Let’s do it step by step
1-In the Config file add the new API Url
2-Create a new Refit interface API class and add the endpoint
3-In the IAPiManager class, add the new method definition
4-In the APiManager class, receive the new API and also implement the GetNews method
5-In the BaseViewModel, create an instance of the RedditApi and pass it to the APIManager
6-Call the request in the ViewModel
And that’s all!
You can check the full source code here:
https://github.com/CrossGeeks/RefitXamarinFormsSample/tree/Part3-ConsumingMultipleApis
Happy coding! 🙂
13 Comments
Nice articals
Thanks , happy coding
Hi.
Do you know how I can register the IApiManger and ApiManager with prism 7?
Yes!();
In the App.cs on the RegisterType method add
containerRegistry.RegisterSingleton
PDT:
Make sure to register the API Servicer as well, for example: https://gist.github.com/Char0394/3c28b29d826d8213b57055e06c4a2589
Hi.
Could you continue with part 4 for Login authentication, store user, refresh token, etc…? Thank so much.
Sure! 🙂
I really can’t find any solution for this. How would rewrite the request with the new access token after you refreshed it due to a 401 error?
Hi Jean, in that case you will have to do a new request to refresh token and then do the actual request again.
Normally I save the token in a Settings so I just have to change it.
Anyway, I have my doubts about refreshing the Token through the app, why? Because if the token expires is for security reasons, that means, to take the user to the Login again so he can login again. In case you don’t want that scenario of take the user to the login and instead refresh the token through the app, it does not make sense, because it is not adding any security, in that case the token should not expired. — That’s just my personal opinion about it —
Damn!!, thank you son much, i needed that 😀
Hi
Thanks for your Article
Just a question, When do you assigning the “CancellationTokenSource” to the task? (in ApiManager.GetNews lines 64 to 68)
Can you please clarify this?
Thank you so much
Sorry my bad, forgot to add it:
for example: DoRegisterAsync(RegisterRequest values) (myApi.GetApi(Priority.UserInitiated).DoRegisterAsync(values, cts.Token));
public async Task
{
var cts = new CancellationTokenSource();
var task = RemoteRequestAsync
runningTasks.Add(task.Id, cts);
return await task;
}
Update:
Just updated the sample with it. Thanks 🙂
Very nice topic! It helped me a lot.
I have two questions:
– How can I cancel the task in the RunSafe method manually?
I realized that sometimes the popup never stops and the only possiblity offered to the user is to close the app.
– I have hundreds of api call methods that I group by theme (Product, Company, Picture,…). How with your code can I handle this without having to put them all in the same interface and manager class?
Hi Sea,
1-You can create a CancellationTask (Same concept I’m doing in the ApiManager) or just cancel the loading in the viewmodel
2-You have some options, one of then is to create an api service per group (One ProductService, One CompanyService, etc)
And extend from BaseApiManager.
Another one is to use partial classes, and divide your calls in different files.