This week's challenge is essentially about fetching Json data from the Web, and deserializing it into objects. I don't have much time to devote to this one, so what I have is very basic: it displays the names of all pokemons in a WPF ListView:
Here's the code for the window:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
var service = new PokemonApp.Web.Pokedex();
var pokemons = service.SelectAllPokemonReferences().OrderBy(pokemon => pokemon.Name);
DataContext = new MainWindowViewModel { References = pokemons };
}
}
As you've guessed, the interesting code is in the Pokedex
class:
public class Pokedex
{
public IEnumerable<ResourceReference> SelectAllPokemonReferences()
{
string jsonResult = GetJsonResult(PokeDexResultBase.BaseUrl + "pokedex/1/");
dynamic pokemonRefs = JsonConvert.DeserializeObject(jsonResult);
ICollection<ResourceReference> references = new List<ResourceReference>();
foreach (var pokeRef in pokemonRefs.pokemon)
{
string uri = PokeDexResultBase.BaseUrl + pokeRef.resource_uri.ToString();
references.Add(new ResourceReference
{
Name = pokeRef.name,
ResourceUri = new Uri(uri)
});
}
return references;
}
private static string GetJsonResult(string url)
{
string result;
WebRequest request = HttpWebRequest.Create(url);
request.ContentType = "application/json; charset=utf-8";
try
{
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
}
catch (Exception exception)
{
result = string.Empty;
// throw?
}
return result;
}
}
The ResourceReference
class has nothing special:
public class ResourceReference
{
public string Name { get; set; }
public Uri ResourceUri { get; set; }
}
This quite basic code makes a foundation for the more complex objects:
public abstract class PokeDexResultBase : IPokeDexUri
{
private readonly string _controller;
protected PokeDexResultBase(string controller)
{
_controller = string.IsNullOrEmpty(controller) ? BaseUrl : controller;
}
public static string BaseUrl { get { return "http://pokeapi.co/api/v1/"; } }
public int Id { get; set; }
public Uri ResourceUri { get; set; }
public DateTime Created { get; set; }
public DateTime Modified { get; set; }
public virtual string Url { get { return BaseUrl + _controller + (Id == 0 ? string.Empty : Id.ToString()); } }
}
That base class is inherited by everything that has an Id
in the API:
public class Pokemon : PokeDexResultBase
{
public Pokemon() : base("pokemon/") { }
public string Name { get; set; }
public ICollection<ResourceReference> Abilities { get; set; }
public ICollection<ResourceReference> EggGroups { get; set; }
public ICollection<PokemonEvolution> Evolutions { get; set; }
public ICollection<PokemonMove> Moves { get; set; }
public ICollection<PokemonType> Types { get; set; }
public int Attack { get; set; }
public int CatchRate { get; set; }
public int Defense { get; set; }
public int EggCycles { get; set; }
public string EvolutionYield { get; set; }
public int ExperienceYield { get; set; }
public string GrowthRate { get; set; }
public int Happiness { get; set; }
public string Height { get; set; }
public int HitPoints { get; set; }
public string MaleFemaleRatio { get; set; }
public int SpecialAttack { get; set; }
public int SpecialDefense { get; set; }
public string Species { get; set; }
public int Speed { get; set; }
public int Total { get; set; }
public string Weight { get; set; }
}
public class PokemonEvolution
{
public int Level { get; set; }
public string Method { get; set; }
public Uri ResourceUri { get; set; }
public string ToPokemonName { get; set; }
}
There are other classes involved, but there's nothing much to review about them, they're very similar to the Pokemon
class.
As I extend my code I'll add more methods to the Pokedex
class, which will use the GetJsonResult
method.
Have I well analyzed the API - I mean, is this code a solid foundation for deserializing pokemons and eventually getting them to fight against each other? What could be done better? Any nitpicks?