Developers

Flat
F# API Tutorial
User: stonesilent
Date: 2/14/2014 9:18 am
Views: 2269
Rating: 0

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())

Re: F# API Tutorial
User: stonesilent
Date: 2/14/2014 9:21 am
Views: 401
Rating: 0

Attached the file so that formatting is preserved

fsharp_api.txt fsharp_api.txt
Re: F# API Tutorial
JT
User: JT
Date: 2/16/2014 2:08 pm
Views: 593
Rating: 0

Thanks. Points awarded.

PreviousNext