RequestNode.cs 3.93 KB
using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using Globanet;
using Newtonsoft.Json;

namespace Globanet
{
    namespace REST
    {
        public class RequestNode : MonoBehaviour
        {
            void Awake()
            {
                DontDestroyOnLoad(this);
            }

            private RequestData data = null;
            private float delay = 0;

            public void Initialize(RequestData data, float delay)
            {
                this.data = data;
                this.delay = delay;

                StartCoroutine(SendRequest());
            }

            private IEnumerator SendRequest()
            {
                yield return new WaitForSeconds(delay);

                string url = data.url;

                /*
                //need to figure out the best way of handling this
                if (data.endpoint != null)
                {
                    string endpoint = data.endpoint;
                    //url = CmsSettings.url + "/" + endpoint;
                }
                else if (data.url != null)
                {
                    url = data.url;
                }*/
                //if (endpoint[endpoint.Length - 1] != '/')
                //    endpoint += '/';


                if (data.method != RequestMethod.GET && data.data == null)
                {
                    var d = new Dictionary<string, string>();
                    d.Add("foo", "bar");
                    data.data = d;
                }

                Debug.Log("Request Data: " + JsonConvert.SerializeObject(data.data));

                byte[] postData = null;
                if (data.data != null)
                    postData = System.Text.Encoding.UTF8.GetBytes(JsonConvert.SerializeObject(data.data));

                var headers = data.headers;
                if (headers == null)
                    headers = new Dictionary<string, string>();

                if (!headers.ContainsKey("Content-Type"))
                    headers.Add("Content-Type", "application/json");

                if (!headers.ContainsKey("X-HTTP-Method-Override"))
                    headers.Add("X-HTTP-Method-Override", data.method.ToString());

                if (!headers.ContainsKey("Authentication"))
                {
                    if (!string.IsNullOrEmpty(data.authentication))
                        headers.Add("Authorization", data.authentication);
                }

                
                Debug.Log(data.method + " request to " + url);

                WWW request = new WWW(url, postData, headers);
                yield return request;

                ResponseData response = new ResponseData();
                response.request = data;

                if (!string.IsNullOrEmpty(request.error))
                {
                    print(request.error);
                    try
                    {
                        response.error = request.error;
                        response.errorCode = System.Convert.ToInt32(System.Text.RegularExpressions.Regex.Match(request.error, @"\d+").Value);
                    }
                    catch (System.FormatException)
                    {
                        if (request.error == "couldn't connect to host")
                        {
                            response.error = request.error;
                            response.errorCode = 7;
                        }
                        else
                        {
                            response.error = request.error;
                            response.errorCode = 99;
                        }
                    }
                }
                else
                {
                    response.text = request.text;
                    //response.data = JsonConvert.DeserializeObject(request.text);
                }

                if (data.callback != null)
                    data.callback(response);

                Destroy(gameObject);
            }
        }
    }
}