F# Example

NOTE: Get an APIKey.

This code will allow you to get a session, fetch account info, upload a file, and search games. From it you should be able to discern how to work with the entire TGC API.

 open System
 open System.Threading
 open Newtonsoft.Json // get this here = http://james.newtonking.com/json
 open RestSharp // get this here = http://restsharp.org/
 open Dynamitey 
 open System.Dynamic
 open System.Collections.Generic
 open System.Xml.Linq
 open System.Numerics
 open Microsoft.CSharp.RuntimeBinder
 
 let API_KEY_ID = "" // get your own
 let USERNAME = "" // put your username here;
 let PASSWORD = "" // put your password here;
 let client = RestSharp.RestClient("https://www.thegamecrafter.com/api/")
 let mutable session_id = ""
 let mutable user_id = ""
 let GetASession(username : string, password : string) =
     let request = new RestSharp.RestRequest("session", RestSharp.Method.POST);
     ignore (request.AddParameter("username", username))
     ignore (request.AddParameter("password", password))
     ignore (request.AddParameter("api_key_id", API_KEY_ID))
     let response = client.Execute(request)
     let data:Newtonsoft.Json.Linq.JObject = JsonConvert.DeserializeObject(response.Content) :?> Newtonsoft.Json.Linq.JObject
     session_id <- data.GetValue("result").SelectToken("id").ToString()
     user_id <-  data.GetValue("result").SelectToken("user_id").ToString()
 
 let FetchMyAccountInfo() =
     let request = new RestRequest("user/" + user_id, Method.GET)
     ignore (request.AddParameter("session_id", session_id))
     let response = client.Execute(request)
     let data:Newtonsoft.Json.Linq.JObject = JsonConvert.DeserializeObject(response.Content) :?> Newtonsoft.Json.Linq.JObject
     let user = data.GetValue("result")
     user
         
 let UploadAFile(user:Newtonsoft.Json.Linq.JToken, filepath, filename) =
     let request = new RestSharp.RestRequest("file", Method.POST)
     ignore (request.AddFile("file", filepath))
     ignore (request.AddParameter("name", filename))
     ignore (request.AddParameter("folder_id", user.SelectToken("root_folder_id")))
     ignore (request.AddParameter("session_id", session_id))
     let response = client.Execute(request)
     let data:Newtonsoft.Json.Linq.JObject = JsonConvert.DeserializeObject(response.Content) :?> Newtonsoft.Json.Linq.JObject
     let file = data.GetValue("result")
     file
 
 let SearchGames() =
     let request = new RestSharp.RestRequest("game", Method.GET)
     ignore (request.AddParameter("q", "Steampunk"))
     ignore (request.AddParameter("session_id", session_id))
     let response = client.Execute(request)
     let data:Newtonsoft.Json.Linq.JObject = JsonConvert.DeserializeObject(response.Content) :?> Newtonsoft.Json.Linq.JObject
     let games = data //.GetValue("result")
     games
 
 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...");
 let user = FetchMyAccountInfo()
 Console.WriteLine("Display Name: " + user.SelectToken("display_name").ToString())
 Console.WriteLine("email:        " + user.SelectToken("email").ToString())
 Thread.Sleep(1000);
 
 Console.WriteLine("\n")
 Console.WriteLine("Uploading A File...")
 let file = UploadAFile(user, "c:\\erase\\hi.png", "image.png")
 Console.WriteLine("File name:    " + file.SelectToken("name").ToString())
 Console.WriteLine("File ID:      " + file.SelectToken("id").ToString())
 Thread.Sleep(1000);
 
 Console.WriteLine("\n");
 Console.WriteLine("Searching Games...")
 let games = SearchGames()
 
 
 for game in games do
     Console.WriteLine("Game Name:    " + game.Value.SelectToken("items").First.SelectToken("name").ToString())
     Console.WriteLine("Game ID:      " + game.Value.SelectToken("items").First.SelectToken("id").ToString())
     Thread.Sleep(100)
 
 Console.WriteLine("\n");
 Console.Write("Hit any key to close...");
 ignore (Console.ReadKey())
^ Back to Top ^