internal protected virtual void ApiAsync(string path, IDictionary <string, object> parameters, HttpMethod httpMethod, object userToken)
        {
            var mergedParameters = FacebookUtils.Merge(null, parameters);

            if (!mergedParameters.ContainsKey("access_token") && !String.IsNullOrEmpty(AccessToken))
            {
                mergedParameters["access_token"] = AccessToken;
            }

            Uri    requestUrl;
            string contentType;

            byte[] postData = BuildRequestData(path, mergedParameters, httpMethod, out requestUrl, out contentType);

            var tempState = new WebClientStateContainer
            {
                UserState  = userToken,
                Method     = httpMethod,
                RequestUri = requestUrl
            };

            Action <string, Exception> callback =
                (json, ex) =>
            {
                byte[] result = null;

                if (ex == null)
                {
                    result = Encoding.UTF8.GetBytes(json);
                }

                if (httpMethod == HttpMethod.Get)
                {
                    DownloadDataCompleted(this, new DownloadDataCompletedEventArgsWrapper(ex, false, tempState, result));
                }
                else
                {
                    UploadDataCompleted(this, new UploadDataCompletedEventArgsWrapper(ex, false, tempState, result));
                }
            };

            var request = (HttpWebRequest)HttpWebRequest.Create(requestUrl);

            request.Method = FacebookUtils.ConvertToString(httpMethod); // Set the http method GET, POST, etc.

            if (httpMethod == HttpMethod.Post)
            {
                if (path == null && mergedParameters.ContainsKey("batch"))
                {
                    tempState.IsBatchRequest = true;
                }

                request.ContentType = contentType;
                request.BeginGetRequestStream((ar) => { RequestCallback(ar, postData, callback, tempState); }, request);
            }
            else
            {
                request.BeginGetResponse((ar) => { ResponseCallback(ar, callback, tempState); }, request);
            }
        }
        public WhichThrowsGraphExceptionThen()
        {
            this.facebookClient = new FacebookClient();

            var tempState = new WebClientStateContainer
                                {
                                    Method = this.httpMethod,
                                    RequestUri = new Uri(this.requestUrl),
                                };

            this.downloadDataCompletedEventArgs =
                new DownloadDataCompletedEventArgsWrapper(
                    WebClientFakes.GetFakeWebException(jsonResult), false, tempState, null);
        }
        public ThrowsRestApiErrorThen()
        {
            this.facebookClient = new FacebookClient();

            var tempState = new WebClientStateContainer
                                {
                                    Method = this.httpMethod,
                                    RequestUri = new Uri(this.requestUrl),
                                };

            this.downloadDataCompletedEventArgs =
                new DownloadDataCompletedEventArgsWrapper(
                    null, false, tempState, System.Text.Encoding.UTF8.GetBytes(jsonResult));
        }
        protected internal virtual void OAuthRequestAsync(string name, string path, IDictionary<string, object> parameters, object userToken, Func<string, string> processResponseString, Action<object, FacebookApiEventArgs> onDownloadComplete)
        {
            Contract.Requires(!String.IsNullOrEmpty(name));
            Contract.Requires(onDownloadComplete != null);

            var mergedParameters = FacebookUtils.Merge(null, parameters);

            path = FacebookUtils.ParseQueryParametersToDictionary(path, mergedParameters);

            if (!string.IsNullOrEmpty(path) && path.StartsWith("/", StringComparison.OrdinalIgnoreCase))
            {
                path = path.Substring(1, path.Length - 1);
            }

            var requestUrl = GetUrl(name, path, parameters);

            var webClient = WebClient;

            var tempState = new WebClientStateContainer
            {
                UserState = userToken,
                Method = HttpMethod.Get,
                RequestUri = requestUrl
            };

            webClient.DownloadDataCompleted = (o, e) => DownloadDataCompleted(o, e, processResponseString, onDownloadComplete);

            webClient.DownloadDataAsync(requestUrl, tempState);
        }