The Xablu WebApiClient is a C# HTTP library which aims to simplify consuming of Web API services in .NET projects.
- Available on NuGet: https://www.nuget.org/packages/Xablu.WebApiClient/
- Install into each project that utilizes the WebApiClient
Create a new singleton of WebApiClient and use it to do REST operations.
var restApiClient = new RestApiClient("http://baseurl.com/");
If you want to use the standard HTTP client handlers, just register IRestApiClient
with a base url. You can set the type of native handler in your csproj settings.
Mvx.RegisterSingleton<IRestApiClient>(new RestApiClient(Constants.ApiBaseUrl));
When using custom HTTP handler, register a custom iOS Handler
var options = new RestApiClientOptions(Constants.ApiBaseUrl)
{
DefaultHttpMessageHandler = () => new NSUrlSessionHandler();
};
Mvx.RegisterSingleton<IRestApiClient>(new RestApiClient(options));
Register the custom handler in Android
var options = new RestApiClientOptions(Constants.ApiBaseUrl)
{
DefaultHttpMessageHandler = () => new AndroidClientHandler();
};
Mvx.RegisterSingleton<IRestApiClient>(new RestApiClient(options));
Create a client to handle Http traffic
public class AuthenticationClient : IAuthenticationClient
{
public AuthenticationClient(IRestApiClient apiClient) : base(apiClient)
{
}
public Task<int> AuthenticateWithCredentials(string username, string password)
{
var uri = new UriTemplate("user/logon");
var content = new MultipartFormDataContent();
content.Add(new StringContent(username), "username");
content.Add(new StringContent(password), "password");
return ExecuteRemoteRequest(async () => await apiClient.PostAsync<MultipartFormDataContent, int>(Priority.UserInitiated, uri.Resolve(), content).ConfigureAwait(false));
}
}
Add a Service to change and handle user logic
public class AuthenticationService : IAuthenticationService
{
private IBlobCache cache;
private IAuthenticationClient authClient;
public AuthenticationService(IAuthenticationClient authClient, IBlobCache cache = null)
: base()
{
this.cache = (cache ?? BlobCache.Secure);
this.authClient = authClient;
}
public async Task<bool> Login(string username, string password)
{
var userId = await authClient.AuthenticateWithCredentials(username, password).ConfigureAwait(false);
if (userId != default(int))
{
await this.cache.InsertObject(Settings.UserIdCacheKey, userId);
return true;
}
return false;
}
public async Task<bool> IsAuthenticated()
{
try
{
var userId = await cache.GetObject<int>(Settings.UserIdCacheKey).ToTask().ConfigureAwait(false);
return true;
}
catch (KeyNotFoundException)
{
return false;
}
}
public async Task Logout()
{
await cache.InvalidateAll().ToTask().ConfigureAwait(false);
}
}