The Game Crafter – VB Tutorial

The Game Crafter is looking for tutorials on how to use their API. Here’s a VB 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)

Imports System.Threading
Imports Newtonsoft.Json
Imports RestSharp

Module Module1
    Const API_KEY_ID = "" ' get your own
    Const USERNAME = "" ' put your username here;
    Const PASSWORD = "" ' put your password here;
    Dim client As RestClient
    Dim session_id As String
    Dim user_id As String

    Sub Main()
        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(Environment.NewLine)
        Console.WriteLine("Fetching My Account Info...")
        Dim user = FetchMyAccountInfo()
        Console.WriteLine("Display Name: " + user("display_name"))
        Console.WriteLine("email:        " + user("email"))
        Thread.Sleep(1000)

        Console.WriteLine(Environment.NewLine)
        Console.WriteLine("Uploading A File...")
        Dim 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(Environment.NewLine)
        Console.WriteLine("Searching Games...")
        Dim games = SearchGames()
        For Each game As Object In games
            Console.WriteLine("Game Name:    " + game("name"))
            Console.WriteLine("Game ID:      " + game("id"))
            Thread.Sleep(100)
        Next

        Console.WriteLine(Environment.NewLine)
        Console.Write("Hit any key to close...")
        Console.ReadKey()
    End Sub

    Sub GetASession(username As String, password As String)
        Dim request = New RestRequest("session"Method.POST)
        request.AddParameter("username", username)
        request.AddParameter("password", password)
        request.AddParameter("api_key_id", API_KEY_ID)

        Dim response = client.Execute(request)
        Dim data = JsonConvert.DeserializeObject(response.Content)

        session_id = data("result")("id")
        user_id = data("result")("user_id")
    End Sub

    Function FetchMyAccountInfo() As Object
        Dim request = New RestRequest("user/" + user_id, Method.GET)
        request.AddParameter("session_id", session_id)

        Dim response = client.Execute(request)
        Dim data = JsonConvert.DeserializeObject(response.Content)

        Dim user = data("result")

        Return user
    End Function

    Function UploadAFile(user As Object, filepath As String, filename As StringAs Object
        Dim 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)

        Dim response = client.Execute(request)
        Dim data = JsonConvert.DeserializeObject(response.Content)
        Dim file = data("result")

        Return file
    End Function

    Function SearchGames() As Object
        Dim request = New RestRequest("game"Method.GET)
        request.AddParameter("q""Steampunk")
        request.AddParameter("session_id", session_id)

        Dim response = client.Execute(request)
        Dim data = JsonConvert.DeserializeObject(response.Content)

        Dim games = data("result")("items")

        Return games
    End Function
End Module
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: