# Change implementation from getting json from file to get it from API

I used to get a JSON file with some configuration settings from the file system, using this class:

public class ConfigurationFileService : IConfigurationService
{

public ConfigurationFileService(IConfigurationManager configurationManager)
{
_configurationManager = configurationManager;
}

public Configuration GetConfiguration(int agentId)
{
var path = _configurationManager.GetAppSetting("pathToConfigurationFile");

return Configuration.ToObject<Configuration>();
}
}


Now I will be getting that from an API (it will return exactly the same JSON), so I need to change the implementation of the IConfigurationService, I have done this so far.

public class ConfigurationApiClient : IConfigurationService
{

public ConfigurationApiClient()
{
_client = new HttpClient();
}

public Configuration GetConfiguration(int agentId)
{
return GetConfigurationAsync(agentId).Result;
}

{
try
{
var response = await _client.GetAsync(""); // TODO add the url
response.EnsureSuccessStatusCode();
return Configuration;
}
catch(HttpRequestException ex)
{
throw new ConfigurationException(string.Format("Exception when getting  settings for agent: {0}", agentId), ex);
}
}


The interface is very simple:

public interface IConfigurationService
{
Configuration GetConfiguration(int agentId);
}


I am not sure about a couple of things:

1. The way I reuse the httpclient. I have seen that the recommendation is not to dispose it after each request, so I guess putting it in the constructor is a good option as the class itself will be injected.

2. The usage of one public method with the signature of the interface, that blocks the result of the asynchronous method (given that's the correct way of getting the result asynchronously).

• Would you be willing to change the return type of GetConfiguration to Task<Configuration>? – mjolka Nov 12 '15 at 13:39
• @mjolka, not sure what you mean. Maybe skipping having two methods? – mitomed Nov 12 '15 at 13:49
• I mean changing IConfigurationService to look like this: public interface IConfigurationService { Task<Configuration> GetConfigurationAsync(int agentId); } – mjolka Nov 12 '15 at 13:51
• Oh I see @mjolka, well I thought it would be better to maintain the interface as the one accessing the file system returns Configuration – mitomed Nov 12 '15 at 14:07

public interface IAsyncConfigurationService : IConfigurationService{
}

public class ConfigurationApiClient : IAsyncConfigurationService
{
//...
public Configuration GetConfiguration(int agentId)
{
return GetConfigurationAsync(agentId).Result;
}

{
try
{
var response = await _client.GetAsync(""); // TODO add the url
response.EnsureSuccessStatusCode();
return Configuration;
}
catch(HttpRequestException ex)
{
throw new ConfigurationException(string.Format("Exception when getting  settings for agent: {0}", agentId), ex);
}
}
}


With this, at least, you're able to try to do a cast to this interface and call the assynchronous version.

If you are able to change the interface just a little bit and still have only the synchronous version, you could add an event, for when the configuration is ready:

public interface IConfigurationService
{
}

public class ConfigurationArgs{
public Configuration Config{get;set;}
}

public class ConfigurationApiClient : IConfigurationService
{
//...

if(handler != null){
handler(this, new ConfigurationArgs{
Config = config
});
}
}

{
GetConfigurationAsync(agentId).ContinueWith(e => {
});
}

{
try
{
var response = await _client.GetAsync(""); // TODO add the url
response.EnsureSuccessStatusCode();

The semantic now is that you can call LoadConfiguration any number of times (because I guess it could change), and have the result on the event handler.