Inheritance: UploadHandler
Beispiel #1
0
    IEnumerator StartRequestModifyTask(TaskAddRequest reqObj)
    {
        string url = "http://li1440-68.members.linode.com:3102/TaskModify";

        var jsonStr = JsonUtility.ToJson(reqObj);

        Debug.Log("jsonStr" + jsonStr);
        var jsonRaw  = System.Text.Encoding.UTF8.GetBytes(jsonStr);
        var uploader = new UnityEngine.Networking.UploadHandlerRaw(jsonRaw);

        uploader.contentType = "application/json; charset=utf-8";

        var req = UnityEngine.Networking.UnityWebRequest.Post(url, string.Empty);

        req.uploadHandler = uploader;
        req.SetRequestHeader("Accept", "application/json");

        yield return(req.Send());

        if (req.isError)
        {
            Debug.LogError("StartRequestModifyTask req.error" + req.error);
            yield break;
        }
        else
        {
            StandardResponse res = null;
            Debug.Log("StartRequestModifyTask completed response=" + req.downloadHandler.text);
            try
            {
                res = JsonUtility.FromJson <StandardResponse>(req.downloadHandler.text);
            }
            catch
            {
                yield break;
            }

            if (null == res || res.Success == false)
            {
                if (null != res)
                {
                    Debug.LogWarning("StartRequestModifyTask() res.Success=" + res.Success);
                }
                else
                {
                    Debug.LogWarning("StartRequestModifyTask() null == res");
                }

                yield break;
            }
            else
            {
                Debug.Log("StartRequestModifyTask completed");
                m_ModifyTaskInterfaceHelper.Clear();
                FetchTask();
            }
        }

        yield return(null);
    }
        /// <summary>
        /// Create a HTTP web request based on the provided arguments and invokes a callback method which you provide
        /// to handle the reponse once the download is complete.
        /// </summary>
        /// <param name="endPoint">The specific endpoint to send the request to. Appends the domain value to the begining of this string to form the full URI.</param>
        /// <param name="callback">A public method to invoke which takes the web request once the download has
        /// completed.</param>
        /// <param name="method">The type of web request to make. GET by defualt.</param>
        /// <param name="input">Whatever data to pass in as an input to converted to JSON for the endpoint. Null by default.</param>
        /// <returns>Coroutine handle which yields the web request.</returns>
        public virtual IEnumerator HttpRequest(string endPoint, UnityAction <UnityWebRequest> callback, RequestMethod method = RequestMethod.GET, object input = null)
        {
            DownloadHandler downloader = new DownloadHandlerBuffer();
            UnityWebRequest request;
            string          uri = domain + endPoint;

            switch (method)
            {
            case RequestMethod.GET:
                request = new UnityWebRequest(uri);
                request.downloadHandler = downloader;
                break;

            case RequestMethod.POST:
                byte[]        rawBody  = new System.Text.UTF8Encoding().GetBytes(JsonUtility.ToJson(input));
                UploadHandler uploader = new UploadHandlerRaw(rawBody);
                request = new UnityWebRequest(uri, Enum.GetName(typeof(RequestMethod), method), downloader, uploader);
                break;

            default:
                throw new Exception(string.Format("RequestMethod.{0} not supported. You can choose to extend this class and add your own custom support.", Enum.GetName(typeof(RequestMethod), method)));
            }

            if (useSecurityToken)
            {
                request.SetRequestHeader(securityTokenKey, securityTokenValue);
            }

            request.SetRequestHeader("Content-Type", "application/json");

            yield return(request.SendWebRequest());

            if (!IsResponsePositive(request.responseCode))
            {
                throw new Exception(string.Format("Web request returned negative response: {0} {1} '{2}'", request.responseCode, GetNameOfResponseCode(request.responseCode), request.downloadHandler.text));
            }

            callback.Invoke(request);
        }
    static int _CreateUnityEngine_Networking_UploadHandlerRaw(IntPtr L)
    {
        try
        {
            int count = LuaDLL.lua_gettop(L);

            if (count == 1)
            {
                byte[] arg0 = ToLua.CheckByteBuffer(L, 1);
                UnityEngine.Networking.UploadHandlerRaw obj = new UnityEngine.Networking.UploadHandlerRaw(arg0);
                ToLua.PushSealed(L, obj);
                return(1);
            }
            else
            {
                return(LuaDLL.luaL_throw(L, "invalid arguments to ctor method: UnityEngine.Networking.UploadHandlerRaw.New"));
            }
        }
        catch (Exception e)
        {
            return(LuaDLL.toluaL_exception(L, e));
        }
    }
 private static extern IntPtr Create(UploadHandlerRaw self, byte[] data);
 private static extern unsafe IntPtr Create(UploadHandlerRaw self, byte *data, int dataLength);
Beispiel #6
0
    public bool share(string id = "", string name = "", Dictionary <string, string> values = null)
    {
        if (api_access_token == null || !api_access_token.isValid())
        {
            if (!getAccessToken())
            {
                return(false);
            }
        }

        // If no id is supplied but we have one stored, reuse it.
        if (id == "" && statistic_id != "")
        {
            id = statistic_id;
        }

        string url = "https://api.globalstats.io/v1/statistics";

        if (id != "")
        {
            url = "https://api.globalstats.io/v1/statistics/" + id;
        }
        else
        {
            if (name == "")
            {
                name = "anonymous";
            }
        }

        string json_payload = "{\"name\":\"" + name + "\",\"values\":{";

        bool semicolon = false;

        foreach (KeyValuePair <string, string> value in values)
        {
            if (semicolon)
            {
                json_payload += ",";
            }
            json_payload += "\"" + value.Key + "\":\"" + value.Value + "\"";
            semicolon     = true;
        }
        json_payload += "}}";

        byte[] pData = Encoding.ASCII.GetBytes(json_payload.ToCharArray());

        GlobalstatsIO_StatisticResponse statistic = null;

        if (id == "")
        {
            Dictionary <string, string> headers = new Dictionary <string, string>();
            headers.Add("Authorization", "Bearer " + api_access_token.access_token);
            headers.Add("Content-Type", "application/json");
            headers.Add("Content-Length", json_payload.Length.ToString());

            WWW www = new WWW(url, pData, headers);

            while (!www.isDone)
            {
                System.Threading.Thread.Sleep(100);
            }

            // check for errors
            if (www.error == null)
            {
                //UnityEngine.Debug.Log ("WWW POST Ok!");
            }
            else
            {
                UnityEngine.Debug.Log("WWW POST Error: " + www.error);
                UnityEngine.Debug.Log("WWW POST Content: " + www.text);
                return(false);
            }

            statistic = JsonUtility.FromJson <GlobalstatsIO_StatisticResponse>(www.text);
        }
        else
        {
            UnityEngine.Networking.UnityWebRequest www = UnityEngine.Networking.UnityWebRequest.Put(url, pData);
            www.SetRequestHeader("Authorization", "Bearer " + api_access_token.access_token);
            www.SetRequestHeader("Content-Type", "application/json");

            UnityEngine.Networking.UploadHandler uploader = new UnityEngine.Networking.UploadHandlerRaw(pData);
            www.uploadHandler = uploader;

            www.Send();

            while (!www.isDone)
            {
                System.Threading.Thread.Sleep(100);
            }

            // check for errors
            if (www.error == null)
            {
                //UnityEngine.Debug.Log ("WWW PUT Ok!");
            }
            else
            {
                UnityEngine.Debug.Log("WWW PUT Error: " + www.error);
                UnityEngine.Debug.Log("WWW PUT Content: " + Encoding.ASCII.GetString(www.downloadHandler.data));
                return(false);
            }

            statistic = JsonUtility.FromJson <GlobalstatsIO_StatisticResponse>(Encoding.ASCII.GetString(www.downloadHandler.data));
        }

        // ID is available only on create, not on update, so do not overwrite it
        if (statistic._id != null && statistic._id != "")
        {
            statistic_id = statistic._id;
        }

        user_name = statistic.name;

        //Store the returned data statically
        foreach (GlobalstatsIO_StatisticValues value in statistic.values)
        {
            bool updated_existing = false;
            for (int i = 0; i < statistic_values.Count; i++)
            {
                if (statistic_values[i].key == value.key)
                {
                    statistic_values[i] = value;
                    updated_existing    = true;
                    break;
                }
            }
            if (!updated_existing)
            {
                statistic_values.Add(value);
            }
        }

        return(true);
    }