Hi there
I have used RestSharp and wrapped an api into a nice NET Standard library
the library takes username, password and platform as arguments and then api outputs api key which library uses internally it also had data models in it
now it works fine, but I don't know how to integrate it into Xmarin.Forms, where to initiate it to be able to acsess its methods and its data from every xmarin.forms subpage (I am using a AppShell)
if we start with Shell Template that visual studio 2019 provides
should I initiate my API in AppShell.xaml.cs or in App.xaml.cs
but then if I do that in there how would I acsess it in for instance AboutPage View Model (I can make it static but this doesn't seem right and it wierd you have to call AppShell.myApi everytime you want to use an api)?
How would I access it in Xmarin.Android portion (I could use MessageCenter(but I can only call void methods with this I cannot(or don't know how) return data) or I could create some static classes in Xmarin.Forms portion, but I don't know if it is the right approach)
what is the best way to handle exeptions (for now my library only throws Exeption with message: throw new Exception("Message Here"))
API has errors like this:
200 - Request successful. More information is returned in json object. Object schema depends on method.
201 - Request successful. No result returned.
400 - Error occurred. Additional information provided in json response (ErrorResponse format: more info below)
401 - Authentication failure. There are two possible reasons: disabled API access or invalid ApiKey provided
403 - Access forbidden
404 - Invalid method URL
405 - HTTP method not allowed, please verify HTTP method of the request
429 - Too many requests in a given amount of time. API limits are 20 requests/second, 1500 requests/hour, 10000 requests/day
5xx - Something wrong happened in Calamari backend. Please wait a while and repeat your request. If error still occurs, drop us a line.
If status 4xx is returned, you can read additional information from returned json object.
{
"message": string,
"code": string,
"field": string
}
message - Human readable error explanation.
code - Error code. Possible value depends on method but there is few common error codes.
field - Which field of payload object caused error. NULL when error is not related to particular field
What is the best aproach to throw appropriate exeption for this, at the end I would like to show an alert to the user if exeption accures, so my library would trow 500Exeption for instance and user would get an error
how would I do this globally, so I don't need to wrap every button in try/catch block
All theese are more of a design questions that bother me and I don't know where to find anwsers to them, as I never saw an example with Xmarin.Forms and REST api that takes username/password api then sends api key which app uses in all subsequent API requests
Hopefully someone can give me some advice on all this, (particulary how to handle exeption and inform the user and where in the project to construct the api so I can easly acsess it from all subpages)
Thannks for Anwsering and Best Regards
Answers
@igork_10
after looking at this(Thanks for posting it): https://github.com/xamarin/xamarin-forms-samples/tree/master/WebServices/TodoREST/TodoREST
I saw that they are iniciating the api in mainApp to so I saw that this is normal
now I have to figure out how would I handle exeptions so I don't need to wrap every API call in try/catch to show alert to the user if anyone has any idea about it
I have my API call return a default object if the returned status code is not Success/Conflict. The calling process then deals with that as appropriate.
I do think you need to have some kind of handling in place so you can let the user know something went wrong or have your code handle the fact it couldn't get/post what it needed.
Polly does this for you
https://blog.verslu.is/xamarin/xamarin-forms-xamarin/network-resilience-polly/
https://github.com/App-vNext/Polly
I have similar way to throw exceptions as @grimorde
Your API must to return any data of exception. In your application you just need to check if any troubles exist and throw exception
Try-catch-finally blocks I always use on events (or command) when the user interact with application. For example, user taps button and in command you have try-catch.
@igorkr_10 yea but then I need to wrap everywhere where I think exeption could accur in try/cath/finally, but I don't have acsess to await DisplayAlert (); everywhere so I wanted something globally
((App)Application).MainPage.DisplayAlert()
right yea I can do that (although I had to do it like this: App.Current.MainPage.DisplayAlert(e.message); to make it work (I have a static class))
but I cannot seem to cath exeption (catch (Exeption e): CS0246 The type or namespace name 'Exeption' could not be found (are you missing a using directive or an assembly reference?)
I(my library) am trowing them like so: throw new Exception(err.code);
also if you remember I made my login page working: https://forums.xamarin.com/discussion/comment/401812#Comment_401812
but now when I call my DoLogin method my ui freezes up while data is fetch
so if I have method in my library like so
how could I change it so I won't lock the UI while it fetches data?
I am calling it like so in my LoginViewModel
how could I change and call my method so it won't lock the UI while it gets data?
I recommend to use HttpClient with methods SendAsync and GetAsync. You will find a lot of examples in web.
Also, make sure the json from your request deserealizing asynchronously too.
Also, you need to use async/await in any long running cases.