You can call Api.Get<T>() to invoke a GET based API. The parameters can either be embedded in the url (route) or added as query string.
// Or with query string parameter:
await Api.Get<Order[]>("api/v1/orders?from=" + fromDate);
// You can also use an anonymous object for sending query string parameters:
await Api.Get<Order[]>("api/v1/orders", new { from = fromDate, to = toDate, ... });
Whenever the server returns a valid response (which is often Json) it will be saved in the application as a local file under Resources\-ApiCache\[URL-HASH].txt which can then be used in the future when the same URL (i.e. the same API with the same parameters) is called when the network or server is unavailable or there is any error. This allows the application to function in a read-only mode when offline.
Note: In certain scenarios you should delete the cache. For example when logging out, it's very important to delete the cache folder to prevent previous user's data being accidentally revealed to a new user account, and to prevent errors. You can achieve that using the following:
An optional parameter of Api.Get() is called cacheChoice which you can set to any of the following:
Another optional parameter of Api.Get() is called errorAction, which allows you to specify what should happen in case the network or server is down, or there is any problem in processing the response. For example:
The default option is to show a Toast. Learn more about OnError.
If the API responds successfully with no error, the fresh result will always be returned and your extra parameters will be ignored anyway (unless Cache option of Prefer is used). But if an error occurred, then use one of the following options depending on what outcome is acceptable for you:
It's recommended to make all API calls in your business domain or service classes in the App, which will be then called in your UI.
Example:
{
...
public static Task<Order[]> GetOrders() => Api.HttpGet<Order[]>("v1/orders");
...
}
In the UI then you can use this as your list module's data source.
Imagine a page which uses GET APIs to fetch and show a list of some data. At the first time that you visit that page, fresh data will be downloaded, cached locally and rendered.
In a typical scenario, you will probably go to another page and then back to this page. When you go back, the remote data may or may not have been changed. And you don't know what's the case. So you have to call the Web API for fresh data and wait for the response before rendering the page which will slow things down and annoy the user.
Zebble provides a better way!
This way every page of your app will be displayed immediately, providing the best user experience. Then if the data was changed, it will then reload the UI to reflect that, often in less than a second.
Example:
<ListView z-of="Product, Row" Id="ProductsList" DataSource="Items">
.....
</ListView>
bool IsReturnVisitToCachedPage = false;
public override async Task OnInitializing()
{
Items = await GetOrRefreshDataSource();
await base.OnInitializing();
await InitializeComponents();
...
// If the page is cached, every time the user comes back to this page the ShownEvent will be fired (but not OnInitializing).
// If you specifically don't want the page to be cached, you can remove it.
await WhenShown(() =>
{
if (IsReturnVisitToCachedPage) await GetOrRefreshDataSource();
else IsReturnVisitToCachedPage = true; // for next time
});
}
Task<Product[]> GetOrRefreshDataSource()
{
return Api.Get<Product[]>("api/v1/products", ApiResponseCache.PreferThenUpdate, Refresh);
}