The Game Crafter – C# Tutorial

The Game Crafter is looking for tutorials on how to use their API. Here’s a C# example of their tutorial.  I use Json.Net and RestSharp to simplify the whole process.  I recommend copying this into your IDE to take a look at it.  It’s a console application, which should run without any problem, provided you put in a valid API Key, Username, and Password.

Source Code – (Visual Studio 2010 .sln)

using System;
using System.Threading;
using Newtonsoft.Json;
using RestSharp;

namespace TGCTutorial
{
    class Program
    {
        const string API_KEY_ID = ""// get your own
        const string USERNAME = ""// put your username here;
        const string PASSWORD = ""// put your password here;
        static RestClient client;
        static string session_id;
        static string user_id;

        static void Main(string[] args)
        {
            client = new RestClient("https://www.thegamecrafter.com/api/");

            Console.WriteLine("Getting A Session...");
            GetASession(USERNAME, PASSWORD);
            Console.WriteLine("Session ID:   " + session_id);
            Console.WriteLine("User ID:      " + user_id);
            Thread.Sleep(1000);

            Console.WriteLine("\n");
            Console.WriteLine("Fetching My Account Info...");
            dynamic user = FetchMyAccountInfo();
            Console.WriteLine("Display Name: " + user.display_name);
            Console.WriteLine("email:        " + user.email);
            Thread.Sleep(1000);

            Console.WriteLine("\n");
            Console.WriteLine("Uploading A File...");
            dynamic file = UploadAFile(user, "path/to/image.png""image.png");
            Console.WriteLine("File name:    " + file.name);
            Console.WriteLine("File ID:      " + file.id);
            Thread.Sleep(1000);

            Console.WriteLine("\n");
            Console.WriteLine("Searching Games...");
            dynamic games = SearchGames();
            foreach (dynamic game in games)
            {
                Console.WriteLine("Game Name:    " + game.name);
                Console.WriteLine("Game ID:      " + game.id);
                Thread.Sleep(100);
            }

            Console.WriteLine("\n");
            Console.Write("Hit any key to close...");
            Console.ReadKey();
        }

        static void GetASession(string username, string password)
        {
            RestRequest request = new RestRequest("session"Method.POST);
            request.AddParameter("username", username);
            request.AddParameter("password", password);
            request.AddParameter("api_key_id", API_KEY_ID);

            var response = client.Execute(request);
            dynamic data = JsonConvert.DeserializeObject(response.Content);

            session_id = data.result.id;
            user_id = data.result.user_id;
        }

        static dynamic FetchMyAccountInfo()
        {
            RestRequest request = new RestRequest("user/" + user_id, Method.GET);
            request.AddParameter("session_id", session_id);

            var response = client.Execute(request);
            dynamic data = JsonConvert.DeserializeObject(response.Content);

            dynamic user = data.result;

            return user;
        }

        static dynamic UploadAFile(dynamic user, string filepath, string filename)
        {
            RestRequest request = new RestRequest("file"Method.POST);
            request.AddFile("file", filepath);
            request.AddParameter("name", filename);
            request.AddParameter("folder_id", user.root_folder_id);
            request.AddParameter("session_id", session_id);

            var response = client.Execute(request);
            dynamic data = JsonConvert.DeserializeObject(response.Content);
            var file = data.result;

            return file;
        }

        static dynamic SearchGames()
        {
            RestRequest request = new RestRequest("game"Method.GET);
            request.AddParameter("q""Steampunk");
            request.AddParameter("session_id", session_id);

            var response = client.Execute(request);
            dynamic data = JsonConvert.DeserializeObject(response.Content);

            dynamic games = data.result.items;

            return games;
        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: