Created
May 11, 2011 07:49
-
-
Save AHartTN/966072 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
using System.Collections.ObjectModel; | |
using System.Dynamic; | |
using System.IO; | |
using System.Linq; | |
using System.Net; | |
using System.Text; | |
using System.Web.Script.Serialization; | |
using System.Windows.Forms; | |
using LacunaExpanse; | |
using Logic; | |
namespace LEADMIN | |
{ | |
class Program | |
{ | |
static LEAdmin _admin = new LEAdmin(); | |
static void Main(string[] args) | |
{ | |
LESession session = _admin.Login("BlahBlahBlah", "BlahBlahBlah"); | |
Console.WriteLine(); | |
} | |
} | |
} | |
namespace Logic | |
{ | |
public class DynamicJsonObject : DynamicObject | |
{ | |
private IDictionary<string, object> Dictionary { get; set; } | |
public DynamicJsonObject(IDictionary<string, object> dictionary) | |
{ | |
Dictionary = dictionary; | |
} | |
public override bool TryGetMember(GetMemberBinder binder, out object result) | |
{ | |
result = Dictionary[binder.Name]; | |
if (result is IDictionary<string, object>) | |
{ | |
result = new DynamicJsonObject(result as IDictionary<string, object>); | |
} | |
else if (result is ArrayList && (result as ArrayList) is IDictionary<string, object>) | |
{ | |
result = new List<DynamicJsonObject>((result as ArrayList).ToArray().Select(x => new DynamicJsonObject(x as IDictionary<string, object>))); | |
} | |
else if (result is ArrayList) | |
{ | |
result = new List<object>((result as ArrayList).ToArray()); | |
} | |
return Dictionary.ContainsKey(binder.Name); | |
} | |
} | |
public class DynamicJsonConverter : JavaScriptConverter | |
{ | |
public override object Deserialize(IDictionary<string, object> dictionary, Type type, JavaScriptSerializer serializer) | |
{ | |
if (dictionary == null) | |
throw new ArgumentNullException("dictionary"); | |
if (type == typeof(object)) | |
{ | |
return new DynamicJsonObject(dictionary); | |
} | |
return null; | |
} | |
public override IDictionary<string, object> Serialize(object obj, JavaScriptSerializer serializer) | |
{ | |
throw new NotImplementedException(); | |
} | |
public override IEnumerable<Type> SupportedTypes | |
{ | |
get { return new ReadOnlyCollection<Type>(new List<Type>(new[] { typeof(object) })); } | |
} | |
} | |
public static class Traffic | |
{ | |
public static string CompileJsonString(object obj) | |
{ | |
JavaScriptSerializer jss = new JavaScriptSerializer(); | |
return jss.Serialize(obj); | |
} | |
public static dynamic JSONPost(string url, string strJson) | |
{ | |
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); | |
request.ContentType = "application/json; charset=utf-8"; | |
request.Accept = "application/json, text/javascript, */*"; | |
request.Method = "POST"; | |
byte[] byteArray = Encoding.UTF8.GetBytes(strJson); | |
request.ContentLength = byteArray.Length; | |
using (Stream s = request.GetRequestStream()) | |
{ | |
s.Write(byteArray, 0, byteArray.Length); | |
} | |
string json = ""; | |
try | |
{ | |
WebResponse response = request.GetResponse(); | |
Stream stream = response.GetResponseStream(); | |
using (StreamReader sr = new StreamReader(stream)) | |
{ | |
while (!sr.EndOfStream) | |
{ | |
json += sr.ReadLine(); | |
} | |
} | |
} | |
catch (Exception e) | |
{ | |
MessageBox.Show(e.Message + "\r\n" + e); | |
} | |
JavaScriptSerializer jss = new JavaScriptSerializer(); | |
jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); | |
dynamic entry = jss.Deserialize(json, typeof(object)) as dynamic; | |
return entry; | |
} | |
public static dynamic JSONGet(string url) | |
{ | |
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url); | |
request.ContentType = "application/json-rpc; charset=utf-8"; | |
request.Accept = "application/json-rpc, text/javascript, */*"; | |
request.Method = "GET"; | |
WebResponse response = request.GetResponse(); | |
Stream stream = response.GetResponseStream(); | |
string json = ""; | |
using (StreamReader sr = new StreamReader(stream)) | |
{ | |
while (!sr.EndOfStream) | |
{ | |
json += sr.ReadLine(); | |
} | |
} | |
JavaScriptSerializer jss = new JavaScriptSerializer(); | |
jss.RegisterConverters(new JavaScriptConverter[] { new DynamicJsonConverter() }); | |
dynamic entry = jss.Deserialize(json, typeof(object)) as dynamic; | |
return entry; | |
} | |
} | |
} | |
namespace LacunaExpanse | |
{ | |
public class APIKEY | |
{ | |
public const string Public = @"BlahBlahBlah"; | |
private const string Private = @"BlahBlahBlah"; | |
} | |
public class LEServer | |
{ | |
public string Name { get; set; } | |
public string Uri { get; set; } | |
public string Status { get; set; } | |
public string Location { get; set; } | |
public string Type { get; set; } | |
public string Description { get; set; } | |
} | |
public class LEJSON | |
{ | |
public string jsonrpc = "2.0"; | |
public string id = "1"; | |
public string method { get; set; } | |
public object parms { get; set; } | |
} | |
public class LELogin | |
{ | |
public LELogin(string user, string pass) | |
{ | |
name = user; | |
password = pass; | |
} | |
public string name { get; set; } | |
public string password { get; set; } | |
public string api_key { get { return APIKEY.Public; } } | |
} | |
public class LESession | |
{ | |
public string SessionID { get; set; } | |
} | |
public class LEAdmin | |
{ | |
public List<LEServer> Servers { get; set; } | |
public LESession Session { get; set; } | |
public LESession Login(string username, string password) | |
{ | |
LELogin login = new LELogin(username, password); | |
LESession session = new LESession(); | |
JavaScriptSerializer jss = new JavaScriptSerializer(); | |
LEJSON lejson = new LEJSON { method = "login", parms = login }; | |
string json = jss.Serialize(lejson); | |
json = json.Replace("parms", "params"); | |
//MessageBox.Show(json); | |
dynamic result = Traffic.JSONPost(@"http://us1.lacunaexpanse.com/empire", json); | |
session.SessionID = result.session_id; | |
Session = session; | |
return session; | |
} | |
public List<LEServer> GetServers() | |
{ | |
List<LEServer> servers = new List<LEServer>(); | |
dynamic svrs = Traffic.JSONGet(@"http://www.lacunaexpanse.com/servers.json"); | |
foreach (dynamic svr in svrs) | |
{ | |
LEServer server = new LEServer(); | |
server.Name = svr.name; | |
server.Uri = svr.uri; | |
server.Status = svr.status; | |
server.Location = svr.location; | |
server.Type = svr.type; | |
server.Description = svr.description; | |
servers.Add(server); | |
} | |
Servers = servers; | |
return servers; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment