Exemplo n.º 1
0
        /// <summary>
        /// Sends a POST request to an API endpoint.</summary>
        /// <param name="url"> The URL of the API endpoint.</param>
        /// <param name="form"> When sending a POST request, you should create and populate a WWWForm object that defines the variables you are sending to the server.  If this parameter is not null, the request is assumed to be a POST request.  Otherwise it's a GET request.</param>
        /// <param name="timeout"> How long to wait for a response from the server before we give up.</param>
        /// <param name="webResponsePopulateDelegate"> This will be called when we have data back from the server, in order to allow us to populate response data into an object.  If you are calling a JSON API, you don't have to worry about this, since then you can just call either the SendGetRequest or SendPostRequest method, and it will take care of populating your response object for you.</param>
        /// <param name="onComplete"> This will be called when the request has completed (for both success and failure).</param>
        public string SendWebRequest <T>(string url, WWWForm form, float timeout
                                         , WebResponsePopulateDelegate webResponsePopulateDelegate, WebRequestFinishedDelegate onComplete)
        {
            // We're creating a unique identifier and passing it back out of this method so that upload progress can be tracked, if we're uploading a file.
            string requestIdentifier = Guid.NewGuid().ToString();

            WebRequestDetails webRequestDetails = new WebRequestDetails(url, form, timeout, typeof(T), webResponsePopulateDelegate, onComplete);

            webRequestDetails.MarkStartTime();
            _webRequests.Add(requestIdentifier, webRequestDetails);

            return(requestIdentifier);
        }
Exemplo n.º 2
0
        private void Update()
        {
            // We're setting up this webRequestsToRemove list and the keysArray so that we can remove specific web requests
            // down below, since we don't want to remove them while iterating over the _webRequests dictionary.
            List <string> webRequestsToRemove = new List <string>();

            string[] keysArray = _webRequests.Keys.ToArray();
            int      total     = keysArray.Length;

            for (int i = 0; i < total; ++i)
            {
                string key = keysArray[i];

                WebRequestDetails webRequestDetails = _webRequests[key];
                if (webRequestDetails.www.isDone)
                {
                    // The web request is done, so populate any data we need, call the finished delegate, then remove it from the _webRequests dictionary.
                    bool success = false;
                    if (string.IsNullOrEmpty(webRequestDetails.www.error))
                    {
                        if (webRequestDetails.webResponsePopulateDelegate != null)
                        {
                            success = webRequestDetails.webResponsePopulateDelegate(webRequestDetails.www, out webRequestDetails.responseObject, webRequestDetails.responseObjectType);
                        }
                    }

                    if (webRequestDetails.webRequestFinishedDelegate != null)
                    {
                        webRequestDetails.webRequestFinishedDelegate(success, webRequestDetails.www.error, ParseStatusCode(webRequestDetails.www), webRequestDetails.responseObject);
                    }

                    webRequestsToRemove.Add(key);
                }
                else if (webRequestDetails.HasTimedOut())
                {
                    // The web request timed out, so call the finished delegate, then remove it from the _webRequests dictionary.
                    if (webRequestDetails.webRequestFinishedDelegate != null)
                    {
                        webRequestDetails.webRequestFinishedDelegate(false, "Request timed out.", ParseStatusCode(webRequestDetails.www), null);
                    }

                    webRequestsToRemove.Add(key);
                }
            }

            foreach (string key in webRequestsToRemove)
            {
                _webRequests.Remove(key);
            }
        }