WinRT: Using Giant Bomb API to get Games Information
Giant Bomb API is a great resource for video game information. Here we will show you step by step that how to implement Giant Bomb API to search for any game and get information about them so that you can use it for making great gaming apps for Windows Store and Windows Phone apps.
Steps
- First of all, you have to register in Giant Bomb website and get API key from here http://www.giantbomb.com/api/
- You will get your API key on the link given above, more information is given on Giant Bomb Documentation http://www.giantbomb.com/api/documentation
Basics of using an API is that we request the server with a query and it gives information in response. So here we will request Giant Bomb server to search for a game, and it will provide us with the game details, which includes lots of information about that game.
API
The query to search for a game using Giant Bomb API is this.
In this query, you will provide your API Key and the name of the game that you want to search for. Other parameters include the response format of the query which we have set JSON here and the limit of results that.
If you paste the above URL into your browser with the parameters properly you will get lots of data, which is actually a JSON response from the web server. We will use this data to create our C# classes so that we can use them to get data in our app. We have used a website http://json2csharp.com which helps us in converting the JSON response to classes. Classes look like this when pasted into a new C# file.
01.public class RootObject
02.{
03. public string error { get; set; }
04. public int limit { get; set; }
05. public int offset { get; set; }
06. public int number_of_page_results { get; set; }
07. public int number_of_total_results { get; set; }
08. public int status_code { get; set; }
09. public ApiSearchResult[] results { get; set; }
10. public string version { get; set; }
11.}
12.
13.public class Result
14.{
15. public string aliases { get; set; }
16. public string api_detail_url { get; set; }
17. public string date_added { get; set; }
18. public string date_last_updated { get; set; }
19. public string deck { get; set; }
20. public string description { get; set; }
21. public object expected_release_day { get; set; }
22. public object expected_release_month { get; set; }
23. public object expected_release_quarter { get; set; }
24. public object expected_release_year { get; set; }
25. public int id { get; set; }
26. public Image image { get; set; }
27. public string name { get; set; }
28. public int number_of_user_reviews { get; set; }
29. public Original_Game_Rating[] original_game_rating { get; set; }
30. public string original_release_date { get; set; }
31. public Platform[] platforms { get; set; }
32. public string site_detail_url { get; set; }
33. public string resource_type { get; set; }
34.}
35.
36.public class Image
37.{
38. public string icon_url { get; set; }
39. public string medium_url { get; set; }
40. public string screen_url { get; set; }
41. public string small_url { get; set; }
42. public string super_url { get; set; }
43. public string thumb_url { get; set; }
44. public string tiny_url { get; set; }
45.}
46.
47.public class Original_Game_Rating
48.{
49. public string api_detail_url { get; set; }
50. public int id { get; set; }
51. public string name { get; set; }
52.}
53.
54.public class Platform
55.{
56. public string api_detail_url { get; set; }
57. public int id { get; set; }
58. public string name { get; set; }
59. public string site_detail_url { get; set; }
60. public string abbreviation { get; set; }
61.}
Save all of the above classes in a c# file.
Deserialize
To Deserialize the response object we will use a third-party library of JSON, i.e. Newtonsoft.json. To install the library go to ** Tools -> NuGet Package Manager -> Package Manager Console**
This will open a console. Type this there:
This command will install the JSON library to deserialize the object of the server response, in our case it is of the result type.
In App.xaml.cs file we have created an Observable Collection of type Result that will store all the data and games list we get as a response.
1.public static ObservableCollection<Result> ApiGamesList = new ObservableCollection<Result>();
Function to get data
Now take a look at function below which will get the data from the server in Root Object.
01.public static async Task<bool> SearchInputGame(string SearchKeyword)
02.{
03. RootObject gamesFound = null;
04.
05. string jsonData;
06.
07. using (var client = new HttpClient())
08. {
09. try
10. {
11. //query to API //response will be a string which will contain json data
12. jsonData = await client.GetStringAsync(new Uri
13. ("http://www.giantbomb.com/api/search/?api_key=YOUR_API_KEY&format=json&query="
14. + SearchKeyword + "&resources=game&limit=100"));
15.
16.
17. if (jsonData == null) { return false; }
18.
19. //all games list
20. gamesFound = JsonConvert.DeserializeObject<RootObject>(jsonData);
21.
22. foreach (var game in gamesFound.results)
23. {
24. App.ApiGamesList.Add(game);
25. }
26.
27. }
28. catch (Exception)
29. { }
30. }
31. return true;
32.}
Above function gets the search keyword of the game. We will use HttpClient() to connect to the server. jsonData is of string type that will store the result from the server which is in JSON. All of the results that you get from the server will be stored in ApiGamesList Observable Collection. Now you can display this data anywhere you want.