예제 #1
0
        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"));
        }
예제 #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);
        }
예제 #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);
        }
예제 #5
0
        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);
        }
예제 #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);
        }
예제 #8
0
        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;
 }
예제 #12
0
        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);
        }
예제 #13
0
        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);
        }
예제 #14
0
 protected virtual void SetPlatformSpecificProperties(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     // do nothing by default
 }
예제 #15
0
 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());
     }
 }
예제 #16
0
 private static void SetCustomHeaders(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     if (restRequest.Headers != null)
     {
         foreach (var kvp in restRequest.Headers)
         {
             httpRequest.Headers[kvp.Key] = kvp.Value;
         }
     }
 }
예제 #17
0
 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;
 }
예제 #18
0
 protected virtual void SetContentType(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     if (!string.IsNullOrEmpty(restRequest.ContentType))
     {
         httpRequest.ContentType = restRequest.ContentType;
     }
 }
예제 #19
0
 protected virtual void SetUserAgent(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     if (!string.IsNullOrEmpty(restRequest.UserAgent))
     {
         httpRequest.Headers["user-agent"] = restRequest.UserAgent;
     }
 }
예제 #20
0
 protected virtual void SetAccept(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     if (!string.IsNullOrEmpty(restRequest.Accept))
     {
         httpRequest.Accept = restRequest.Accept;
     }
 }
예제 #21
0
        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;
        }
예제 #22
0
        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);
        }
예제 #23
0
 protected virtual void SetMethod(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     httpRequest.Method = restRequest.Verb;
 }
예제 #24
0
 protected virtual void SetCredentials(MvxRestRequest restRequest, HttpWebRequest httpRequest)
 {
     if (restRequest.Credentials != null)
     {
         httpRequest.Credentials = restRequest.Credentials;
     }
 }
예제 #25
0
 protected virtual HttpWebRequest CreateHttpWebRequest(MvxRestRequest restRequest)
 {
     return (HttpWebRequest)WebRequest.Create(restRequest.Uri);
 }
예제 #26
0
        protected virtual void ProcessExceptionResponse(MvxRestRequest restRequest, Exception ex, Action<MvxRestResponse> continueAction)
        {
            var restResponse = new MvxRestResponse
            {
                Tag = restRequest?.Tag,
                StatusCode = HttpStatusCode.BadRequest
            };

            continueAction?.Invoke(restResponse);
        }