Let's build our client web service controller to access the API. Since we built the back end, we should be able to whip this up very quickly. Our first step is to create the object which will deserialize a StockItem
. We refer to these as contracts. Add a new folder in your Stocklist.Portable
project called StocklistWebServiceController
, and add another folder in this called Contracts
. Create a new file called StockItemContract.cs
and implement the following:
public sealed class StockItemContract { #region Public Properties public int Id { get; set;} public string Name { get; set; } public string Category { get; set; } public decimal Price { get; set; } #endregion }
Now let's go ahead and build the IStocklistWebServiceController
interface:
public interface IStocklistWebServiceController { #region Methods and Operators IObservable<StockItemContract> GetAllStockItems (); Task<StockItemContract> GetStockItemById(int id); #endregion }
The functions match the exact functions we have in the API controller. Before we implement this interface we have to create a new file called Config.resx
in the Resources
folder. For now, let's just add some empty values for each URL path because we don't know these until we either have the site running locally, or if we deploy it somewhere:
<data name="ApiAllItems" xml:space="preserve"> <value></value> </data> <data name="GetStockItem" xml:space="preserve"> <value></value> </data>
Now let's implement the IStocklistWebServiceController
interface. Starting the constructor; we will have to retrieve the HttpClientHandler
(we will register this in the IoC
container later):
#region Constructors and Destructors public StocklistWebServiceController(HttpClientHandler clientHandler) { _clientHandler = clientHandler; } #endregion
Now let's implement the first function to retrieve all the items. It will use a HttpClient
to create an Observable
from the asynchronous function SendAsync
via an HttpClient
. The Observable
stream will be generated from the results returned from this function. We will then retrieve the response as a string (this will be JSON), and deserialize the string into multiple StockItemContracts
, which then (using Linq) will be passed into the Observable
stream and returned to the result of the function:
public IObservable<StockItemContract> GetAllStockItems () { var authClient = new HttpClient (this.clientHandler); var message = new HttpRequestMessage (HttpMethod.Get, new Uri (Config.ApiAllItems)); return Observable.FromAsync(() => authClient.SendAsync (message, new CancellationToken(false))) .SelectMany(async response => { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception("Respone error"); } return await response.Content.ReadAsStringAsync(); }) .Select(json => JsonConvert.DeserializeObject<StockItemContract>(json)); }
And now for the GetStockItem
function:
public IObservable<StockItemContract> GetStockItem (int id) { var authClient = new HttpClient(this.clientHandler); var message = new HttpRequestMessage(HttpMethod.Get, new Uri(string.Format(Config.GetStockItem, id))); return await Observable.FromAsync(() => authClient.SendAsync(message, new CancellationToken(false))) .SelectMany(async response => { if (response.StatusCode != HttpStatusCode.OK) { throw new Exception("Respone error"); } return await response.Content.ReadAsStringAsync(); }) .Select(json => JsonConvert.DeserializeObject<StockItemContract>(json)); }
Great! We now have our StocklistWebServiceController
; we now need to register this object to the interface inside the IoC container. Open up the PortableModule
class and add the following:
builer.RegisterType<StocklistWebServiceController> ().As<IStocklistWebServiceController>().SingleInstance();
3.137.187.233