You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

73 lines
2.1 KiB

using UnityEngine;
using System.Collections;
using System.Net.Sockets;
using System.Collections.Generic;
using System.Net;
using System;
using Newtonsoft.Json;
namespace UC
{
public class Request<T> where T : new()
{
public virtual IEnumerator execute() { yield return null; }
public override string ToString()
{
string s = url_root + rest_path + "?";
foreach (System.Reflection.FieldInfo i in GetType().GetFields())
{
object o = i.GetValue(this);
if (o == null || o.ToString() == response.ToString())
continue;
s += i.Name + "=" + o.ToString() + "&";
}
return s;
}
protected void parse_response(string r)
{
if (r == null)
return;
JsonSerializerSettings setting = new JsonSerializerSettings();
setting.MissingMemberHandling = MissingMemberHandling.Ignore;
setting.DefaultValueHandling = DefaultValueHandling.Include;
setting.NullValueHandling = NullValueHandling.Ignore;
response = JsonConvert.DeserializeObject<T>(r, setting);
}
protected string url { get { return ToString(); } }
protected string url_root;
protected string rest_path;
public T response = new T();
}
public class GETRequest<T> : Request<T> where T : new()
{
public override IEnumerator execute()
{
Debug.Log("GET: " + url);
WWW www = new WWW(url);
yield return www;
Debug.Log("Response: [" + rest_path + "]" + www.text);
parse_response(www.text);
www.Dispose();
}
}
public class POSTRequest<T> : Request<T> where T : new()
{
public override IEnumerator execute()
{
Debug.Log("POST: " + url);
WWW www = new WWW(url, postForm);
yield return www;
Debug.Log("Response: " + www.text);
parse_response(www.text);
}
protected WWWForm postForm = null;
}
}