private void RequestResult <T>(IDictionary <string, string> parameters, string adminEndpoint, Action <AdminResult <T> > callback, IDictionary <string, string> postData = null)
        {
            var uri = new Uri(string.Format("{0}{1}?{2}", _serverUrl, adminEndpoint, GetQueryString(parameters)), UriKind.Absolute);

            bool       isPost     = postData != null;
            WebRequest webRequest = WebRequest.Create(uri);

            webRequest.ContentType = "application/x-www-form-urlencoded";
            webRequest.Method      = isPost ? "POST" : "GET";
            //webRequest.Accept = "text/plain";

            Action <IAsyncResult> getResponse = asyncResult =>
            {
                var result = new AdminResult <T>();
                try
                {
                    using (var response = webRequest.EndGetResponse(asyncResult))
                    {
                        try
                        {
                            using (var reader = new StreamReader(response.GetResponseStream()))
                            {
                                string responseString = reader.ReadToEnd();
                                var    json           = JObject.Parse(responseString);
                                var    jsonToken      = json["status"];
                                if (jsonToken != null)
                                {
                                    // If a request has a status property, only worry about
                                    // the requests that come back with {"status": "error", otherstuff }
                                    if (jsonToken.ToString() == "error")
                                    {
                                        var exception = ServiceException.Parse(json);
                                        result.Exception = exception;
                                    }
                                }
                                else
                                {
                                    result.Result = JsonConvert.DeserializeObject <T>(responseString);
                                }
                                if (callback != null)
                                {
                                    callback(result);
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            result.Exception = e;
                            result.Status    = RequestStatus.Error;
                        }
                    }
                }
                catch (Exception e)
                {
                    result.Exception = e;
                    result.Status    = RequestStatus.Error;
                }
            };

            Action <IAsyncResult> postResponse = asyncResult =>
            {
                try
                {
                    if (postData != null)
                    {
                        var encoding = new UTF8Encoding();
                        var bytes    = encoding.GetBytes(GetQueryString(postData));
                        using (
                            Stream os =
                                webRequest.EndGetRequestStream(asyncResult))
                        {
                            os.Write(bytes, 0, bytes.Length);
                        }
                    }
                    webRequest.BeginGetResponse(ar2 => getResponse(ar2), null);
                }
                catch (Exception e)
                {
                    AdminResult <T> result = new AdminResult <T>();
                    result.Exception = e;
                    result.Status    = RequestStatus.Error;
                    if (callback != null)
                    {
                        callback(result);
                    }
                }
            };

            if (isPost)
            {
                webRequest.BeginGetRequestStream(ar => postResponse(ar), null);
            }
            else
            {
                webRequest.BeginGetResponse(ar => getResponse(ar), null);
            }
        }
Example #2
0
        internal static void SetAdminResult <T>(this TaskCompletionSource <T> source, AdminResult <T> result)
        {
            if (source == null)
            {
                return;
            }
            if (result == null)
            {
                return;
            }

            if (result.Exception != null)
            {
                source.SetException(result.Exception);
            }
            else
            {
                source.SetResult(result.Result);
            }
        }