I'm writing a basic wrapper around a restful service that returns a list of Stores. I'm using RestSharp which I'm injecting into the class.
Naturally, I want to make it completely testable (that's a major requirement!).
I'm also thinking that it could be more generic i.e. GetStores
but I'm struggling how to do as I need to specify things like the RootElement
.
public class TopManClient : ITopManClient
{
private readonly IRestClient _restClient;
private const string BaseUrl = "https://TopmanStores.Api.com/";
private const string AcceptTenant = "uk";
private const string AcceptLanguage = "en-GB";
public TopManClient(IRestClient restClient)
{
_restClient = restClient;
_restClient.BaseUrl = new Uri(BaseUrl);
_restClient.AddDefaultHeader("Accept-Tenant", AcceptTenant);
_restClient.AddDefaultHeader("Accept-Language", AcceptLanguage);
}
public IRestRequest GetRequest(string url)
{
return new RestRequest(url, Method.GET);
}
public List<Store> GetStores(string postCode)
{
var request = GetRequest("stores");
request.RootElement = "Stores";
request.AddQueryParameter("q", postCode);
var response = _restClient.Execute<List<Store>>(request);
if (response.ErrorException != null)
{
const string message = "Error retrieving response. Check inner details for more info.";
throw new ApplicationException(message, response.ErrorException);
}
return response.Data;
}
}