Creating an API data service

Using BaseHttpService as a foundation that abstracts away the HTTP request details, we can now begin to create services that leverage it to get responses back from the API in the form of domain-specific models. Specifically, we will create a data service that can be used by the ViewModels to get the TripLogEntry objects from the backend service.

We will start off by defining an interface for the data service that can be injected into the ViewModels, ensuring that there is no strict dependency on the API, or the logic that communicates with it, continuing the pattern we put in place in Chapter 4, Platform Specific Services and Dependency Injection. To create a data service for the TripLog API, perform the following steps:

  1. Create a new interface named ITripLogDataService in the Services folder of the core library:
      public interface ITripLogDataService 
{ }
  1. Update the ITripLogDataService interface with methods to get, update, and delete TripLogEntry objects:
      public interface ITripLogDataService
{
Task<IList<TripLogEntry>> GetEntriesAsync();
Task<TripLogEntry> GetEntryAsync(string id);
Task<TripLogEntry> AddEntryAsync(TripLogEntry entry);
Task<TripLogEntry> UpdateEntryAsync(TripLogEntry entry);
Task RemoveEntryAsync(TripLogEntry entry);
}

Next, we will create an implementation of this interface that will also subclass BaseHttpService so that it has access to our HttpClient implementation, as shown in the following steps:

  1. Create a new class in the core library Services folder named TripLogApiDataService that subclasses BaseHttpService and implements ITripLogDataService:
      public class TripLogApiDataService 
: BaseHttpService, ITripLogDataService
{ }
  1. Add two private properties to the TripLogApiDataService class—a Uri and an IDictionary<string, string>to store the base URL and headers, respectively, to be used for all requests:
      public class TripLogApiDataService 
: BaseHttpService, ITripLogDataService
{
readonly Uri _baseUri;
readonly IDictionary<string, string> _headers;
}
  1. Update the TripLogApiDataService constructor to take in a Uri parameter, then set the private _baseUri and _headers properties:
      public class TripLogApiDataService 
: BaseHttpService, ITripLogDataService
{
readonly Uri _baseUri;
readonly IDictionary<string, string> _headers;

public TripLogApiDataService(Uri baseUri)
{
_baseUri = baseUri;
_headers = new Dictionary<string, string>();

// TODO: Add header with auth-based token in chapter 7
_headers.Add ("zumo-api-version", "2.0.0");
}
}
  1. Finally, implement the members of ITripLogDataService using the SendRequestAsync<T> base class method:
      public class TripLogApiDataService 
: BaseHttpService, ITripLogDataService
{
readonly Uri _baseUri;
readonly IDictionary<string, string> _headers;

// ...

public async Task<IList<TripLogEntry>> GetEntriesAsync()
{
var url = new Uri(_baseUri, "/tables/entry");
var response = await SendRequestAsync<TripLogEntry[]>(url,
HttpMethod.Get, _headers);


return response;
}

public async Task<TripLogEntry> GetEntryAsync(string id)
{
var url = new Uri(_baseUri,
string.Format("/tables/entry/{0}", id));

var response = await SendRequestAsync<TripLogEntry>(url,
HttpMethod.Get, _headers);

return response;
}

public async Task<TripLogEntry> AddEntryAsync(TripLogEntry entry)
{
var url = new Uri(_baseUri, "/tables/entry");
var response = await SendRequestAsync<TripLogEntry>(url,
HttpMethod.Post, _headers, entry);


return response;
}

public async Task<TripLogEntry> UpdateEntryAsync(TripLogEntry entry)
{
var url = new Uri(_baseUri,
string.Format("/tables/entry/{0}", entry.Id));

var response = await SendRequestAsync<TripLogEntry>(url,
new HttpMethod("PATCH"), _headers, entry);

return response;
}

public async Task RemoveEntryAsync(TripLogEntry entry)
{
var url = new Uri(_baseUri,
string.Format("/tables/entry/{0}", entry.Id));

var response = await SendRequestAsync<TripLogEntry>(url,
HttpMethod.Delete, _headers);

}
}

Each method in this TripLog data service calls the SendRequestAsync method on the base class passing in the API route, the appropriate HttpMethod, and the zumo-api-version header that we used in the first section. The AddEntryAsync and UpdateEntryAsync methods also pass in a TripLogEntry object, which will be serialized and added to the HTTP request message content. In the next chapter, we will implement authentication with the API and update this service to pass in an authentication-based token in the header as well.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.188.152.157