When I develop, I usually try to follow the principles of SOLID . Usually you have an interface that is implemented by all affected classes, and then use the interface as a parameter in further calculations.
My question is: how can this be achieved when calling, for example, a web service? The code below is not very skinny and really does NOT correspond to a single template of responsibility and the principle of open / closed.
How would you reverse engineer the following code to follow SRP and O / C:
public class Fetch
{
public void Run()
{
var url = "https://api.nasa.gov/planetary/apod?api_key=NNKOjkoul8n1CH18TWA9gwngW1s1SmjESPjNoUFo";
var client = new HttpClient();
var response = client.GetAsync(url).Result;
var data = response.Content.ReadAsStringAsync().Result;
var parsedData = JsonConvert.DeserializeObject<Nasa>(data);
if(parsedData.media_type.Equals("image"))
{
CreateImage(parsedData);
}
if (parsedData.media_type.Equals("video"))
{
CreateVideo(parsedData);
}
if (parsedData.media_type.Equals("text"))
{
CreateText(parsedData);
}
}
}
public class Nasa
{
public string copyright { get; set; }
public string date { get; set; }
public string explanation { get; set; }
public string hdurl { get; set; }
public string media_type { get; set; }
public string service_version { get; set; }
public string title { get; set; }
public string url { get; set; }
}
(The Api key is taken from the Nasa example site, so don’t worry about exposure. The asynchronous part using .Resultis for this example only)