예제 #1
0
파일: MiniAPI.cs 프로젝트: chewax/MiniAPI
        /**
         * GET with plain request as callback
         */
        public void GET(string endPoint, GETCallback cb)
        {
            HTTP.Request req = new HTTP.Request( "get", this.baseURL + endPoint );

            // Add headers
            foreach (DictionaryEntry h in headers) {
                req.AddHeader((string) h.Key, (string) h.Value);
            }

            req.Send( ( request ) => { cb(request); });
        }
예제 #2
0
파일: MiniAPI.cs 프로젝트: chewax/MiniAPI
        /**
         * GET with result parsed as list
         */
        public void GET(string endPoint, GETListCallback cb)
        {
            HTTP.Request req = new HTTP.Request( "get", this.baseURL + endPoint );

            // Add headers
            foreach (DictionaryEntry h in headers) {
                req.AddHeader((string) h.Key, (string) h.Value);
            }

            req.Send( ( request ) => {
                var resList = Json.Deserialize(request.response.Text) as List<object>;
                cb(resList);
            });
        }
예제 #3
0
        public static void Upload(ContentMoment moment)
        {
            WWWForm form = moment.ToWWWForm();

            HTTP.Request postRequest = new HTTP.Request("post", GetUrl("/moments"), form);
            postRequest.AddHeader("Authorization", accessToken);

            postRequest.Send((request) => {
                bool result = false;
                try {
                    Hashtable thing = (Hashtable)JSON.JsonDecode(request.response.Text, ref result);
                } catch (NullReferenceException e) {
                    Debug.Log(e.ToString());
                    return;
                }

                if (!result) {
                    Debug.LogWarning("There is something wrong");
                    return;
                }
                Debug.Log("Moment uploaded");
            });
        }
예제 #4
0
        public bool sendApiRequest(string request_str, ref JSONNode jsonRes)
        {
            Debug.Log("Request = " + request_str);

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            data = encoding.GetBytes(request_str);

            Debug.Log("m_url = " + m_url);
            Debug.Log("data = " + data);

            HTTP.Request theRequest = new HTTP.Request("post", m_url, data);
            theRequest.AddHeader("Content-Type", "application/x-www-form-urlencoded");
            theRequest.synchronous = true;
            theRequest.Send();

            Debug.Log("theRequest = " + theRequest);
            if (theRequest.response == null)
            {
                Debug.Log("RESPONSE IS NULL!!!!");
                return(false);
            }
            Debug.Log("theRequest.response = " + theRequest.response);


            string strResp = theRequest.response.Text;

            Debug.Log("Response: (" + theRequest.response.status + "): " + strResp);

            if (theRequest.response.status != 200)
            {
                return(false);
            }

            try {
                jsonRes = JSON.Parse(strResp);
                if (jsonRes == null)
                {
                    Debug.Log("json parse failed");
                    return(false);
                }
            } catch (Exception e) {
                Debug.Log("Json parse Exception: " + e);
                return(false);
            }

            if (jsonRes["error"] == null)
            {
                Debug.Log("No error node!");
                return(false);
            }
            JSONNode error = jsonRes["error"];

            if ((error["success"] == null) || (error["success"].AsBool == false))
            {
                Debug.Log("json success is false");
                m_errorMsg = error["message"].Value;
                return(false);
            }

            return(true);
        }
예제 #5
0
        public bool sendSignedRequest(List <string> paramArray, string postBody, ref JSONNode jsonRes)
        {
            TimeSpan t         = (DateTime.UtcNow - new DateTime(1970, 1, 1));
            string   timestamp = t.ToString();

            List <string> parameters = (paramArray != null) ? paramArray : new List <string>();

            parameters.Add("key=" + m_Key);
            parameters.Add("timestamp=" + timestamp);

            string url_params = String.Join("&", parameters.ToArray());
            string url        = m_url + "?" + url_params;

            Debug.Log("Signed url request: " + url);

            List <string> headers = new List <string>();

            headers.Add("Host: " + ApiUtil.API_HOST);
            headers.Add("Content-type: application/json");
            headers.Add("User-Agent: Unity Knetik SDK");



            HTTP.Request theRequest;

            if (postBody != null)
            {
                System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
                headers.Add("Content-Length: " + postBody.Length);
                data       = encoding.GetBytes(postBody);
                theRequest = new HTTP.Request((m_method != null) ? m_method : "post", url, data);
                theRequest.AddHeader("Signature", signRequest(m_url, parameters, headers, postBody));
                //theRequest.AddHeader("Content-length", postBody.Length.ToString());
            }
            else
            {
                theRequest = new HTTP.Request((m_method != null) ? m_method : "get", url);
                theRequest.AddHeader("Signature", signRequest(m_url, parameters, headers, null));
            }

            theRequest.AddHeader("Content-type", "application/json");
            //theRequest.AddHeader("connection", "Close");
            theRequest.AddHeader("User-Agent", "Unity Knetik SDK");
            theRequest.synchronous = true;
            theRequest.Send();


            string strResp = theRequest.response.Text;

            Debug.Log("Response: (" + theRequest.response.status + "): " + strResp);

            if (theRequest.response.status != 200)
            {
                return(false);
            }


            try {
                jsonRes = JSON.Parse(strResp);
                if (jsonRes == null)
                {
                    Debug.Log("json parse failed");
                    return(false);
                }
            } catch (Exception e) {
                Debug.Log("Json parse Exception: " + e);
                return(false);
            }

            if (jsonRes["error"] == null)
            {
                Debug.Log("No error node!");
                return(false);
            }
            JSONNode error = jsonRes["error"];

            if ((error["success"] == null) || (error["success"].AsBool == false))
            {
                Debug.Log("json success is false");
                m_errorMsg = error["message"].Value;
                return(false);
            }

            return(true);
        }
예제 #6
0
파일: MiniAPI.cs 프로젝트: chewax/MiniAPI
        /**
         * POST with result parsed as Dictionary<string,object>
         */
        public void POST(string endPoint, Hashtable body, GETDictCallback cb)
        {
            Console.WriteLine(this.baseURL + endPoint);
            HTTP.Request req = new HTTP.Request( "post", this.baseURL + endPoint, body );

            // Add headers
            foreach (DictionaryEntry h in headers) {
                req.AddHeader((string) h.Key, (string) h.Value);
            }

            req.Send( ( request ) => {
                var resObj = Json.Deserialize(request.response.Text) as Dictionary<string, object>;
                cb(resObj);
            });
        }