This is a follow up from my previous post: Basic API wrapper around a Restful service
I'm writing a basic wrapper around a restful service that returns a list of Stores. Naturally, I want to make it completely testable (that's a major requirement!).
I have split out my TopmanClient
class so that just deals with the rest client bits (using RestSharp).
I then inject the TopmanClient
class into my TopmanRepository
:
public class TopmanClient : ITopmanClient
{
private readonly IRestClient _restClient;
private const string BaseUrl = "https://public.Topman.com/";
private const string AcceptTenant = "uk";
private const string AcceptLanguage = "en-GB";
public TopmanClient()
{
_restClient = new RestClient(BaseUrl);
_restClient.AddDefaultHeader("Accept-Tenant", AcceptTenant);
_restClient.AddDefaultHeader("Accept-Language", AcceptLanguage);
}
public IRestRequest Request(string url)
{
return new RestRequest(url);
}
public IRestResponse<T> Execute<T>(IRestRequest request) where T : new()
{
return _restClient.Execute<T>(request);
}
}
public class TopmanRepository : ITopmanRepository<Store>
{
private readonly ITopmanClient _topmanClient;
public TopmanRepository(ITopmanClient topmanClient)
{
_topmanClient = topmanClient;
}
public List<Store> Get(string query)
{
var request = _topmanClient.Request("stores");
request.RootElement = "Stores";
request.AddQueryParameter("q", query);
var response = _topmanClient.Execute<List<Store>>(request);
return response.Data;
}
}