Base class for a Windows Phone 8.1 (Silverlight), Windows Phone 8.1 XAML and Windows 8.1 REST service implementation. Also works fin in Windows 10 projects (UWP).
PM> Install-Package Kulman.WPA81.BaseRestService
Create your service class and inherit from BaseRestService. The minimum you need to do to make it work is to override the GetBaseUrl() method to set the base url for all the requests.
public class MyDataService: BaseRestService
{
protected override string GetBaseUrl()
{
return "my base url";
}
}
You can (but do not have to) also override the GetRequestHeaders() method to set the default request headers.
protected override Dictionary<string, string> GetRequestHeaders(string requestUrl)
{
return new Dictionary<string, string>
{
{ "Accept-Encoding", "gzip, deflate" },
{ "Accept", "application/json" },
};
}
You can also override the CreateJsonSerializerSettings
method with you need custom JSON deserialization settings.
Now you can use the following methods in your class:
Task<T> Get<T>(string url);
Task<T> Put<T>(string url, object request);
Task<T> Post<T>(string url, object request);
Task<T> Patch<T>(string url, object request);
Task Delete(string url);
Task<Dictionary<string, string>> Head(string url);
If you need to get the raw request, there are overloads returning HttpResponseMessage
:
Task<HttpResponseMessage> Get(string url);
Task<HttpResponseMessage> Put(string url, object request);
Task<HttpResponseMessage> Post(string url, object request);
Task<HttpResponseMessage> Patch(string url, object request);
All the available methods have overloads accepting and CancellationToken
.
Methods in your service may then look like this
public Task<List<Account>> GetAccounts()
{
return Get<List<Account>>("/accounts");
}
public Task<Account> UpdateAccount(Account account)
{
return Patch<Account>("/accounts",account);
}
For more information, see my blog post REST service base class for Windows Phone 8.1 XAML apps.
For changes, see the changelog.