public void GetDataFromGoogleBooks()
        {
            ClearAll();

            // not a real test yet....
            var url = BooksService.GetSearchUrl("MonoTouch");

            var json = new MvvmCross.Plugins.Json.MvxJsonConverter();
            var client = new MvvmCross.Plugins.Network.Rest.MvxJsonRestClient
                {
                    JsonConverterProvider = () => json
                };
            var request = new MvxRestRequest(url);
            MvxDecodedRestResponse<BookSearchResult> theResponse = null;
            Exception exception = null;
            client.MakeRequestFor<BookSearchResult>(request,
                                                    (result) => { theResponse = result; },
                                                    (error) => { exception = error; });

            System.Threading.Thread.Sleep(3000);
            Assert.IsNotNull(theResponse);
            Assert.IsNull(exception);
            Assert.IsNotNull(theResponse.Result);
            Assert.AreEqual(HttpStatusCode.OK, theResponse.StatusCode);
            Assert.IsTrue(theResponse.Result.items.Count == 10);
            Assert.IsTrue(theResponse.Result.items[0].ToString().Contains("MonoTouch"));
        }
Exemple #2
0
        public async Task <MvxDecodedRestResponse <T> > MakeRequestForAsync <T>(MvxRestRequest restRequest, CancellationToken cancellationToken = default(CancellationToken))
        {
            var streamResponse = await MakeStreamRequestAsync(restRequest, cancellationToken).ConfigureAwait(false);

            using (var textReader = new StreamReader(streamResponse.Stream))
            {
                var text            = textReader.ReadToEnd();
                var result          = JsonConverterProvider().DeserializeObject <T>(text);
                var decodedResponse = new MvxDecodedRestResponse <T>
                {
                    CookieCollection = streamResponse.CookieCollection,
                    Result           = result,
                    StatusCode       = streamResponse.StatusCode,
                    Tag = streamResponse.Tag
                };
                return(decodedResponse);
            }
        }
        protected virtual void ProcessStreamResponse(MvxRestRequest restRequest, HttpWebRequest httpRequest, Action <MvxStreamRestResponse> successAction)
        {
            httpRequest.BeginGetResponse(result =>
            {
                var response = (HttpWebResponse)httpRequest.EndGetResponse(result);

                var code           = response.StatusCode;
                var responseStream = response.GetResponseStream();
                var restResponse   = new MvxStreamRestResponse
                {
                    CookieCollection = response.Cookies,
                    Stream           = responseStream,
                    Tag        = restRequest.Tag,
                    StatusCode = code
                };
                successAction?.Invoke(restResponse);
            }, null);
        }
Exemple #4
0
        protected virtual void ProcessResponse(MvxRestRequest restRequest, HttpWebRequest httpRequest, Action <MvxRestResponse> successAction, Action <Exception> errorAction)
        {
            httpRequest.BeginGetResponse(result =>
                                         TryCatch(() =>
            {
                var response = (HttpWebResponse)httpRequest.EndGetResponse(result);

                var code = response.StatusCode;

                var restResponse = new MvxRestResponse
                {
                    CookieCollection = response.Cookies,
                    Tag        = restRequest.Tag,
                    StatusCode = code
                };
                successAction?.Invoke(restResponse);
            }, errorAction), null);
        }
        protected virtual void ProcessRequestThen(
            MvxRestRequest restRequest,
            HttpWebRequest httpRequest,
            Action continueAction,
            Action <Exception> errorAction)
        {
            httpRequest.BeginGetRequestStream(result =>
                                              TryCatch(() =>
            {
                using (var stream = httpRequest.EndGetRequestStream(result))
                {
                    restRequest.ProcessRequestStream(stream);
                    stream.Flush();
                }

                continueAction();
            }, errorAction)
                                              , null);
        }
Exemple #6
0
 public IMvxAbortable MakeRequestFor <T>(MvxRestRequest restRequest, Action <MvxDecodedRestResponse <T> > successAction, Action <Exception> errorAction)
 {
     return(MakeRequest(restRequest, (MvxStreamRestResponse streamResponse) =>
     {
         using (var textReader = new StreamReader(streamResponse.Stream))
         {
             var text = textReader.ReadToEnd();
             var result = JsonConverterProvider().DeserializeObject <T>(text);
             var decodedResponse = new MvxDecodedRestResponse <T>
             {
                 CookieCollection = streamResponse.CookieCollection,
                 Result = result,
                 StatusCode = streamResponse.StatusCode,
                 Tag = streamResponse.Tag
             };
             successAction(decodedResponse);
         }
     }, errorAction));
 }
        public IMvxAbortable MakeRequest(MvxRestRequest restRequest, Action <MvxRestResponse> successAction, Action <Exception> errorAction)
        {
            HttpWebRequest httpRequest = null;

            TryCatch(() =>
            {
                httpRequest = BuildHttpRequest(restRequest);

                Action processResponse = () => ProcessResponse(restRequest, httpRequest, successAction, errorAction);
                if (restRequest.NeedsRequestStream)
                {
                    ProcessRequestThen(restRequest, httpRequest, processResponse, errorAction);
                }
                else
                {
                    processResponse();
                }
            }, errorAction);

            return(httpRequest != null ? new MvxRestRequestAsyncHandle(httpRequest) : null);
        }
        public IMvxAbortable MakeRequest(MvxRestRequest restRequest, Action<MvxRestResponse> successAction, Action<Exception> errorAction)
        {
            HttpWebRequest httpRequest = null;

            TryCatch(() =>
                {
                    httpRequest = BuildHttpRequest(restRequest);

                    Action processResponse = () => ProcessResponse(restRequest, httpRequest, successAction, errorAction);
                    if (restRequest.NeedsRequestStream)
                    {
                        ProcessRequestThen(restRequest, httpRequest, processResponse, errorAction);
                    }
                    else
                    {
                        processResponse();
                    }
                }, errorAction);

            return httpRequest != null ? new MvxRestRequestAsyncHandle(httpRequest) : null;
        }
 protected virtual void SetPlatformSpecificProperties(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     // do nothing by default
 }
 protected virtual HttpWebRequest CreateHttpWebRequest(MvxRestRequest restRequest)
 {
     return((HttpWebRequest)WebRequest.Create(restRequest.Uri));
 }
 protected virtual void SetMethod(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     httpRequest.Method = restRequest.Verb;
 }
        protected virtual void ProcessResponse(MvxRestRequest restRequest, HttpWebRequest httpRequest, Action<MvxRestResponse> successAction)
        {
            httpRequest.BeginGetResponse(result =>
            {
                var response = (HttpWebResponse)httpRequest.EndGetResponse(result);

                var code = response.StatusCode;

                var restResponse = new MvxRestResponse
                {
                    CookieCollection = response.Cookies,
                    Tag = restRequest.Tag,
                    StatusCode = code
                };
                successAction?.Invoke(restResponse);
            }, null);
        }
        protected virtual void ProcessRequestThen(
            MvxRestRequest restRequest,
            HttpWebRequest httpRequest,
            Action continueAction,
            Action<Exception> errorAction)
        {
            httpRequest.BeginGetRequestStream(result =>
                                              TryCatch(() =>
                                                  {
                                                      using (var stream = httpRequest.EndGetRequestStream(result))
                                                      {
                                                          restRequest.ProcessRequestStream(stream);
                                                          stream.Flush();
                                                      }

                                                      continueAction?.Invoke();
                                                  }, errorAction)
                                              , null);
        }
 protected virtual void SetPlatformSpecificProperties(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     // do nothing by default
 }
 protected virtual void SetCookieContainer(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     // note that we don't call
     //   httpRequest.SupportsCookieContainer
     // here - this is because Android complained about this...
     try
     {
         if (restRequest.CookieContainer != null)
         {
             httpRequest.CookieContainer = restRequest.CookieContainer;
         }
     }
     catch (Exception exception)
     {
         Mvx.Warning("Error masked during Rest call - cookie creation - {0}", exception.ToLongString());
     }
 }
 private static void SetCustomHeaders(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     if (restRequest.Headers != null)
     {
         foreach (var kvp in restRequest.Headers)
         {
             httpRequest.Headers[kvp.Key] = kvp.Value;
         }
     }
 }
 protected virtual HttpWebRequest BuildHttpRequest(MvxRestRequest restRequest)
 {
     var httpRequest = CreateHttpWebRequest(restRequest);
     SetMethod(restRequest, httpRequest);
     SetContentType(restRequest, httpRequest);
     SetUserAgent(restRequest, httpRequest);
     SetAccept(restRequest, httpRequest);
     SetCookieContainer(restRequest, httpRequest);
     SetCredentials(restRequest, httpRequest);
     SetCustomHeaders(restRequest, httpRequest);
     SetPlatformSpecificProperties(restRequest, httpRequest);
     return httpRequest;
 }
 protected virtual void SetContentType(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     if (!string.IsNullOrEmpty(restRequest.ContentType))
     {
         httpRequest.ContentType = restRequest.ContentType;
     }
 }
 protected virtual void SetUserAgent(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     if (!string.IsNullOrEmpty(restRequest.UserAgent))
     {
         httpRequest.Headers["user-agent"] = restRequest.UserAgent;
     }
 }
 protected virtual void SetAccept(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     if (!string.IsNullOrEmpty(restRequest.Accept))
     {
         httpRequest.Accept = restRequest.Accept;
     }
 }
        public Task<MvxRestResponse> MakeRequestAsync(MvxRestRequest restRequest, CancellationToken cancellationToken = default(CancellationToken))
        {
            TaskCompletionSource<MvxRestResponse> taskCompletionSource = new TaskCompletionSource<MvxRestResponse>();

            HttpWebRequest httpRequest = BuildHttpRequest(restRequest);

            using (CancellationTokenRegistration tokenRegistration = cancellationToken.Register(() =>
            {
                httpRequest.Abort();
                taskCompletionSource.SetCanceled();
            }))
            {
                Action processResponse = () => ProcessResponse(restRequest, httpRequest, response => taskCompletionSource.SetResult(response));
                if (restRequest.NeedsRequestStream)
                {
                    ProcessRequestThen(restRequest, httpRequest, processResponse);
                }
                else
                {
                    processResponse();
                }
            }

            return taskCompletionSource.Task;
        }
        protected virtual void ProcessResponse(
            MvxRestRequest restRequest,
            HttpWebRequest httpRequest,
            Action<MvxStreamRestResponse> successAction,
            Action<Exception> errorAction)
        {
            httpRequest.BeginGetResponse(result =>
                                         TryCatch(() =>
                    {
                        var response = (HttpWebResponse)httpRequest.EndGetResponse(result);

                        var code = response.StatusCode;
                        var responseStream = response.GetResponseStream();
                        var restResponse = new MvxStreamRestResponse
                        {
                            CookieCollection = response.Cookies,
                            Stream = responseStream,
                            Tag = restRequest.Tag,
                            StatusCode = code
                        };
                        successAction?.Invoke(restResponse);
                    }, errorAction)
                , null);
        }
 protected virtual void SetMethod(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     httpRequest.Method = restRequest.Verb;
 }
 protected virtual void SetCredentials(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     if (restRequest.Credentials != null)
     {
         httpRequest.Credentials = restRequest.Credentials;
     }
 }
 protected virtual HttpWebRequest CreateHttpWebRequest(MvxRestRequest restRequest)
 {
     return (HttpWebRequest)WebRequest.Create(restRequest.Uri);
 }
        protected virtual void ProcessExceptionResponse(MvxRestRequest restRequest, Exception ex, Action<MvxRestResponse> continueAction)
        {
            var restResponse = new MvxRestResponse
            {
                Tag = restRequest?.Tag,
                StatusCode = HttpStatusCode.BadRequest
            };

            continueAction?.Invoke(restResponse);
        }