From 7bc1b1c7a10cffdc31ede99a6f12825a3384dcd1 Mon Sep 17 00:00:00 2001 From: scidec Date: Wed, 7 May 2014 08:16:38 -0600 Subject: [PATCH 1/5] Add MERGE method to rest requests. --- RestSharp/Enum.cs | 3 ++- RestSharp/Http.Async.cs | 7 ++++++- RestSharp/Http.Sync.cs | 10 +++++++++- RestSharp/IHttp.cs | 6 ++++-- RestSharp/RestClient.Async.cs | 3 ++- RestSharp/RestClient.Sync.cs | 5 +++-- 6 files changed, 26 insertions(+), 8 deletions(-) diff --git a/RestSharp/Enum.cs b/RestSharp/Enum.cs index ad62429e3..5f6248db8 100644 --- a/RestSharp/Enum.cs +++ b/RestSharp/Enum.cs @@ -49,7 +49,8 @@ public enum Method DELETE, HEAD, OPTIONS, - PATCH + PATCH, + MERGE, } /// diff --git a/RestSharp/Http.Async.cs b/RestSharp/Http.Async.cs index 50999f349..a181995e2 100644 --- a/RestSharp/Http.Async.cs +++ b/RestSharp/Http.Async.cs @@ -75,7 +75,12 @@ public HttpWebRequest PutAsync(Action action) public HttpWebRequest PatchAsync(Action action) { return PutPostInternalAsync("PATCH", action); - } + } + + public HttpWebRequest MergeAsync(Action action) + { + return PutPostInternalAsync("MERGE", action); + } /// /// Execute an async POST-style request with the specified HTTP Method. diff --git a/RestSharp/Http.Sync.cs b/RestSharp/Http.Sync.cs index 72c354d0f..91539edc2 100644 --- a/RestSharp/Http.Sync.cs +++ b/RestSharp/Http.Sync.cs @@ -85,7 +85,15 @@ public HttpResponse Delete() public HttpResponse Patch() { return PostPutInternal("PATCH"); - } + } + + /// + /// Execute a MERGE request + /// + public HttpResponse Merge() + { + return PostPutInternal("MERGE"); + } /// /// Execute a GET-style request with the specified HTTP Method. diff --git a/RestSharp/IHttp.cs b/RestSharp/IHttp.cs index f43bfdc33..8857f2bc1 100644 --- a/RestSharp/IHttp.cs +++ b/RestSharp/IHttp.cs @@ -69,7 +69,8 @@ public interface IHttp HttpWebRequest OptionsAsync(Action action); HttpWebRequest PostAsync(Action action); HttpWebRequest PutAsync(Action action); - HttpWebRequest PatchAsync(Action action); + HttpWebRequest PatchAsync(Action action); + HttpWebRequest MergeAsync(Action action); HttpWebRequest AsPostAsync(Action action, string httpMethod); HttpWebRequest AsGetAsync(Action action, string httpMethod); @@ -80,7 +81,8 @@ public interface IHttp HttpResponse Options(); HttpResponse Post(); HttpResponse Put(); - HttpResponse Patch(); + HttpResponse Patch(); + HttpResponse Merge(); HttpResponse AsPost(string httpMethod); HttpResponse AsGet(string httpMethod); diff --git a/RestSharp/RestClient.Async.cs b/RestSharp/RestClient.Async.cs index 73fa13144..af5cdbd2b 100644 --- a/RestSharp/RestClient.Async.cs +++ b/RestSharp/RestClient.Async.cs @@ -43,7 +43,8 @@ public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action< string method = Enum.GetName(typeof (Method), request.Method); #endif switch (request.Method) - { + { + case Method.MERGE: case Method.PATCH: case Method.POST: case Method.PUT: diff --git a/RestSharp/RestClient.Sync.cs b/RestSharp/RestClient.Sync.cs index 6351c3942..1480c7a14 100644 --- a/RestSharp/RestClient.Sync.cs +++ b/RestSharp/RestClient.Sync.cs @@ -38,8 +38,9 @@ public virtual IRestResponse Execute(IRestRequest request) switch (request.Method) { case Method.POST: - case Method.PUT: - case Method.PATCH: + case Method.PUT: + case Method.PATCH: + case Method.MERGE: return Execute(request, method, DoExecuteAsPost); default: return Execute(request, method, DoExecuteAsGet); From 2cddedb20cdff28fc96b12df1eb346ae7ee05cfc Mon Sep 17 00:00:00 2001 From: scidec Date: Thu, 17 Jul 2014 11:36:28 -0600 Subject: [PATCH 2/5] Update Enum.cs --- RestSharp/Enum.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/RestSharp/Enum.cs b/RestSharp/Enum.cs index 5f6248db8..942dac2e3 100644 --- a/RestSharp/Enum.cs +++ b/RestSharp/Enum.cs @@ -50,7 +50,7 @@ public enum Method HEAD, OPTIONS, PATCH, - MERGE, + MERGE, } /// From ca7e25ce9b4cd6d1282b55ee368b5cd4cd5d0e8f Mon Sep 17 00:00:00 2001 From: scidec Date: Thu, 17 Jul 2014 11:39:22 -0600 Subject: [PATCH 3/5] Fix spaces vs tabs. --- RestSharp/Http.Async.cs | 920 +++++++++++++++++----------------- RestSharp/Http.Sync.cs | 612 +++++++++++----------- RestSharp/Http.cs | 907 ++++++++++++++++----------------- RestSharp/IHttp.cs | 178 +++---- RestSharp/RestClient.Async.cs | 792 ++++++++++++++--------------- RestSharp/RestClient.Sync.cs | 234 ++++----- 6 files changed, 1823 insertions(+), 1820 deletions(-) diff --git a/RestSharp/Http.Async.cs b/RestSharp/Http.Async.cs index 4b8bd121c..ab29d68ad 100644 --- a/RestSharp/Http.Async.cs +++ b/RestSharp/Http.Async.cs @@ -1,460 +1,460 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -using System; -using System.Net; -using System.Threading; -using RestSharp.Extensions; - -#if SILVERLIGHT -using System.Windows.Browser; -using System.Net.Browser; -#endif - -#if WINDOWS_PHONE -using System.Windows.Threading; -using System.Windows; -#endif - -#if (FRAMEWORK && !MONOTOUCH && !MONODROID && !PocketPC) -using System.Web; -#endif - -namespace RestSharp -{ - /// - /// HttpWebRequest wrapper (async methods) - /// - public partial class Http - { - private TimeOutState _timeoutState; - - public HttpWebRequest DeleteAsync(Action action) - { - return GetStyleMethodInternalAsync("DELETE", action); - } - - public HttpWebRequest GetAsync(Action action) - { - return GetStyleMethodInternalAsync("GET", action); - } - - public HttpWebRequest HeadAsync(Action action) - { - return GetStyleMethodInternalAsync("HEAD", action); - } - - public HttpWebRequest OptionsAsync(Action action) - { - return GetStyleMethodInternalAsync("OPTIONS", action); - } - - public HttpWebRequest PostAsync(Action action) - { - return PutPostInternalAsync("POST", action); - } - - public HttpWebRequest PutAsync(Action action) - { - return PutPostInternalAsync("PUT", action); - } - - public HttpWebRequest PatchAsync(Action action) - { - return PutPostInternalAsync("PATCH", action); - } - - public HttpWebRequest MergeAsync(Action action) - { - return PutPostInternalAsync("MERGE", action); - } - - /// - /// Execute an async POST-style request with the specified HTTP Method. - /// - /// The HTTP method to execute. - /// - public HttpWebRequest AsPostAsync(Action action, string httpMethod) - { -#if PocketPC - return PutPostInternalAsync(httpMethod.ToUpper(), action); -#else - return PutPostInternalAsync(httpMethod.ToUpperInvariant(), action); -#endif - } - - /// - /// Execute an async GET-style request with the specified HTTP Method. - /// - /// The HTTP method to execute. - /// - public HttpWebRequest AsGetAsync(Action action, string httpMethod) - { -#if PocketPC - return GetStyleMethodInternalAsync(httpMethod.ToUpper(), action); -#else - return GetStyleMethodInternalAsync(httpMethod.ToUpperInvariant(), action); -#endif - } - - private HttpWebRequest GetStyleMethodInternalAsync(string method, Action callback) - { - HttpWebRequest webRequest = null; - try - { - var url = Url; - webRequest = ConfigureAsyncWebRequest(method, url); - if (HasBody && (method == "DELETE" || method == "OPTIONS")) - { - webRequest.ContentType = RequestContentType; - WriteRequestBodyAsync(webRequest, callback); - } - else - { - _timeoutState = new TimeOutState { Request = webRequest }; - var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest); - SetTimeout(asyncResult, _timeoutState); - } - } - catch(Exception ex) - { - ExecuteCallback(CreateErrorResponse(ex), callback); - } - return webRequest; - } - - private HttpResponse CreateErrorResponse(Exception ex) - { - var response = new HttpResponse(); - var webException = ex as WebException; - if (webException != null && webException.Status == WebExceptionStatus.RequestCanceled) - { - response.ResponseStatus = _timeoutState.TimedOut ? ResponseStatus.TimedOut : ResponseStatus.Aborted; - return response; - } - - response.ErrorMessage = ex.Message; - response.ErrorException = ex; - response.ResponseStatus = ResponseStatus.Error; - return response; - } - - private HttpWebRequest PutPostInternalAsync(string method, Action callback) - { - HttpWebRequest webRequest = null; - try - { - webRequest = ConfigureAsyncWebRequest(method, Url); - PreparePostBody(webRequest); - WriteRequestBodyAsync(webRequest, callback); - } - catch(Exception ex) - { - ExecuteCallback(CreateErrorResponse(ex), callback); - } - - return webRequest; - } - - private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action callback) - { - IAsyncResult asyncResult; - _timeoutState = new TimeOutState { Request = webRequest }; - - if (HasBody || HasFiles || AlwaysMultipartFormData) - { -#if !WINDOWS_PHONE && !PocketPC - webRequest.ContentLength = CalculateContentLength(); -#endif - asyncResult = webRequest.BeginGetRequestStream(result => RequestStreamCallback(result, callback), webRequest); - } - - else - { - asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest); - } - - SetTimeout(asyncResult, _timeoutState); - } - - private long CalculateContentLength() - { - if (RequestBodyBytes != null) - return RequestBodyBytes.Length; - - if (!HasFiles && !AlwaysMultipartFormData) - { - return _defaultEncoding.GetByteCount(RequestBody); - } - - // calculate length for multipart form - long length = 0; - foreach (var file in Files) - { - length += _defaultEncoding.GetByteCount(GetMultipartFileHeader(file)); - length += file.ContentLength; - length += _defaultEncoding.GetByteCount(_lineBreak); - } - - foreach (var param in Parameters) - { - length += _defaultEncoding.GetByteCount(GetMultipartFormData(param)); - } - - length += _defaultEncoding.GetByteCount(GetMultipartFooter()); - return length; - } - - private void RequestStreamCallback(IAsyncResult result, Action callback) - { - var webRequest = (HttpWebRequest)result.AsyncState; - - if (_timeoutState.TimedOut) - { - var response = new HttpResponse {ResponseStatus = ResponseStatus.TimedOut}; - ExecuteCallback(response, callback); - return; - } - - // write body to request stream - try - { - using(var requestStream = webRequest.EndGetRequestStream(result)) - { - if(HasFiles || AlwaysMultipartFormData) - { - WriteMultipartFormData(requestStream); - } - else if (RequestBodyBytes != null) - { - requestStream.Write(RequestBodyBytes, 0, RequestBodyBytes.Length); - } - else - { - WriteStringTo(requestStream, RequestBody); - } - } - } - catch (Exception ex) - { - ExecuteCallback(CreateErrorResponse(ex), callback); - return; - } - - IAsyncResult asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest); - SetTimeout(asyncResult, _timeoutState); - } - - private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState) - { -#if FRAMEWORK && !PocketPC - if (Timeout != 0) - { - ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), timeOutState, Timeout, true); - } -#endif - } - - private static void TimeoutCallback(object state, bool timedOut) - { - if (!timedOut) - return; - - var timeoutState = state as TimeOutState; - - if (timeoutState == null) - { - return; - } - - lock (timeoutState) - { - timeoutState.TimedOut = true; - } - - if (timeoutState.Request != null) - { - timeoutState.Request.Abort(); - } - } - - private static void GetRawResponseAsync(IAsyncResult result, Action callback) - { - var response = new HttpResponse(); - response.ResponseStatus = ResponseStatus.None; - - HttpWebResponse raw = null; - - try - { - var webRequest = (HttpWebRequest)result.AsyncState; - raw = webRequest.EndGetResponse(result) as HttpWebResponse; - } - catch(WebException ex) - { - if(ex.Status == WebExceptionStatus.RequestCanceled) - { - throw ex; - } - - // Check to see if this is an HTTP error or a transport error. - // In cases where an HTTP error occurs ( status code >= 400 ) - // return the underlying HTTP response, otherwise assume a - // transport exception (ex: connection timeout) and - // rethrow the exception - - if (ex.Response is HttpWebResponse) - { - raw = ex.Response as HttpWebResponse; - } - else - { - throw ex; - } - } - - callback(raw); - raw.Close(); - } - - private void ResponseCallback(IAsyncResult result, Action callback) - { - var response = new HttpResponse {ResponseStatus = ResponseStatus.None}; - - try - { - if(_timeoutState.TimedOut) - { - response.ResponseStatus = ResponseStatus.TimedOut; - ExecuteCallback(response, callback); - return; - } - - GetRawResponseAsync(result, webResponse => - { - ExtractResponseData(response, webResponse); - ExecuteCallback(response, callback); - }); - } - catch(Exception ex) - { - ExecuteCallback(CreateErrorResponse(ex), callback); - } - } - - private static void ExecuteCallback(HttpResponse response, Action callback) - { - callback(response); - } - - partial void AddAsyncHeaderActions() - { -#if SILVERLIGHT - _restrictedHeaderActions.Add("Content-Length", (r, v) => r.ContentLength = Convert.ToInt64(v)); -#endif -#if WINDOWS_PHONE - // WP7 doesn't as of Beta doesn't support a way to set Content-Length either directly - // or indirectly - _restrictedHeaderActions.Add("Content-Length", (r, v) => { }); -#endif - } - - // TODO: Try to merge the shared parts between ConfigureWebRequest and ConfigureAsyncWebRequest (quite a bit of code - // TODO: duplication at the moment). - private HttpWebRequest ConfigureAsyncWebRequest(string method, Uri url) - { -#if SILVERLIGHT - WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp); - WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp); -#endif - var webRequest = (HttpWebRequest)WebRequest.Create(url); -#if !PocketPC - webRequest.UseDefaultCredentials = UseDefaultCredentials; -#endif - -#if !WINDOWS_PHONE && !SILVERLIGHT - webRequest.PreAuthenticate = PreAuthenticate; -#endif - AppendHeaders(webRequest); - AppendCookies(webRequest); - - webRequest.Method = method; - - // make sure Content-Length header is always sent since default is -1 -#if !WINDOWS_PHONE && !PocketPC - // WP7 doesn't as of Beta doesn't support a way to set this value either directly - // or indirectly - if(!HasFiles && !AlwaysMultipartFormData) - { - webRequest.ContentLength = 0; - } -#endif - - if(Credentials != null) - { - webRequest.Credentials = Credentials; - } - -#if !SILVERLIGHT - if(UserAgent.HasValue()) - { - webRequest.UserAgent = UserAgent; - } -#endif - -#if FRAMEWORK - if(ClientCertificates != null) - { - webRequest.ClientCertificates.AddRange(ClientCertificates); - } - - webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None; - ServicePointManager.Expect100Continue = false; - - if (Timeout != 0) - { - webRequest.Timeout = Timeout; - } - - if (ReadWriteTimeout != 0) - { - webRequest.ReadWriteTimeout = ReadWriteTimeout; - } - - if (Proxy != null) - { - webRequest.Proxy = Proxy; - } - - if (FollowRedirects && MaxRedirects.HasValue) - { - webRequest.MaximumAutomaticRedirections = MaxRedirects.Value; - } -#endif - -#if !SILVERLIGHT - webRequest.AllowAutoRedirect = FollowRedirects; -#endif - return webRequest; - } - - private class TimeOutState - { - public bool TimedOut { get; set; } - public HttpWebRequest Request { get; set; } - } - } -} +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +using System; +using System.Net; +using System.Threading; +using RestSharp.Extensions; + +#if SILVERLIGHT +using System.Windows.Browser; +using System.Net.Browser; +#endif + +#if WINDOWS_PHONE +using System.Windows.Threading; +using System.Windows; +#endif + +#if (FRAMEWORK && !MONOTOUCH && !MONODROID && !PocketPC) +using System.Web; +#endif + +namespace RestSharp +{ + /// + /// HttpWebRequest wrapper (async methods) + /// + public partial class Http + { + private TimeOutState _timeoutState; + + public HttpWebRequest DeleteAsync(Action action) + { + return GetStyleMethodInternalAsync("DELETE", action); + } + + public HttpWebRequest GetAsync(Action action) + { + return GetStyleMethodInternalAsync("GET", action); + } + + public HttpWebRequest HeadAsync(Action action) + { + return GetStyleMethodInternalAsync("HEAD", action); + } + + public HttpWebRequest OptionsAsync(Action action) + { + return GetStyleMethodInternalAsync("OPTIONS", action); + } + + public HttpWebRequest PostAsync(Action action) + { + return PutPostInternalAsync("POST", action); + } + + public HttpWebRequest PutAsync(Action action) + { + return PutPostInternalAsync("PUT", action); + } + + public HttpWebRequest PatchAsync(Action action) + { + return PutPostInternalAsync("PATCH", action); + } + + public HttpWebRequest MergeAsync(Action action) + { + return PutPostInternalAsync("MERGE", action); + } + + /// + /// Execute an async POST-style request with the specified HTTP Method. + /// + /// The HTTP method to execute. + /// + public HttpWebRequest AsPostAsync(Action action, string httpMethod) + { +#if PocketPC + return PutPostInternalAsync(httpMethod.ToUpper(), action); +#else + return PutPostInternalAsync(httpMethod.ToUpperInvariant(), action); +#endif + } + + /// + /// Execute an async GET-style request with the specified HTTP Method. + /// + /// The HTTP method to execute. + /// + public HttpWebRequest AsGetAsync(Action action, string httpMethod) + { +#if PocketPC + return GetStyleMethodInternalAsync(httpMethod.ToUpper(), action); +#else + return GetStyleMethodInternalAsync(httpMethod.ToUpperInvariant(), action); +#endif + } + + private HttpWebRequest GetStyleMethodInternalAsync(string method, Action callback) + { + HttpWebRequest webRequest = null; + try + { + var url = Url; + webRequest = ConfigureAsyncWebRequest(method, url); + if (HasBody && (method == "DELETE" || method == "OPTIONS")) + { + webRequest.ContentType = RequestContentType; + WriteRequestBodyAsync(webRequest, callback); + } + else + { + _timeoutState = new TimeOutState { Request = webRequest }; + var asyncResult = webRequest.BeginGetResponse(result => ResponseCallback(result, callback), webRequest); + SetTimeout(asyncResult, _timeoutState); + } + } + catch (Exception ex) + { + ExecuteCallback(CreateErrorResponse(ex), callback); + } + return webRequest; + } + + private HttpResponse CreateErrorResponse(Exception ex) + { + var response = new HttpResponse(); + var webException = ex as WebException; + if (webException != null && webException.Status == WebExceptionStatus.RequestCanceled) + { + response.ResponseStatus = _timeoutState.TimedOut ? ResponseStatus.TimedOut : ResponseStatus.Aborted; + return response; + } + + response.ErrorMessage = ex.Message; + response.ErrorException = ex; + response.ResponseStatus = ResponseStatus.Error; + return response; + } + + private HttpWebRequest PutPostInternalAsync(string method, Action callback) + { + HttpWebRequest webRequest = null; + try + { + webRequest = ConfigureAsyncWebRequest(method, Url); + PreparePostBody(webRequest); + WriteRequestBodyAsync(webRequest, callback); + } + catch (Exception ex) + { + ExecuteCallback(CreateErrorResponse(ex), callback); + } + + return webRequest; + } + + private void WriteRequestBodyAsync(HttpWebRequest webRequest, Action callback) + { + IAsyncResult asyncResult; + _timeoutState = new TimeOutState { Request = webRequest }; + + if (HasBody || HasFiles || AlwaysMultipartFormData) + { +#if !WINDOWS_PHONE && !PocketPC + webRequest.ContentLength = CalculateContentLength(); +#endif + asyncResult = webRequest.BeginGetRequestStream(result => RequestStreamCallback(result, callback), webRequest); + } + + else + { + asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest); + } + + SetTimeout(asyncResult, _timeoutState); + } + + private long CalculateContentLength() + { + if (RequestBodyBytes != null) + return RequestBodyBytes.Length; + + if (!HasFiles && !AlwaysMultipartFormData) + { + return _defaultEncoding.GetByteCount(RequestBody); + } + + // calculate length for multipart form + long length = 0; + foreach (var file in Files) + { + length += _defaultEncoding.GetByteCount(GetMultipartFileHeader(file)); + length += file.ContentLength; + length += _defaultEncoding.GetByteCount(_lineBreak); + } + + foreach (var param in Parameters) + { + length += _defaultEncoding.GetByteCount(GetMultipartFormData(param)); + } + + length += _defaultEncoding.GetByteCount(GetMultipartFooter()); + return length; + } + + private void RequestStreamCallback(IAsyncResult result, Action callback) + { + var webRequest = (HttpWebRequest)result.AsyncState; + + if (_timeoutState.TimedOut) + { + var response = new HttpResponse { ResponseStatus = ResponseStatus.TimedOut }; + ExecuteCallback(response, callback); + return; + } + + // write body to request stream + try + { + using (var requestStream = webRequest.EndGetRequestStream(result)) + { + if (HasFiles || AlwaysMultipartFormData) + { + WriteMultipartFormData(requestStream); + } + else if (RequestBodyBytes != null) + { + requestStream.Write(RequestBodyBytes, 0, RequestBodyBytes.Length); + } + else + { + WriteStringTo(requestStream, RequestBody); + } + } + } + catch (Exception ex) + { + ExecuteCallback(CreateErrorResponse(ex), callback); + return; + } + + IAsyncResult asyncResult = webRequest.BeginGetResponse(r => ResponseCallback(r, callback), webRequest); + SetTimeout(asyncResult, _timeoutState); + } + + private void SetTimeout(IAsyncResult asyncResult, TimeOutState timeOutState) + { +#if FRAMEWORK && !PocketPC + if (Timeout != 0) + { + ThreadPool.RegisterWaitForSingleObject(asyncResult.AsyncWaitHandle, new WaitOrTimerCallback(TimeoutCallback), timeOutState, Timeout, true); + } +#endif + } + + private static void TimeoutCallback(object state, bool timedOut) + { + if (!timedOut) + return; + + var timeoutState = state as TimeOutState; + + if (timeoutState == null) + { + return; + } + + lock (timeoutState) + { + timeoutState.TimedOut = true; + } + + if (timeoutState.Request != null) + { + timeoutState.Request.Abort(); + } + } + + private static void GetRawResponseAsync(IAsyncResult result, Action callback) + { + var response = new HttpResponse(); + response.ResponseStatus = ResponseStatus.None; + + HttpWebResponse raw = null; + + try + { + var webRequest = (HttpWebRequest)result.AsyncState; + raw = webRequest.EndGetResponse(result) as HttpWebResponse; + } + catch (WebException ex) + { + if (ex.Status == WebExceptionStatus.RequestCanceled) + { + throw ex; + } + + // Check to see if this is an HTTP error or a transport error. + // In cases where an HTTP error occurs ( status code >= 400 ) + // return the underlying HTTP response, otherwise assume a + // transport exception (ex: connection timeout) and + // rethrow the exception + + if (ex.Response is HttpWebResponse) + { + raw = ex.Response as HttpWebResponse; + } + else + { + throw ex; + } + } + + callback(raw); + raw.Close(); + } + + private void ResponseCallback(IAsyncResult result, Action callback) + { + var response = new HttpResponse { ResponseStatus = ResponseStatus.None }; + + try + { + if (_timeoutState.TimedOut) + { + response.ResponseStatus = ResponseStatus.TimedOut; + ExecuteCallback(response, callback); + return; + } + + GetRawResponseAsync(result, webResponse => + { + ExtractResponseData(response, webResponse); + ExecuteCallback(response, callback); + }); + } + catch (Exception ex) + { + ExecuteCallback(CreateErrorResponse(ex), callback); + } + } + + private static void ExecuteCallback(HttpResponse response, Action callback) + { + callback(response); + } + + partial void AddAsyncHeaderActions() + { +#if SILVERLIGHT + _restrictedHeaderActions.Add("Content-Length", (r, v) => r.ContentLength = Convert.ToInt64(v)); +#endif +#if WINDOWS_PHONE + // WP7 doesn't as of Beta doesn't support a way to set Content-Length either directly + // or indirectly + _restrictedHeaderActions.Add("Content-Length", (r, v) => { }); +#endif + } + + // TODO: Try to merge the shared parts between ConfigureWebRequest and ConfigureAsyncWebRequest (quite a bit of code + // TODO: duplication at the moment). + private HttpWebRequest ConfigureAsyncWebRequest(string method, Uri url) + { +#if SILVERLIGHT + WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp); + WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp); +#endif + var webRequest = (HttpWebRequest)WebRequest.Create(url); +#if !PocketPC + webRequest.UseDefaultCredentials = UseDefaultCredentials; +#endif + +#if !WINDOWS_PHONE && !SILVERLIGHT + webRequest.PreAuthenticate = PreAuthenticate; +#endif + AppendHeaders(webRequest); + AppendCookies(webRequest); + + webRequest.Method = method; + + // make sure Content-Length header is always sent since default is -1 +#if !WINDOWS_PHONE && !PocketPC + // WP7 doesn't as of Beta doesn't support a way to set this value either directly + // or indirectly + if (!HasFiles && !AlwaysMultipartFormData) + { + webRequest.ContentLength = 0; + } +#endif + + if (Credentials != null) + { + webRequest.Credentials = Credentials; + } + +#if !SILVERLIGHT + if (UserAgent.HasValue()) + { + webRequest.UserAgent = UserAgent; + } +#endif + +#if FRAMEWORK + if (ClientCertificates != null) + { + webRequest.ClientCertificates.AddRange(ClientCertificates); + } + + webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None; + ServicePointManager.Expect100Continue = false; + + if (Timeout != 0) + { + webRequest.Timeout = Timeout; + } + + if (ReadWriteTimeout != 0) + { + webRequest.ReadWriteTimeout = ReadWriteTimeout; + } + + if (Proxy != null) + { + webRequest.Proxy = Proxy; + } + + if (FollowRedirects && MaxRedirects.HasValue) + { + webRequest.MaximumAutomaticRedirections = MaxRedirects.Value; + } +#endif + +#if !SILVERLIGHT + webRequest.AllowAutoRedirect = FollowRedirects; +#endif + return webRequest; + } + + private class TimeOutState + { + public bool TimedOut { get; set; } + public HttpWebRequest Request { get; set; } + } + } +} \ No newline at end of file diff --git a/RestSharp/Http.Sync.cs b/RestSharp/Http.Sync.cs index 91539edc2..7380b23b0 100644 --- a/RestSharp/Http.Sync.cs +++ b/RestSharp/Http.Sync.cs @@ -1,309 +1,309 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -#if FRAMEWORK || PocketPC -using System; -using System.Net; - -#if !MONOTOUCH && !MONODROID && !PocketPC -using System.Web; -#endif - -using RestSharp.Extensions; - -namespace RestSharp -{ - /// - /// HttpWebRequest wrapper (sync methods) - /// - public partial class Http - { - /// - /// Execute a POST request - /// - public HttpResponse Post() - { - return PostPutInternal("POST"); - } - - /// - /// Execute a PUT request - /// - public HttpResponse Put() - { - return PostPutInternal("PUT"); - } - - /// - /// Execute a GET request - /// - public HttpResponse Get() - { - return GetStyleMethodInternal("GET"); - } - - /// - /// Execute a HEAD request - /// - public HttpResponse Head() - { - return GetStyleMethodInternal("HEAD"); - } - - /// - /// Execute an OPTIONS request - /// - public HttpResponse Options() - { - return GetStyleMethodInternal("OPTIONS"); - } - - /// - /// Execute a DELETE request - /// - public HttpResponse Delete() - { - return GetStyleMethodInternal("DELETE"); - } - - /// - /// Execute a PATCH request - /// - public HttpResponse Patch() - { - return PostPutInternal("PATCH"); +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +#if FRAMEWORK || PocketPC +using System; +using System.Net; + +#if !MONOTOUCH && !MONODROID && !PocketPC +using System.Web; +#endif + +using RestSharp.Extensions; + +namespace RestSharp +{ + /// + /// HttpWebRequest wrapper (sync methods) + /// + public partial class Http + { + /// + /// Execute a POST request + /// + public HttpResponse Post() + { + return PostPutInternal("POST"); + } + + /// + /// Execute a PUT request + /// + public HttpResponse Put() + { + return PostPutInternal("PUT"); + } + + /// + /// Execute a GET request + /// + public HttpResponse Get() + { + return GetStyleMethodInternal("GET"); + } + + /// + /// Execute a HEAD request + /// + public HttpResponse Head() + { + return GetStyleMethodInternal("HEAD"); + } + + /// + /// Execute an OPTIONS request + /// + public HttpResponse Options() + { + return GetStyleMethodInternal("OPTIONS"); + } + + /// + /// Execute a DELETE request + /// + public HttpResponse Delete() + { + return GetStyleMethodInternal("DELETE"); + } + + /// + /// Execute a PATCH request + /// + public HttpResponse Patch() + { + return PostPutInternal("PATCH"); + } + + /// + /// Execute a MERGE request + /// + public HttpResponse Merge() + { + return PostPutInternal("MERGE"); + } + + /// + /// Execute a GET-style request with the specified HTTP Method. + /// + /// The HTTP method to execute. + /// + public HttpResponse AsGet(string httpMethod) + { +#if PocketPC + return GetStyleMethodInternal(httpMethod.ToUpper()); +#else + return GetStyleMethodInternal(httpMethod.ToUpperInvariant()); +#endif + } + + /// + /// Execute a POST-style request with the specified HTTP Method. + /// + /// The HTTP method to execute. + /// + public HttpResponse AsPost(string httpMethod) + { +#if PocketPC + return PostPutInternal(httpMethod.ToUpper()); +#else + return PostPutInternal(httpMethod.ToUpperInvariant()); +#endif + } + + private HttpResponse GetStyleMethodInternal(string method) + { + var webRequest = ConfigureWebRequest(method, Url); + + if (HasBody && (method == "DELETE" || method == "OPTIONS")) + { + webRequest.ContentType = RequestContentType; + WriteRequestBody(webRequest); + } + + return GetResponse(webRequest); + } + + private HttpResponse PostPutInternal(string method) + { + var webRequest = ConfigureWebRequest(method, Url); + + PreparePostData(webRequest); + + WriteRequestBody(webRequest); + return GetResponse(webRequest); + } + + partial void AddSyncHeaderActions() + { + _restrictedHeaderActions.Add("Connection", (r, v) => r.Connection = v); + _restrictedHeaderActions.Add("Content-Length", (r, v) => r.ContentLength = Convert.ToInt64(v)); + _restrictedHeaderActions.Add("Expect", (r, v) => r.Expect = v); + _restrictedHeaderActions.Add("If-Modified-Since", (r, v) => r.IfModifiedSince = Convert.ToDateTime(v)); + _restrictedHeaderActions.Add("Referer", (r, v) => r.Referer = v); + _restrictedHeaderActions.Add("Transfer-Encoding", (r, v) => { r.TransferEncoding = v; r.SendChunked = true; }); + _restrictedHeaderActions.Add("User-Agent", (r, v) => r.UserAgent = v); + } + + private void ExtractErrorResponse(HttpResponse httpResponse, Exception ex) + { + var webException = ex as WebException; + + if (webException != null && webException.Status == WebExceptionStatus.Timeout) + { + httpResponse.ResponseStatus = ResponseStatus.TimedOut; + httpResponse.ErrorMessage = ex.Message; + httpResponse.ErrorException = webException; + return; + } + + httpResponse.ErrorMessage = ex.Message; + httpResponse.ErrorException = ex; + httpResponse.ResponseStatus = ResponseStatus.Error; + } + + private HttpResponse GetResponse(HttpWebRequest request) + { + var response = new HttpResponse { ResponseStatus = ResponseStatus.None }; + + try + { + var webResponse = GetRawResponse(request); + ExtractResponseData(response, webResponse); + } + catch (Exception ex) + { + ExtractErrorResponse(response, ex); + } + + return response; + } + + private static HttpWebResponse GetRawResponse(HttpWebRequest request) + { + try + { + return (HttpWebResponse)request.GetResponse(); + } + catch (WebException ex) + { + // Check to see if this is an HTTP error or a transport error. + // In cases where an HTTP error occurs ( status code >= 400 ) + // return the underlying HTTP response, otherwise assume a + // transport exception (ex: connection timeout) and + // rethrow the exception + + if (ex.Response is HttpWebResponse) + { + return ex.Response as HttpWebResponse; + } + throw; + } } - /// - /// Execute a MERGE request - /// - public HttpResponse Merge() - { - return PostPutInternal("MERGE"); - } - - /// - /// Execute a GET-style request with the specified HTTP Method. - /// - /// The HTTP method to execute. - /// - public HttpResponse AsGet(string httpMethod) - { -#if PocketPC - return GetStyleMethodInternal(httpMethod.ToUpper()); -#else - return GetStyleMethodInternal(httpMethod.ToUpperInvariant()); -#endif - } - - /// - /// Execute a POST-style request with the specified HTTP Method. - /// - /// The HTTP method to execute. - /// - public HttpResponse AsPost(string httpMethod) - { -#if PocketPC - return PostPutInternal(httpMethod.ToUpper()); -#else - return PostPutInternal(httpMethod.ToUpperInvariant()); -#endif - } - - private HttpResponse GetStyleMethodInternal(string method) - { - var webRequest = ConfigureWebRequest(method, Url); - - if (HasBody && (method == "DELETE" || method == "OPTIONS")) - { - webRequest.ContentType = RequestContentType; - WriteRequestBody(webRequest); - } - - return GetResponse(webRequest); - } - - private HttpResponse PostPutInternal(string method) - { - var webRequest = ConfigureWebRequest(method, Url); - - PreparePostData(webRequest); - - WriteRequestBody(webRequest); - return GetResponse(webRequest); - } - - partial void AddSyncHeaderActions() - { - _restrictedHeaderActions.Add("Connection", (r, v) => r.Connection = v); - _restrictedHeaderActions.Add("Content-Length", (r, v) => r.ContentLength = Convert.ToInt64(v)); - _restrictedHeaderActions.Add("Expect", (r, v) => r.Expect = v); - _restrictedHeaderActions.Add("If-Modified-Since", (r, v) => r.IfModifiedSince = Convert.ToDateTime(v)); - _restrictedHeaderActions.Add("Referer", (r, v) => r.Referer = v); - _restrictedHeaderActions.Add("Transfer-Encoding", (r, v) => { r.TransferEncoding = v; r.SendChunked = true; }); - _restrictedHeaderActions.Add("User-Agent", (r, v) => r.UserAgent = v); - } - - private void ExtractErrorResponse(HttpResponse httpResponse, Exception ex) - { - var webException = ex as WebException; - - if (webException != null && webException.Status == WebExceptionStatus.Timeout) - { - httpResponse.ResponseStatus = ResponseStatus.TimedOut; - httpResponse.ErrorMessage = ex.Message; - httpResponse.ErrorException = webException; - return; - } - - httpResponse.ErrorMessage = ex.Message; - httpResponse.ErrorException = ex; - httpResponse.ResponseStatus = ResponseStatus.Error; - } - - private HttpResponse GetResponse(HttpWebRequest request) - { - var response = new HttpResponse { ResponseStatus = ResponseStatus.None }; - - try - { - var webResponse = GetRawResponse(request); - ExtractResponseData(response, webResponse); - } - catch (Exception ex) - { - ExtractErrorResponse(response, ex); - } - - return response; - } - - private static HttpWebResponse GetRawResponse(HttpWebRequest request) - { - try - { - return (HttpWebResponse)request.GetResponse(); - } - catch (WebException ex) - { - // Check to see if this is an HTTP error or a transport error. - // In cases where an HTTP error occurs ( status code >= 400 ) - // return the underlying HTTP response, otherwise assume a - // transport exception (ex: connection timeout) and - // rethrow the exception - - if (ex.Response is HttpWebResponse) - { - return ex.Response as HttpWebResponse; - } - throw; - } - } - - private void PreparePostData(HttpWebRequest webRequest) - { - if (HasFiles || AlwaysMultipartFormData) - { - webRequest.ContentType = GetMultipartFormContentType(); - using (var requestStream = webRequest.GetRequestStream()) - { - WriteMultipartFormData(requestStream); - } - } - - PreparePostBody(webRequest); - } - - private void WriteRequestBody(HttpWebRequest webRequest) - { - if (!HasBody) - return; - - var bytes = RequestBodyBytes == null ? _defaultEncoding.GetBytes(RequestBody) : RequestBodyBytes; - - webRequest.ContentLength = bytes.Length; - - using (var requestStream = webRequest.GetRequestStream()) - { - requestStream.Write(bytes, 0, bytes.Length); - } - } - - // TODO: Try to merge the shared parts between ConfigureWebRequest and ConfigureAsyncWebRequest (quite a bit of code - // TODO: duplication at the moment). - private HttpWebRequest ConfigureWebRequest(string method, Uri url) - { - var webRequest = (HttpWebRequest)WebRequest.Create(url); -#if !PocketPC - webRequest.UseDefaultCredentials = UseDefaultCredentials; -#endif - webRequest.PreAuthenticate = PreAuthenticate; - ServicePointManager.Expect100Continue = false; - - AppendHeaders(webRequest); - AppendCookies(webRequest); - - webRequest.Method = method; - - // make sure Content-Length header is always sent since default is -1 - if (!HasFiles && !AlwaysMultipartFormData) - { - webRequest.ContentLength = 0; - } - - webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None; -#if FRAMEWORK - if(ClientCertificates != null) - { - webRequest.ClientCertificates.AddRange(ClientCertificates); - } -#endif - if(UserAgent.HasValue()) - { - webRequest.UserAgent = UserAgent; - } - - if(Timeout != 0) - { - webRequest.Timeout = Timeout; - } - - if (ReadWriteTimeout != 0) - { - webRequest.ReadWriteTimeout = ReadWriteTimeout; - } - - if(Credentials != null) - { - webRequest.Credentials = Credentials; - } - - if(Proxy != null) - { - webRequest.Proxy = Proxy; - } - - webRequest.AllowAutoRedirect = FollowRedirects; - if(FollowRedirects && MaxRedirects.HasValue) - { - webRequest.MaximumAutomaticRedirections = MaxRedirects.Value; - } - - return webRequest; - } - } -} + private void PreparePostData(HttpWebRequest webRequest) + { + if (HasFiles || AlwaysMultipartFormData) + { + webRequest.ContentType = GetMultipartFormContentType(); + using (var requestStream = webRequest.GetRequestStream()) + { + WriteMultipartFormData(requestStream); + } + } + + PreparePostBody(webRequest); + } + + private void WriteRequestBody(HttpWebRequest webRequest) + { + if (!HasBody) + return; + + var bytes = RequestBodyBytes == null ? _defaultEncoding.GetBytes(RequestBody) : RequestBodyBytes; + + webRequest.ContentLength = bytes.Length; + + using (var requestStream = webRequest.GetRequestStream()) + { + requestStream.Write(bytes, 0, bytes.Length); + } + } + + // TODO: Try to merge the shared parts between ConfigureWebRequest and ConfigureAsyncWebRequest (quite a bit of code + // TODO: duplication at the moment). + private HttpWebRequest ConfigureWebRequest(string method, Uri url) + { + var webRequest = (HttpWebRequest)WebRequest.Create(url); +#if !PocketPC + webRequest.UseDefaultCredentials = UseDefaultCredentials; +#endif + webRequest.PreAuthenticate = PreAuthenticate; + ServicePointManager.Expect100Continue = false; + + AppendHeaders(webRequest); + AppendCookies(webRequest); + + webRequest.Method = method; + + // make sure Content-Length header is always sent since default is -1 + if (!HasFiles && !AlwaysMultipartFormData) + { + webRequest.ContentLength = 0; + } + + webRequest.AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip | DecompressionMethods.None; +#if FRAMEWORK + if (ClientCertificates != null) + { + webRequest.ClientCertificates.AddRange(ClientCertificates); + } +#endif + if (UserAgent.HasValue()) + { + webRequest.UserAgent = UserAgent; + } + + if (Timeout != 0) + { + webRequest.Timeout = Timeout; + } + + if (ReadWriteTimeout != 0) + { + webRequest.ReadWriteTimeout = ReadWriteTimeout; + } + + if (Credentials != null) + { + webRequest.Credentials = Credentials; + } + + if (Proxy != null) + { + webRequest.Proxy = Proxy; + } + + webRequest.AllowAutoRedirect = FollowRedirects; + if (FollowRedirects && MaxRedirects.HasValue) + { + webRequest.MaximumAutomaticRedirections = MaxRedirects.Value; + } + + return webRequest; + } + } +} #endif \ No newline at end of file diff --git a/RestSharp/Http.cs b/RestSharp/Http.cs index 7c6763807..d8e8f5d7c 100644 --- a/RestSharp/Http.cs +++ b/RestSharp/Http.cs @@ -1,453 +1,456 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Security.Cryptography.X509Certificates; -using System.Text; -using RestSharp.Extensions; - -#if WINDOWS_PHONE -using RestSharp.Compression.ZLib; -#endif - -namespace RestSharp -{ - /// - /// HttpWebRequest wrapper - /// - public partial class Http : IHttp, IHttpFactory - { - private const string _lineBreak = "\r\n"; - private static readonly Encoding _defaultEncoding = Encoding.UTF8; - - /// - /// Creates an IHttp - /// - /// - public IHttp Create() - { - return new Http(); - } - - /// - /// True if this HTTP request has any HTTP parameters - /// - protected bool HasParameters - { - get - { - return Parameters.Any(); - } - } - - /// - /// True if this HTTP request has any HTTP cookies - /// - protected bool HasCookies - { - get - { - return Cookies.Any(); - } - } - - /// - /// True if a request body has been specified - /// - protected bool HasBody - { - get - { - return RequestBodyBytes != null || !string.IsNullOrEmpty(RequestBody); - } - } - - /// - /// True if files have been set to be uploaded - /// - protected bool HasFiles - { - get - { - return Files.Any(); - } - } - - /// - /// Always send a multipart/form-data request - even when no Files are present. - /// - public bool AlwaysMultipartFormData { get; set; } - - /// - /// UserAgent to be sent with request - /// - public string UserAgent { get; set; } - /// - /// Timeout in milliseconds to be used for the request - /// - public int Timeout { get; set; } - /// - /// The number of milliseconds before the writing or reading times out. - /// - public int ReadWriteTimeout { get; set; } - /// - /// System.Net.ICredentials to be sent with request - /// - public ICredentials Credentials { get; set; } - /// - /// The System.Net.CookieContainer to be used for the request - /// -#if !PocketPC - public CookieContainer CookieContainer { get; set; } -#endif - /// - /// The method to use to write the response instead of reading into RawBytes - /// - public Action ResponseWriter { get; set; } - /// - /// Collection of files to be sent with request - /// - public IList Files { get; private set; } -#if !SILVERLIGHT - /// - /// Whether or not HTTP 3xx response redirects should be automatically followed - /// - public bool FollowRedirects { get; set; } -#endif -#if FRAMEWORK - /// - /// X509CertificateCollection to be sent with request - /// - public X509CertificateCollection ClientCertificates { get; set; } -#endif -#if FRAMEWORK || PocketPC - /// - /// Maximum number of automatic redirects to follow if FollowRedirects is true - /// - public int? MaxRedirects { get; set; } -#endif - /// - /// Determine whether or not the "default credentials" (e.g. the user account under which the current process is running) - /// will be sent along to the server. - /// -#if !PocketPC - public bool UseDefaultCredentials { get; set; } -#endif - /// - /// HTTP headers to be sent with request - /// - public IList Headers { get; private set; } - /// - /// HTTP parameters (QueryString or Form values) to be sent with request - /// - public IList Parameters { get; private set; } - /// - /// HTTP cookies to be sent with request - /// - public IList Cookies { get; private set; } - /// - /// Request body to be sent with request - /// - public string RequestBody { get; set; } - /// - /// Content type of the request body. - /// - public string RequestContentType { get; set; } - /// - /// An alternative to RequestBody, for when the caller already has the byte array. - /// - public byte[] RequestBodyBytes { get; set; } - /// - /// URL to call for this request - /// - public Uri Url { get; set; } - - public bool PreAuthenticate { get; set; } - -#if FRAMEWORK || PocketPC - /// - /// Proxy info to be sent with request - /// - public IWebProxy Proxy { get; set; } -#endif - - /// - /// Default constructor - /// - public Http() - { - Headers = new List(); - Files = new List(); - Parameters = new List(); - Cookies = new List(); - - _restrictedHeaderActions = new Dictionary>(StringComparer.OrdinalIgnoreCase); - - AddSharedHeaderActions(); - AddSyncHeaderActions(); - } - - partial void AddSyncHeaderActions(); - partial void AddAsyncHeaderActions(); - private void AddSharedHeaderActions() - { - _restrictedHeaderActions.Add("Accept", (r, v) => r.Accept = v); - _restrictedHeaderActions.Add("Content-Type", (r, v) => r.ContentType = v); -#if NET4 - _restrictedHeaderActions.Add("Date", (r, v) => - { - DateTime parsed; - if (DateTime.TryParse(v, out parsed)) - { - r.Date = parsed; - } - }); - _restrictedHeaderActions.Add("Host", (r, v) => r.Host = v); -#else - _restrictedHeaderActions.Add("Date", (r, v) => { /* Set by system */ }); - _restrictedHeaderActions.Add("Host", (r, v) => { /* Set by system */ }); -#endif -#if FRAMEWORK - _restrictedHeaderActions.Add("Range", (r, v) => { AddRange(r, v); }); -#endif - } - - private const string FormBoundary = "-----------------------------28947758029299"; - private static string GetMultipartFormContentType() - { - return string.Format("multipart/form-data; boundary={0}", FormBoundary); - } - - private static string GetMultipartFileHeader (HttpFile file) - { - return string.Format ("--{0}{4}Content-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"{4}Content-Type: {3}{4}{4}", - FormBoundary, file.Name, file.FileName, file.ContentType ?? "application/octet-stream", _lineBreak); - } - - private static string GetMultipartFormData (HttpParameter param) - { - return string.Format ("--{0}{3}Content-Disposition: form-data; name=\"{1}\"{3}{3}{2}{3}", - FormBoundary, param.Name, param.Value, _lineBreak); - } - - private static string GetMultipartFooter () - { - return string.Format ("--{0}--{1}", FormBoundary, _lineBreak); - } - - private readonly IDictionary> _restrictedHeaderActions; - - // handle restricted headers the .NET way - thanks @dimebrain! - // http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx - private void AppendHeaders(HttpWebRequest webRequest) - { - foreach (var header in Headers) - { - if (_restrictedHeaderActions.ContainsKey(header.Name)) - { - _restrictedHeaderActions[header.Name].Invoke(webRequest, header.Value); - } - else - { -#if FRAMEWORK - webRequest.Headers.Add(header.Name, header.Value); -#else - webRequest.Headers[header.Name] = header.Value; -#endif - } - } - } - - private void AppendCookies(HttpWebRequest webRequest) - { -#if !PocketPC - webRequest.CookieContainer = this.CookieContainer ?? new CookieContainer(); -#endif - foreach (var httpCookie in Cookies) - { -#if !PocketPC -#if FRAMEWORK - var cookie = new Cookie - { - Name = httpCookie.Name, - Value = httpCookie.Value, - Domain = webRequest.RequestUri.Host - }; - webRequest.CookieContainer.Add(cookie); -#else - var cookie = new Cookie - { - Name = httpCookie.Name, - Value = httpCookie.Value - }; - var uri = webRequest.RequestUri; - webRequest.CookieContainer.Add(new Uri(string.Format("{0}://{1}", uri.Scheme, uri.Host)), cookie); -#endif -#endif - } - } - - private string EncodeParameters() - { - var querystring = new StringBuilder(); - foreach (var p in Parameters) - { - if (querystring.Length > 1) - querystring.Append("&"); - querystring.AppendFormat("{0}={1}", p.Name.UrlEncode(), p.Value.UrlEncode()); - } - - return querystring.ToString(); - } - - private void PreparePostBody(HttpWebRequest webRequest) - { - if (HasFiles || AlwaysMultipartFormData) - { - webRequest.ContentType = GetMultipartFormContentType(); - } - else if(HasParameters) - { - webRequest.ContentType = "application/x-www-form-urlencoded"; - RequestBody = EncodeParameters(); - } - else if(HasBody) - { - webRequest.ContentType = RequestContentType; - } - } - - private static void WriteStringTo(Stream stream, string toWrite) - { - var bytes = _defaultEncoding.GetBytes(toWrite); - stream.Write(bytes, 0, bytes.Length); - } - - private void WriteMultipartFormData(Stream requestStream) - { - foreach (var param in Parameters) - { - WriteStringTo(requestStream, GetMultipartFormData(param)); - } - - foreach (var file in Files) - { - // Add just the first part of this param, since we will write the file data directly to the Stream - WriteStringTo(requestStream, GetMultipartFileHeader(file)); - - // Write the file data directly to the Stream, rather than serializing it to a string. - file.Writer(requestStream); - WriteStringTo(requestStream, _lineBreak); - } - - WriteStringTo(requestStream, GetMultipartFooter()); - } - - private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse) - { - using (webResponse) - { -#if FRAMEWORK - response.ContentEncoding = webResponse.ContentEncoding; - response.Server = webResponse.Server; -#endif - response.ContentType = webResponse.ContentType; - response.ContentLength = webResponse.ContentLength; - Stream webResponseStream = webResponse.GetResponseStream(); -#if WINDOWS_PHONE - if (String.Equals(webResponse.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) - { - var gzStream = new GZipStream(webResponseStream); - ProcessResponseStream(gzStream, response); - } - else - { - ProcessResponseStream(webResponseStream, response); - } -#else - ProcessResponseStream(webResponseStream, response); -#endif - response.StatusCode = webResponse.StatusCode; - response.StatusDescription = webResponse.StatusDescription; - response.ResponseUri = webResponse.ResponseUri; - response.ResponseStatus = ResponseStatus.Completed; - -#if !PocketPC - if (webResponse.Cookies != null) - { - foreach (Cookie cookie in webResponse.Cookies) - { - response.Cookies.Add(new HttpCookie { - Comment = cookie.Comment, - CommentUri = cookie.CommentUri, - Discard = cookie.Discard, - Domain = cookie.Domain, - Expired = cookie.Expired, - Expires = cookie.Expires, - HttpOnly = cookie.HttpOnly, - Name = cookie.Name, - Path = cookie.Path, - Port = cookie.Port, - Secure = cookie.Secure, - TimeStamp = cookie.TimeStamp, - Value = cookie.Value, - Version = cookie.Version - }); - } - } -#endif - foreach (var headerName in webResponse.Headers.AllKeys) - { - var headerValue = webResponse.Headers[headerName]; - response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue }); - } - - webResponse.Close(); - } - } - - private void ProcessResponseStream(Stream webResponseStream, HttpResponse response) - { - if (ResponseWriter == null) - { - response.RawBytes = webResponseStream.ReadAsBytes(); - } - else - { - ResponseWriter(webResponseStream); - } - } - -#if FRAMEWORK - private void AddRange(HttpWebRequest r, string range) - { - System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(range, "=(\\d+)-(\\d+)$"); - if (!m.Success) - { - return; - } - - int from = Convert.ToInt32(m.Groups[1].Value); - int to = Convert.ToInt32(m.Groups[2].Value); - r.AddRange(from, to); - } -#endif - } +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using RestSharp.Extensions; + +#if WINDOWS_PHONE +using RestSharp.Compression.ZLib; +#endif + +namespace RestSharp +{ + /// + /// HttpWebRequest wrapper + /// + public partial class Http : IHttp, IHttpFactory + { + private const string _lineBreak = "\r\n"; + private static readonly Encoding _defaultEncoding = Encoding.UTF8; + + /// + /// Creates an IHttp + /// + /// + public IHttp Create() + { + return new Http(); + } + + /// + /// True if this HTTP request has any HTTP parameters + /// + protected bool HasParameters + { + get + { + return Parameters.Any(); + } + } + + /// + /// True if this HTTP request has any HTTP cookies + /// + protected bool HasCookies + { + get + { + return Cookies.Any(); + } + } + + /// + /// True if a request body has been specified + /// + protected bool HasBody + { + get + { + return RequestBodyBytes != null || !string.IsNullOrEmpty(RequestBody); + } + } + + /// + /// True if files have been set to be uploaded + /// + protected bool HasFiles + { + get + { + return Files.Any(); + } + } + + /// + /// Always send a multipart/form-data request - even when no Files are present. + /// + public bool AlwaysMultipartFormData { get; set; } + + /// + /// UserAgent to be sent with request + /// + public string UserAgent { get; set; } + /// + /// Timeout in milliseconds to be used for the request + /// + public int Timeout { get; set; } + /// + /// The number of milliseconds before the writing or reading times out. + /// + public int ReadWriteTimeout { get; set; } + /// + /// System.Net.ICredentials to be sent with request + /// + public ICredentials Credentials { get; set; } + /// + /// The System.Net.CookieContainer to be used for the request + /// +#if !PocketPC + public CookieContainer CookieContainer { get; set; } +#endif + /// + /// The method to use to write the response instead of reading into RawBytes + /// + public Action ResponseWriter { get; set; } + /// + /// Collection of files to be sent with request + /// + public IList Files { get; private set; } +#if !SILVERLIGHT + /// + /// Whether or not HTTP 3xx response redirects should be automatically followed + /// + public bool FollowRedirects { get; set; } +#endif +#if FRAMEWORK + /// + /// X509CertificateCollection to be sent with request + /// + public X509CertificateCollection ClientCertificates { get; set; } +#endif +#if FRAMEWORK || PocketPC + /// + /// Maximum number of automatic redirects to follow if FollowRedirects is true + /// + public int? MaxRedirects { get; set; } +#endif + /// + /// Determine whether or not the "default credentials" (e.g. the user account under which the current process is running) + /// will be sent along to the server. + /// +#if !PocketPC + public bool UseDefaultCredentials { get; set; } +#endif + /// + /// HTTP headers to be sent with request + /// + public IList Headers { get; private set; } + /// + /// HTTP parameters (QueryString or Form values) to be sent with request + /// + public IList Parameters { get; private set; } + /// + /// HTTP cookies to be sent with request + /// + public IList Cookies { get; private set; } + /// + /// Request body to be sent with request + /// + public string RequestBody { get; set; } + /// + /// Content type of the request body. + /// + public string RequestContentType { get; set; } + /// + /// An alternative to RequestBody, for when the caller already has the byte array. + /// + public byte[] RequestBodyBytes { get; set; } + /// + /// URL to call for this request + /// + public Uri Url { get; set; } + /// + /// Flag to send authorisation header with the HttpWebRequest + /// + public bool PreAuthenticate { get; set; } + +#if FRAMEWORK || PocketPC + /// + /// Proxy info to be sent with request + /// + public IWebProxy Proxy { get; set; } +#endif + + /// + /// Default constructor + /// + public Http() + { + Headers = new List(); + Files = new List(); + Parameters = new List(); + Cookies = new List(); + + _restrictedHeaderActions = new Dictionary>(StringComparer.OrdinalIgnoreCase); + + AddSharedHeaderActions(); + AddSyncHeaderActions(); + } + + partial void AddSyncHeaderActions(); + partial void AddAsyncHeaderActions(); + private void AddSharedHeaderActions() + { + _restrictedHeaderActions.Add("Accept", (r, v) => r.Accept = v); + _restrictedHeaderActions.Add("Content-Type", (r, v) => r.ContentType = v); +#if NET4 + _restrictedHeaderActions.Add("Date", (r, v) => + { + DateTime parsed; + if (DateTime.TryParse(v, out parsed)) + { + r.Date = parsed; + } + }); + _restrictedHeaderActions.Add("Host", (r, v) => r.Host = v); +#else + _restrictedHeaderActions.Add("Date", (r, v) => { /* Set by system */ }); + _restrictedHeaderActions.Add("Host", (r, v) => { /* Set by system */ }); +#endif +#if FRAMEWORK + _restrictedHeaderActions.Add("Range", (r, v) => { AddRange(r, v); }); +#endif + } + + private const string FormBoundary = "-----------------------------28947758029299"; + private static string GetMultipartFormContentType() + { + return string.Format("multipart/form-data; boundary={0}", FormBoundary); + } + + private static string GetMultipartFileHeader(HttpFile file) + { + return string.Format("--{0}{4}Content-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"{4}Content-Type: {3}{4}{4}", + FormBoundary, file.Name, file.FileName, file.ContentType ?? "application/octet-stream", _lineBreak); + } + + private static string GetMultipartFormData(HttpParameter param) + { + return string.Format("--{0}{3}Content-Disposition: form-data; name=\"{1}\"{3}{3}{2}{3}", + FormBoundary, param.Name, param.Value, _lineBreak); + } + + private static string GetMultipartFooter() + { + return string.Format("--{0}--{1}", FormBoundary, _lineBreak); + } + + private readonly IDictionary> _restrictedHeaderActions; + + // handle restricted headers the .NET way - thanks @dimebrain! + // http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx + private void AppendHeaders(HttpWebRequest webRequest) + { + foreach (var header in Headers) + { + if (_restrictedHeaderActions.ContainsKey(header.Name)) + { + _restrictedHeaderActions[header.Name].Invoke(webRequest, header.Value); + } + else + { +#if FRAMEWORK + webRequest.Headers.Add(header.Name, header.Value); +#else + webRequest.Headers[header.Name] = header.Value; +#endif + } + } + } + + private void AppendCookies(HttpWebRequest webRequest) + { +#if !PocketPC + webRequest.CookieContainer = this.CookieContainer ?? new CookieContainer(); +#endif + foreach (var httpCookie in Cookies) + { +#if !PocketPC +#if FRAMEWORK + var cookie = new Cookie + { + Name = httpCookie.Name, + Value = httpCookie.Value, + Domain = webRequest.RequestUri.Host + }; + webRequest.CookieContainer.Add(cookie); +#else + var cookie = new Cookie + { + Name = httpCookie.Name, + Value = httpCookie.Value + }; + var uri = webRequest.RequestUri; + webRequest.CookieContainer.Add(new Uri(string.Format("{0}://{1}", uri.Scheme, uri.Host)), cookie); +#endif +#endif + } + } + + private string EncodeParameters() + { + var querystring = new StringBuilder(); + foreach (var p in Parameters) + { + if (querystring.Length > 1) + querystring.Append("&"); + querystring.AppendFormat("{0}={1}", p.Name.UrlEncode(), p.Value.UrlEncode()); + } + + return querystring.ToString(); + } + + private void PreparePostBody(HttpWebRequest webRequest) + { + if (HasFiles || AlwaysMultipartFormData) + { + webRequest.ContentType = GetMultipartFormContentType(); + } + else if (HasParameters) + { + webRequest.ContentType = "application/x-www-form-urlencoded"; + RequestBody = EncodeParameters(); + } + else if (HasBody) + { + webRequest.ContentType = RequestContentType; + } + } + + private static void WriteStringTo(Stream stream, string toWrite) + { + var bytes = _defaultEncoding.GetBytes(toWrite); + stream.Write(bytes, 0, bytes.Length); + } + + private void WriteMultipartFormData(Stream requestStream) + { + foreach (var param in Parameters) + { + WriteStringTo(requestStream, GetMultipartFormData(param)); + } + + foreach (var file in Files) + { + // Add just the first part of this param, since we will write the file data directly to the Stream + WriteStringTo(requestStream, GetMultipartFileHeader(file)); + + // Write the file data directly to the Stream, rather than serializing it to a string. + file.Writer(requestStream); + WriteStringTo(requestStream, _lineBreak); + } + + WriteStringTo(requestStream, GetMultipartFooter()); + } + + private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse) + { + using (webResponse) + { +#if FRAMEWORK + response.ContentEncoding = webResponse.ContentEncoding; + response.Server = webResponse.Server; +#endif + response.ContentType = webResponse.ContentType; + response.ContentLength = webResponse.ContentLength; + Stream webResponseStream = webResponse.GetResponseStream(); +#if WINDOWS_PHONE + if (String.Equals(webResponse.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) + { + var gzStream = new GZipStream(webResponseStream); + ProcessResponseStream(gzStream, response); + } + else + { + ProcessResponseStream(webResponseStream, response); + } +#else + ProcessResponseStream(webResponseStream, response); +#endif + response.StatusCode = webResponse.StatusCode; + response.StatusDescription = webResponse.StatusDescription; + response.ResponseUri = webResponse.ResponseUri; + response.ResponseStatus = ResponseStatus.Completed; + +#if !PocketPC + if (webResponse.Cookies != null) + { + foreach (Cookie cookie in webResponse.Cookies) + { + response.Cookies.Add(new HttpCookie + { + Comment = cookie.Comment, + CommentUri = cookie.CommentUri, + Discard = cookie.Discard, + Domain = cookie.Domain, + Expired = cookie.Expired, + Expires = cookie.Expires, + HttpOnly = cookie.HttpOnly, + Name = cookie.Name, + Path = cookie.Path, + Port = cookie.Port, + Secure = cookie.Secure, + TimeStamp = cookie.TimeStamp, + Value = cookie.Value, + Version = cookie.Version + }); + } + } +#endif + foreach (var headerName in webResponse.Headers.AllKeys) + { + var headerValue = webResponse.Headers[headerName]; + response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue }); + } + + webResponse.Close(); + } + } + + private void ProcessResponseStream(Stream webResponseStream, HttpResponse response) + { + if (ResponseWriter == null) + { + response.RawBytes = webResponseStream.ReadAsBytes(); + } + else + { + ResponseWriter(webResponseStream); + } + } + +#if FRAMEWORK + private void AddRange(HttpWebRequest r, string range) + { + System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(range, "=(\\d+)-(\\d+)$"); + if (!m.Success) + { + return; + } + + int from = Convert.ToInt32(m.Groups[1].Value); + int to = Convert.ToInt32(m.Groups[2].Value); + r.AddRange(from, to); + } +#endif + } } \ No newline at end of file diff --git a/RestSharp/IHttp.cs b/RestSharp/IHttp.cs index 8857f2bc1..7e0e336d3 100644 --- a/RestSharp/IHttp.cs +++ b/RestSharp/IHttp.cs @@ -1,92 +1,92 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -using System; -using System.Collections.Generic; -using System.IO; -using System.Net; -using System.Security.Cryptography.X509Certificates; - -namespace RestSharp -{ - public interface IHttp - { - Action ResponseWriter { get; set; } -#if !PocketPC - CookieContainer CookieContainer { get; set; } -#endif - ICredentials Credentials { get; set; } - - /// - /// Always send a multipart/form-data request - even when no Files are present. - /// - bool AlwaysMultipartFormData { get; set; } - - string UserAgent { get; set; } - int Timeout { get; set; } - int ReadWriteTimeout { get; set; } -#if !SILVERLIGHT - bool FollowRedirects { get; set; } -#endif -#if FRAMEWORK - X509CertificateCollection ClientCertificates { get; set; } - int? MaxRedirects { get; set; } -#endif -#if !PocketPC - bool UseDefaultCredentials { get; set; } -#endif - IList Headers { get; } - IList Parameters { get; } - IList Files { get; } - IList Cookies { get; } - string RequestBody { get; set; } - string RequestContentType { get; set; } - bool PreAuthenticate { get; set; } - - /// - /// An alternative to RequestBody, for when the caller already has the byte array. - /// - byte[] RequestBodyBytes { get; set; } - - Uri Url { get; set; } - - HttpWebRequest DeleteAsync(Action action); - HttpWebRequest GetAsync(Action action); - HttpWebRequest HeadAsync(Action action); - HttpWebRequest OptionsAsync(Action action); - HttpWebRequest PostAsync(Action action); - HttpWebRequest PutAsync(Action action); +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Security.Cryptography.X509Certificates; + +namespace RestSharp +{ + public interface IHttp + { + Action ResponseWriter { get; set; } +#if !PocketPC + CookieContainer CookieContainer { get; set; } +#endif + ICredentials Credentials { get; set; } + + /// + /// Always send a multipart/form-data request - even when no Files are present. + /// + bool AlwaysMultipartFormData { get; set; } + + string UserAgent { get; set; } + int Timeout { get; set; } + int ReadWriteTimeout { get; set; } +#if !SILVERLIGHT + bool FollowRedirects { get; set; } +#endif +#if FRAMEWORK + X509CertificateCollection ClientCertificates { get; set; } + int? MaxRedirects { get; set; } +#endif +#if !PocketPC + bool UseDefaultCredentials { get; set; } +#endif + IList Headers { get; } + IList Parameters { get; } + IList Files { get; } + IList Cookies { get; } + string RequestBody { get; set; } + string RequestContentType { get; set; } + bool PreAuthenticate { get; set; } + + /// + /// An alternative to RequestBody, for when the caller already has the byte array. + /// + byte[] RequestBodyBytes { get; set; } + + Uri Url { get; set; } + + HttpWebRequest DeleteAsync(Action action); + HttpWebRequest GetAsync(Action action); + HttpWebRequest HeadAsync(Action action); + HttpWebRequest OptionsAsync(Action action); + HttpWebRequest PostAsync(Action action); + HttpWebRequest PutAsync(Action action); HttpWebRequest PatchAsync(Action action); - HttpWebRequest MergeAsync(Action action); - HttpWebRequest AsPostAsync(Action action, string httpMethod); - HttpWebRequest AsGetAsync(Action action, string httpMethod); - -#if FRAMEWORK || PocketPC - HttpResponse Delete(); - HttpResponse Get(); - HttpResponse Head(); - HttpResponse Options(); - HttpResponse Post(); - HttpResponse Put(); + HttpWebRequest MergeAsync(Action action); + HttpWebRequest AsPostAsync(Action action, string httpMethod); + HttpWebRequest AsGetAsync(Action action, string httpMethod); + +#if FRAMEWORK || PocketPC + HttpResponse Delete(); + HttpResponse Get(); + HttpResponse Head(); + HttpResponse Options(); + HttpResponse Post(); + HttpResponse Put(); HttpResponse Patch(); - HttpResponse Merge(); - HttpResponse AsPost(string httpMethod); - HttpResponse AsGet(string httpMethod); - - IWebProxy Proxy { get; set; } -#endif - } + HttpResponse Merge(); + HttpResponse AsPost(string httpMethod); + HttpResponse AsGet(string httpMethod); + + IWebProxy Proxy { get; set; } +#endif + } } \ No newline at end of file diff --git a/RestSharp/RestClient.Async.cs b/RestSharp/RestClient.Async.cs index af5cdbd2b..20627f497 100644 --- a/RestSharp/RestClient.Async.cs +++ b/RestSharp/RestClient.Async.cs @@ -1,397 +1,397 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; -#if NET4 || MONODROID || MONOTOUCH || WP8 -using System.Threading.Tasks; -#endif -using System.Text; -using System.Net; - -using RestSharp.Extensions; - -namespace RestSharp -{ - public partial class RestClient - { - /// - /// Executes the request and callback asynchronously, authenticating if needed - /// - /// Request to be executed - /// Callback function to be executed upon completion providing access to the async handle. - public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action callback) - { -#if PocketPC - string method = request.Method.ToString(); -#else - string method = Enum.GetName(typeof (Method), request.Method); -#endif - switch (request.Method) - { - case Method.MERGE: - case Method.PATCH: - case Method.POST: - case Method.PUT: - return ExecuteAsync(request, callback, method, DoAsPostAsync); - default: - return ExecuteAsync(request, callback, method, DoAsGetAsync); - } - } - - /// - /// Executes a GET-style request and callback asynchronously, authenticating if needed - /// - /// Request to be executed - /// Callback function to be executed upon completion providing access to the async handle. - /// The HTTP method to execute - public virtual RestRequestAsyncHandle ExecuteAsyncGet(IRestRequest request, Action callback, string httpMethod) - { - return ExecuteAsync(request, callback, httpMethod, DoAsGetAsync); - } - - /// - /// Executes a POST-style request and callback asynchronously, authenticating if needed - /// - /// Request to be executed - /// Callback function to be executed upon completion providing access to the async handle. - /// The HTTP method to execute - public virtual RestRequestAsyncHandle ExecuteAsyncPost(IRestRequest request, Action callback, string httpMethod) - { - request.Method = Method.POST; // Required by RestClient.BuildUri... - return ExecuteAsync(request, callback, httpMethod, DoAsPostAsync); - } - - private RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action callback, string httpMethod, Func, string, HttpWebRequest> getWebRequest) - { - var http = HttpFactory.Create(); - AuthenticateIfNeeded(this, request); - - ConfigureHttp(request, http); - - var asyncHandle = new RestRequestAsyncHandle(); - - Action response_cb = r => ProcessResponse(request, r, asyncHandle, callback); - -#if !PocketPC - if (UseSynchronizationContext && SynchronizationContext.Current != null) - { - var ctx = SynchronizationContext.Current; - var cb = response_cb; - - response_cb = resp => ctx.Post(s => cb(resp), null); - } -#endif - asyncHandle.WebRequest = getWebRequest(http, response_cb, httpMethod); - return asyncHandle; - } - - private static HttpWebRequest DoAsGetAsync(IHttp http, Action response_cb, string method) - { - return http.AsGetAsync(response_cb, method); - } - - private static HttpWebRequest DoAsPostAsync(IHttp http, Action response_cb, string method) - { - return http.AsPostAsync(response_cb, method); - } - - private void ProcessResponse(IRestRequest request, HttpResponse httpResponse, RestRequestAsyncHandle asyncHandle, Action callback) - { - var restResponse = ConvertToRestResponse(request, httpResponse); - callback(restResponse, asyncHandle); - } - - /// - /// Executes the request and callback asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// Callback function to be executed upon completion - public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action, RestRequestAsyncHandle> callback) - { - return ExecuteAsync(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle)); - } - - /// - /// Executes a GET-style request and callback asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// Callback function to be executed upon completion - /// The HTTP method to execute - public virtual RestRequestAsyncHandle ExecuteAsyncGet(IRestRequest request, Action, RestRequestAsyncHandle> callback, string httpMethod) - { - return ExecuteAsyncGet(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle), httpMethod); - } - - /// - /// Executes a POST-style request and callback asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// Callback function to be executed upon completion - /// The HTTP method to execute - public virtual RestRequestAsyncHandle ExecuteAsyncPost(IRestRequest request, Action, RestRequestAsyncHandle> callback, string httpMethod) - { - return ExecuteAsyncPost(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle), httpMethod); - } - - private void DeserializeResponse(IRestRequest request, Action, RestRequestAsyncHandle> callback, IRestResponse response, RestRequestAsyncHandle asyncHandle) - { - IRestResponse restResponse; - - try - { - restResponse = Deserialize(request, response); - } - catch (Exception ex) - { - restResponse = new RestResponse { Request = request, ResponseStatus = ResponseStatus.Error, ErrorMessage = ex.Message, ErrorException = ex}; - } - - callback(restResponse, asyncHandle); - } - -#if NET4 || MONODROID || MONOTOUCH || WP8 - /// - /// Executes a GET-style request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - public virtual Task> ExecuteGetTaskAsync(IRestRequest request) - { - return ExecuteGetTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes a GET-style request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// The cancellation token - public virtual Task> ExecuteGetTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - request.Method = Method.GET; - return ExecuteTaskAsync(request, token); - } - - /// - /// Executes a POST-style request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - public virtual Task> ExecutePostTaskAsync(IRestRequest request) - { - return ExecutePostTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes a POST-style request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// The cancellation token - public virtual Task> ExecutePostTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - request.Method = Method.POST; - return ExecuteTaskAsync(request, token); - } - - /// - /// Executes the request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - public virtual Task> ExecuteTaskAsync(IRestRequest request) - { - return ExecuteTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes the request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// The cancellation token - public virtual Task> ExecuteTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - var taskCompletionSource = new TaskCompletionSource>(); - - try - { - var async = ExecuteAsync(request, (response, _) => - { - if (token.IsCancellationRequested) - { - taskCompletionSource.TrySetCanceled(); - } - else if (response.ErrorException != null) - { - taskCompletionSource.TrySetException(response.ErrorException); - } - else if (response.ResponseStatus != ResponseStatus.Completed) - { - taskCompletionSource.TrySetException(response.ResponseStatus.ToWebException()); - } - else - { - taskCompletionSource.TrySetResult(response); - } - }); - - token.Register(() => - { - async.Abort(); - taskCompletionSource.TrySetCanceled(); - }); - } - catch (Exception ex) - { - taskCompletionSource.TrySetException(ex); - } - - return taskCompletionSource.Task; - } - - /// - /// Executes the request asynchronously, authenticating if needed - /// - /// Request to be executed - public virtual Task ExecuteTaskAsync(IRestRequest request) - { - return ExecuteTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes a GET-style asynchronously, authenticating if needed - /// - /// Request to be executed - public virtual Task ExecuteGetTaskAsync(IRestRequest request) - { - return this.ExecuteGetTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes a GET-style asynchronously, authenticating if needed - /// - /// Request to be executed - /// The cancellation token - public virtual Task ExecuteGetTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - request.Method = Method.GET; - return ExecuteTaskAsync(request, token); - } - - /// - /// Executes a POST-style asynchronously, authenticating if needed - /// - /// Request to be executed - public virtual Task ExecutePostTaskAsync(IRestRequest request) - { - return this.ExecutePostTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes a POST-style asynchronously, authenticating if needed - /// - /// Request to be executed - /// The cancellation token - public virtual Task ExecutePostTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - request.Method = Method.POST; - return ExecuteTaskAsync(request, token); - } - - /// - /// Executes the request asynchronously, authenticating if needed - /// - /// Request to be executed - /// The cancellation token - public virtual Task ExecuteTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - var taskCompletionSource = new TaskCompletionSource(); - - try - { - var async = this.ExecuteAsync(request, (response, _) => - { - if (token.IsCancellationRequested) - { - taskCompletionSource.TrySetCanceled(); - } - else if (response.ErrorException != null) - { - taskCompletionSource.TrySetException(response.ErrorException); - } - else if (response.ResponseStatus != ResponseStatus.Completed) - { - taskCompletionSource.TrySetException(response.ResponseStatus.ToWebException()); - } - else - { - taskCompletionSource.TrySetResult(response); - } - }); - - token.Register(() => - { - async.Abort(); - taskCompletionSource.TrySetCanceled(); - }); - } - catch (Exception ex) - { - taskCompletionSource.TrySetException(ex); - } - - return taskCompletionSource.Task; - } -#endif - } +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; +#if NET4 || MONODROID || MONOTOUCH || WP8 +using System.Threading.Tasks; +#endif +using System.Text; +using System.Net; + +using RestSharp.Extensions; + +namespace RestSharp +{ + public partial class RestClient + { + /// + /// Executes the request and callback asynchronously, authenticating if needed + /// + /// Request to be executed + /// Callback function to be executed upon completion providing access to the async handle. + public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action callback) + { +#if PocketPC + string method = request.Method.ToString(); +#else + string method = Enum.GetName(typeof(Method), request.Method); +#endif + switch (request.Method) + { + case Method.MERGE: + case Method.PATCH: + case Method.POST: + case Method.PUT: + return ExecuteAsync(request, callback, method, DoAsPostAsync); + default: + return ExecuteAsync(request, callback, method, DoAsGetAsync); + } + } + + /// + /// Executes a GET-style request and callback asynchronously, authenticating if needed + /// + /// Request to be executed + /// Callback function to be executed upon completion providing access to the async handle. + /// The HTTP method to execute + public virtual RestRequestAsyncHandle ExecuteAsyncGet(IRestRequest request, Action callback, string httpMethod) + { + return ExecuteAsync(request, callback, httpMethod, DoAsGetAsync); + } + + /// + /// Executes a POST-style request and callback asynchronously, authenticating if needed + /// + /// Request to be executed + /// Callback function to be executed upon completion providing access to the async handle. + /// The HTTP method to execute + public virtual RestRequestAsyncHandle ExecuteAsyncPost(IRestRequest request, Action callback, string httpMethod) + { + request.Method = Method.POST; // Required by RestClient.BuildUri... + return ExecuteAsync(request, callback, httpMethod, DoAsPostAsync); + } + + private RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action callback, string httpMethod, Func, string, HttpWebRequest> getWebRequest) + { + var http = HttpFactory.Create(); + AuthenticateIfNeeded(this, request); + + ConfigureHttp(request, http); + + var asyncHandle = new RestRequestAsyncHandle(); + + Action response_cb = r => ProcessResponse(request, r, asyncHandle, callback); + +#if !PocketPC + if (UseSynchronizationContext && SynchronizationContext.Current != null) + { + var ctx = SynchronizationContext.Current; + var cb = response_cb; + + response_cb = resp => ctx.Post(s => cb(resp), null); + } +#endif + asyncHandle.WebRequest = getWebRequest(http, response_cb, httpMethod); + return asyncHandle; + } + + private static HttpWebRequest DoAsGetAsync(IHttp http, Action response_cb, string method) + { + return http.AsGetAsync(response_cb, method); + } + + private static HttpWebRequest DoAsPostAsync(IHttp http, Action response_cb, string method) + { + return http.AsPostAsync(response_cb, method); + } + + private void ProcessResponse(IRestRequest request, HttpResponse httpResponse, RestRequestAsyncHandle asyncHandle, Action callback) + { + var restResponse = ConvertToRestResponse(request, httpResponse); + callback(restResponse, asyncHandle); + } + + /// + /// Executes the request and callback asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// Callback function to be executed upon completion + public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action, RestRequestAsyncHandle> callback) + { + return ExecuteAsync(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle)); + } + + /// + /// Executes a GET-style request and callback asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// Callback function to be executed upon completion + /// The HTTP method to execute + public virtual RestRequestAsyncHandle ExecuteAsyncGet(IRestRequest request, Action, RestRequestAsyncHandle> callback, string httpMethod) + { + return ExecuteAsyncGet(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle), httpMethod); + } + + /// + /// Executes a POST-style request and callback asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// Callback function to be executed upon completion + /// The HTTP method to execute + public virtual RestRequestAsyncHandle ExecuteAsyncPost(IRestRequest request, Action, RestRequestAsyncHandle> callback, string httpMethod) + { + return ExecuteAsyncPost(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle), httpMethod); + } + + private void DeserializeResponse(IRestRequest request, Action, RestRequestAsyncHandle> callback, IRestResponse response, RestRequestAsyncHandle asyncHandle) + { + IRestResponse restResponse; + + try + { + restResponse = Deserialize(request, response); + } + catch (Exception ex) + { + restResponse = new RestResponse { Request = request, ResponseStatus = ResponseStatus.Error, ErrorMessage = ex.Message, ErrorException = ex }; + } + + callback(restResponse, asyncHandle); + } + +#if NET4 || MONODROID || MONOTOUCH || WP8 + /// + /// Executes a GET-style request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + public virtual Task> ExecuteGetTaskAsync(IRestRequest request) + { + return ExecuteGetTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes a GET-style request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// The cancellation token + public virtual Task> ExecuteGetTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + request.Method = Method.GET; + return ExecuteTaskAsync(request, token); + } + + /// + /// Executes a POST-style request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + public virtual Task> ExecutePostTaskAsync(IRestRequest request) + { + return ExecutePostTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes a POST-style request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// The cancellation token + public virtual Task> ExecutePostTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + request.Method = Method.POST; + return ExecuteTaskAsync(request, token); + } + + /// + /// Executes the request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + public virtual Task> ExecuteTaskAsync(IRestRequest request) + { + return ExecuteTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes the request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// The cancellation token + public virtual Task> ExecuteTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + var taskCompletionSource = new TaskCompletionSource>(); + + try + { + var async = ExecuteAsync(request, (response, _) => + { + if (token.IsCancellationRequested) + { + taskCompletionSource.TrySetCanceled(); + } + else if (response.ErrorException != null) + { + taskCompletionSource.TrySetException(response.ErrorException); + } + else if (response.ResponseStatus != ResponseStatus.Completed) + { + taskCompletionSource.TrySetException(response.ResponseStatus.ToWebException()); + } + else + { + taskCompletionSource.TrySetResult(response); + } + }); + + token.Register(() => + { + async.Abort(); + taskCompletionSource.TrySetCanceled(); + }); + } + catch (Exception ex) + { + taskCompletionSource.TrySetException(ex); + } + + return taskCompletionSource.Task; + } + + /// + /// Executes the request asynchronously, authenticating if needed + /// + /// Request to be executed + public virtual Task ExecuteTaskAsync(IRestRequest request) + { + return ExecuteTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes a GET-style asynchronously, authenticating if needed + /// + /// Request to be executed + public virtual Task ExecuteGetTaskAsync(IRestRequest request) + { + return this.ExecuteGetTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes a GET-style asynchronously, authenticating if needed + /// + /// Request to be executed + /// The cancellation token + public virtual Task ExecuteGetTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + request.Method = Method.GET; + return ExecuteTaskAsync(request, token); + } + + /// + /// Executes a POST-style asynchronously, authenticating if needed + /// + /// Request to be executed + public virtual Task ExecutePostTaskAsync(IRestRequest request) + { + return this.ExecutePostTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes a POST-style asynchronously, authenticating if needed + /// + /// Request to be executed + /// The cancellation token + public virtual Task ExecutePostTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + request.Method = Method.POST; + return ExecuteTaskAsync(request, token); + } + + /// + /// Executes the request asynchronously, authenticating if needed + /// + /// Request to be executed + /// The cancellation token + public virtual Task ExecuteTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + var taskCompletionSource = new TaskCompletionSource(); + + try + { + var async = this.ExecuteAsync(request, (response, _) => + { + if (token.IsCancellationRequested) + { + taskCompletionSource.TrySetCanceled(); + } + else if (response.ErrorException != null) + { + taskCompletionSource.TrySetException(response.ErrorException); + } + else if (response.ResponseStatus != ResponseStatus.Completed) + { + taskCompletionSource.TrySetException(response.ResponseStatus.ToWebException()); + } + else + { + taskCompletionSource.TrySetResult(response); + } + }); + + token.Register(() => + { + async.Abort(); + taskCompletionSource.TrySetCanceled(); + }); + } + catch (Exception ex) + { + taskCompletionSource.TrySetException(ex); + } + + return taskCompletionSource.Task; + } +#endif + } } \ No newline at end of file diff --git a/RestSharp/RestClient.Sync.cs b/RestSharp/RestClient.Sync.cs index 1480c7a14..2946d33d6 100644 --- a/RestSharp/RestClient.Sync.cs +++ b/RestSharp/RestClient.Sync.cs @@ -1,119 +1,119 @@ -#if FRAMEWORK || PocketPC -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using RestSharp.Deserializers; - -namespace RestSharp -{ - public partial class RestClient - { - - - /// - /// Executes the specified request and downloads the response data - /// - /// Request to execute - /// Response data - public byte[] DownloadData(IRestRequest request) - { - var response = Execute(request); - return response.RawBytes; - } - - /// - /// Executes the request and returns a response, authenticating if needed - /// - /// Request to be executed - /// RestResponse - public virtual IRestResponse Execute(IRestRequest request) - { -#if PocketPC - var method = request.Method.ToString(); -#else - var method = Enum.GetName(typeof(Method), request.Method); -#endif - switch (request.Method) - { - case Method.POST: +#if FRAMEWORK || PocketPC +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using RestSharp.Deserializers; + +namespace RestSharp +{ + public partial class RestClient + { + + + /// + /// Executes the specified request and downloads the response data + /// + /// Request to execute + /// Response data + public byte[] DownloadData(IRestRequest request) + { + var response = Execute(request); + return response.RawBytes; + } + + /// + /// Executes the request and returns a response, authenticating if needed + /// + /// Request to be executed + /// RestResponse + public virtual IRestResponse Execute(IRestRequest request) + { +#if PocketPC + var method = request.Method.ToString(); +#else + var method = Enum.GetName(typeof(Method), request.Method); +#endif + switch (request.Method) + { + case Method.POST: case Method.PUT: - case Method.PATCH: - case Method.MERGE: - return Execute(request, method, DoExecuteAsPost); - default: - return Execute(request, method, DoExecuteAsGet); - } - } - - public IRestResponse ExecuteAsGet(IRestRequest request, string httpMethod) - { - return Execute(request, httpMethod, DoExecuteAsGet); - } - - public IRestResponse ExecuteAsPost(IRestRequest request, string httpMethod) - { - request.Method = Method.POST; // Required by RestClient.BuildUri... - return Execute(request, httpMethod, DoExecuteAsPost); - } - - private IRestResponse Execute(IRestRequest request, string httpMethod, Func getResponse) - { - AuthenticateIfNeeded(this, request); - - IRestResponse response = new RestResponse(); - try - { - var http = HttpFactory.Create(); - - ConfigureHttp(request, http); - - response = ConvertToRestResponse(request, getResponse(http, httpMethod)); - response.Request = request; - response.Request.IncreaseNumAttempts(); - - } - catch (Exception ex) - { - response.ResponseStatus = ResponseStatus.Error; - response.ErrorMessage = ex.Message; - response.ErrorException = ex; - } - - return response; - } - - private static HttpResponse DoExecuteAsGet(IHttp http, string method) - { - return http.AsGet(method); - } - - private static HttpResponse DoExecuteAsPost(IHttp http, string method) - { - return http.AsPost(method); - } - - /// - /// Executes the specified request and deserializes the response content using the appropriate content handler - /// - /// Target deserialization type - /// Request to execute - /// RestResponse[[T]] with deserialized data in Data property - public virtual IRestResponse Execute(IRestRequest request) where T : new() - { - return Deserialize(request, Execute(request)); - } - - public IRestResponse ExecuteAsGet(IRestRequest request, string httpMethod) where T : new() - { - return Deserialize(request, ExecuteAsGet(request, httpMethod)); - } - - public IRestResponse ExecuteAsPost(IRestRequest request, string httpMethod) where T : new() - { - return Deserialize(request, ExecuteAsPost(request, httpMethod)); - } - } -} + case Method.PATCH: + case Method.MERGE: + return Execute(request, method, DoExecuteAsPost); + default: + return Execute(request, method, DoExecuteAsGet); + } + } + + public IRestResponse ExecuteAsGet(IRestRequest request, string httpMethod) + { + return Execute(request, httpMethod, DoExecuteAsGet); + } + + public IRestResponse ExecuteAsPost(IRestRequest request, string httpMethod) + { + request.Method = Method.POST; // Required by RestClient.BuildUri... + return Execute(request, httpMethod, DoExecuteAsPost); + } + + private IRestResponse Execute(IRestRequest request, string httpMethod, Func getResponse) + { + AuthenticateIfNeeded(this, request); + + IRestResponse response = new RestResponse(); + try + { + var http = HttpFactory.Create(); + + ConfigureHttp(request, http); + + response = ConvertToRestResponse(request, getResponse(http, httpMethod)); + response.Request = request; + response.Request.IncreaseNumAttempts(); + + } + catch (Exception ex) + { + response.ResponseStatus = ResponseStatus.Error; + response.ErrorMessage = ex.Message; + response.ErrorException = ex; + } + + return response; + } + + private static HttpResponse DoExecuteAsGet(IHttp http, string method) + { + return http.AsGet(method); + } + + private static HttpResponse DoExecuteAsPost(IHttp http, string method) + { + return http.AsPost(method); + } + + /// + /// Executes the specified request and deserializes the response content using the appropriate content handler + /// + /// Target deserialization type + /// Request to execute + /// RestResponse[[T]] with deserialized data in Data property + public virtual IRestResponse Execute(IRestRequest request) where T : new() + { + return Deserialize(request, Execute(request)); + } + + public IRestResponse ExecuteAsGet(IRestRequest request, string httpMethod) where T : new() + { + return Deserialize(request, ExecuteAsGet(request, httpMethod)); + } + + public IRestResponse ExecuteAsPost(IRestRequest request, string httpMethod) where T : new() + { + return Deserialize(request, ExecuteAsPost(request, httpMethod)); + } + } +} #endif \ No newline at end of file From bade8c3d35b07081c3205ac194b2f5a91e9391b5 Mon Sep 17 00:00:00 2001 From: scidec Date: Thu, 17 Jul 2014 11:47:44 -0600 Subject: [PATCH 4/5] Fix spaces vs tabs. --- RestSharp/Enum.cs | 164 ++++++------ RestSharp/Http.Async.cs | 40 +-- RestSharp/Http.Sync.cs | 8 +- RestSharp/Http.cs | 68 +++-- RestSharp/IHttp.cs | 178 ++++++------- RestSharp/RestClient.Async.cs | 456 +++++++++++++++++----------------- RestSharp/RestClient.Sync.cs | 4 +- 7 files changed, 458 insertions(+), 460 deletions(-) diff --git a/RestSharp/Enum.cs b/RestSharp/Enum.cs index 942dac2e3..7f9712d2d 100644 --- a/RestSharp/Enum.cs +++ b/RestSharp/Enum.cs @@ -1,82 +1,82 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -namespace RestSharp -{ - /// - /// Types of parameters that can be added to requests - /// - public enum ParameterType - { - Cookie, - GetOrPost, - UrlSegment, - HttpHeader, - RequestBody, - QueryString - } - - /// - /// Data formats - /// - public enum DataFormat - { - Json, - Xml - } - - /// - /// HTTP method to use when making requests - /// - public enum Method - { - GET, - POST, - PUT, - DELETE, - HEAD, - OPTIONS, - PATCH, - MERGE, - } - - /// - /// Format strings for commonly-used date formats - /// - public struct DateFormat - { - /// - /// .NET format string for ISO 8601 date format - /// - public const string Iso8601 = "s"; - /// - /// .NET format string for roundtrip date format - /// - public const string RoundTrip = "u"; - } - - /// - /// Status for responses (surprised?) - /// - public enum ResponseStatus - { - None, - Completed, - Error, - TimedOut, - Aborted - } -} +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +namespace RestSharp +{ + /// + /// Types of parameters that can be added to requests + /// + public enum ParameterType + { + Cookie, + GetOrPost, + UrlSegment, + HttpHeader, + RequestBody, + QueryString + } + + /// + /// Data formats + /// + public enum DataFormat + { + Json, + Xml + } + + /// + /// HTTP method to use when making requests + /// + public enum Method + { + GET, + POST, + PUT, + DELETE, + HEAD, + OPTIONS, + PATCH, + MERGE, + } + + /// + /// Format strings for commonly-used date formats + /// + public struct DateFormat + { + /// + /// .NET format string for ISO 8601 date format + /// + public const string Iso8601 = "s"; + /// + /// .NET format string for roundtrip date format + /// + public const string RoundTrip = "u"; + } + + /// + /// Status for responses (surprised?) + /// + public enum ResponseStatus + { + None, + Completed, + Error, + TimedOut, + Aborted + } +} diff --git a/RestSharp/Http.Async.cs b/RestSharp/Http.Async.cs index ab29d68ad..ad06cfbed 100644 --- a/RestSharp/Http.Async.cs +++ b/RestSharp/Http.Async.cs @@ -19,14 +19,14 @@ using System.Threading; using RestSharp.Extensions; -#if SILVERLIGHT -using System.Windows.Browser; -using System.Net.Browser; +#if SILVERLIGHT +using System.Windows.Browser; +using System.Net.Browser; #endif -#if WINDOWS_PHONE -using System.Windows.Threading; -using System.Windows; +#if WINDOWS_PHONE +using System.Windows.Threading; +using System.Windows; #endif #if (FRAMEWORK && !MONOTOUCH && !MONODROID && !PocketPC) @@ -89,8 +89,8 @@ public HttpWebRequest MergeAsync(Action action) /// public HttpWebRequest AsPostAsync(Action action, string httpMethod) { -#if PocketPC - return PutPostInternalAsync(httpMethod.ToUpper(), action); +#if PocketPC + return PutPostInternalAsync(httpMethod.ToUpper(), action); #else return PutPostInternalAsync(httpMethod.ToUpperInvariant(), action); #endif @@ -103,8 +103,8 @@ public HttpWebRequest AsPostAsync(Action action, string httpMethod /// public HttpWebRequest AsGetAsync(Action action, string httpMethod) { -#if PocketPC - return GetStyleMethodInternalAsync(httpMethod.ToUpper(), action); +#if PocketPC + return GetStyleMethodInternalAsync(httpMethod.ToUpper(), action); #else return GetStyleMethodInternalAsync(httpMethod.ToUpperInvariant(), action); #endif @@ -362,13 +362,13 @@ private static void ExecuteCallback(HttpResponse response, Action partial void AddAsyncHeaderActions() { -#if SILVERLIGHT - _restrictedHeaderActions.Add("Content-Length", (r, v) => r.ContentLength = Convert.ToInt64(v)); +#if SILVERLIGHT + _restrictedHeaderActions.Add("Content-Length", (r, v) => r.ContentLength = Convert.ToInt64(v)); #endif -#if WINDOWS_PHONE - // WP7 doesn't as of Beta doesn't support a way to set Content-Length either directly - // or indirectly - _restrictedHeaderActions.Add("Content-Length", (r, v) => { }); +#if WINDOWS_PHONE + // WP7 doesn't as of Beta doesn't support a way to set Content-Length either directly + // or indirectly + _restrictedHeaderActions.Add("Content-Length", (r, v) => { }); #endif } @@ -376,9 +376,9 @@ partial void AddAsyncHeaderActions() // TODO: duplication at the moment). private HttpWebRequest ConfigureAsyncWebRequest(string method, Uri url) { -#if SILVERLIGHT - WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp); - WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp); +#if SILVERLIGHT + WebRequest.RegisterPrefix("http://", WebRequestCreator.ClientHttp); + WebRequest.RegisterPrefix("https://", WebRequestCreator.ClientHttp); #endif var webRequest = (HttpWebRequest)WebRequest.Create(url); #if !PocketPC @@ -457,4 +457,4 @@ private class TimeOutState public HttpWebRequest Request { get; set; } } } -} \ No newline at end of file +} diff --git a/RestSharp/Http.Sync.cs b/RestSharp/Http.Sync.cs index 7380b23b0..3773af25b 100644 --- a/RestSharp/Http.Sync.cs +++ b/RestSharp/Http.Sync.cs @@ -102,8 +102,8 @@ public HttpResponse Merge() /// public HttpResponse AsGet(string httpMethod) { -#if PocketPC - return GetStyleMethodInternal(httpMethod.ToUpper()); +#if PocketPC + return GetStyleMethodInternal(httpMethod.ToUpper()); #else return GetStyleMethodInternal(httpMethod.ToUpperInvariant()); #endif @@ -116,8 +116,8 @@ public HttpResponse AsGet(string httpMethod) /// public HttpResponse AsPost(string httpMethod) { -#if PocketPC - return PostPutInternal(httpMethod.ToUpper()); +#if PocketPC + return PostPutInternal(httpMethod.ToUpper()); #else return PostPutInternal(httpMethod.ToUpperInvariant()); #endif diff --git a/RestSharp/Http.cs b/RestSharp/Http.cs index d8e8f5d7c..8ccb78a80 100644 --- a/RestSharp/Http.cs +++ b/RestSharp/Http.cs @@ -23,8 +23,8 @@ using System.Text; using RestSharp.Extensions; -#if WINDOWS_PHONE -using RestSharp.Compression.ZLib; +#if WINDOWS_PHONE +using RestSharp.Compression.ZLib; #endif namespace RestSharp @@ -178,9 +178,7 @@ protected bool HasFiles /// URL to call for this request /// public Uri Url { get; set; } - /// - /// Flag to send authorisation header with the HttpWebRequest - /// + public bool PreAuthenticate { get; set; } #if FRAMEWORK || PocketPC @@ -212,16 +210,16 @@ private void AddSharedHeaderActions() { _restrictedHeaderActions.Add("Accept", (r, v) => r.Accept = v); _restrictedHeaderActions.Add("Content-Type", (r, v) => r.ContentType = v); -#if NET4 - _restrictedHeaderActions.Add("Date", (r, v) => - { - DateTime parsed; - if (DateTime.TryParse(v, out parsed)) - { - r.Date = parsed; - } - }); - _restrictedHeaderActions.Add("Host", (r, v) => r.Host = v); +#if NET4 + _restrictedHeaderActions.Add("Date", (r, v) => + { + DateTime parsed; + if (DateTime.TryParse(v, out parsed)) + { + r.Date = parsed; + } + }); + _restrictedHeaderActions.Add("Host", (r, v) => r.Host = v); #else _restrictedHeaderActions.Add("Date", (r, v) => { /* Set by system */ }); _restrictedHeaderActions.Add("Host", (r, v) => { /* Set by system */ }); @@ -270,8 +268,8 @@ private void AppendHeaders(HttpWebRequest webRequest) { #if FRAMEWORK webRequest.Headers.Add(header.Name, header.Value); -#else - webRequest.Headers[header.Name] = header.Value; +#else + webRequest.Headers[header.Name] = header.Value; #endif } } @@ -293,14 +291,14 @@ private void AppendCookies(HttpWebRequest webRequest) Domain = webRequest.RequestUri.Host }; webRequest.CookieContainer.Add(cookie); -#else - var cookie = new Cookie - { - Name = httpCookie.Name, - Value = httpCookie.Value - }; - var uri = webRequest.RequestUri; - webRequest.CookieContainer.Add(new Uri(string.Format("{0}://{1}", uri.Scheme, uri.Host)), cookie); +#else + var cookie = new Cookie + { + Name = httpCookie.Name, + Value = httpCookie.Value + }; + var uri = webRequest.RequestUri; + webRequest.CookieContainer.Add(new Uri(string.Format("{0}://{1}", uri.Scheme, uri.Host)), cookie); #endif #endif } @@ -373,16 +371,16 @@ private void ExtractResponseData(HttpResponse response, HttpWebResponse webRespo response.ContentType = webResponse.ContentType; response.ContentLength = webResponse.ContentLength; Stream webResponseStream = webResponse.GetResponseStream(); -#if WINDOWS_PHONE - if (String.Equals(webResponse.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) - { - var gzStream = new GZipStream(webResponseStream); - ProcessResponseStream(gzStream, response); - } - else - { - ProcessResponseStream(webResponseStream, response); - } +#if WINDOWS_PHONE + if (String.Equals(webResponse.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) + { + var gzStream = new GZipStream(webResponseStream); + ProcessResponseStream(gzStream, response); + } + else + { + ProcessResponseStream(webResponseStream, response); + } #else ProcessResponseStream(webResponseStream, response); #endif diff --git a/RestSharp/IHttp.cs b/RestSharp/IHttp.cs index 7e0e336d3..8857f2bc1 100644 --- a/RestSharp/IHttp.cs +++ b/RestSharp/IHttp.cs @@ -1,92 +1,92 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -using System; -using System.Collections.Generic; -using System.IO; -using System.Net; -using System.Security.Cryptography.X509Certificates; - -namespace RestSharp -{ - public interface IHttp - { - Action ResponseWriter { get; set; } -#if !PocketPC - CookieContainer CookieContainer { get; set; } -#endif - ICredentials Credentials { get; set; } - - /// - /// Always send a multipart/form-data request - even when no Files are present. - /// - bool AlwaysMultipartFormData { get; set; } - - string UserAgent { get; set; } - int Timeout { get; set; } - int ReadWriteTimeout { get; set; } -#if !SILVERLIGHT - bool FollowRedirects { get; set; } -#endif -#if FRAMEWORK - X509CertificateCollection ClientCertificates { get; set; } - int? MaxRedirects { get; set; } -#endif -#if !PocketPC - bool UseDefaultCredentials { get; set; } -#endif - IList Headers { get; } - IList Parameters { get; } - IList Files { get; } - IList Cookies { get; } - string RequestBody { get; set; } - string RequestContentType { get; set; } - bool PreAuthenticate { get; set; } - - /// - /// An alternative to RequestBody, for when the caller already has the byte array. - /// - byte[] RequestBodyBytes { get; set; } - - Uri Url { get; set; } - - HttpWebRequest DeleteAsync(Action action); - HttpWebRequest GetAsync(Action action); - HttpWebRequest HeadAsync(Action action); - HttpWebRequest OptionsAsync(Action action); - HttpWebRequest PostAsync(Action action); - HttpWebRequest PutAsync(Action action); +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +using System; +using System.Collections.Generic; +using System.IO; +using System.Net; +using System.Security.Cryptography.X509Certificates; + +namespace RestSharp +{ + public interface IHttp + { + Action ResponseWriter { get; set; } +#if !PocketPC + CookieContainer CookieContainer { get; set; } +#endif + ICredentials Credentials { get; set; } + + /// + /// Always send a multipart/form-data request - even when no Files are present. + /// + bool AlwaysMultipartFormData { get; set; } + + string UserAgent { get; set; } + int Timeout { get; set; } + int ReadWriteTimeout { get; set; } +#if !SILVERLIGHT + bool FollowRedirects { get; set; } +#endif +#if FRAMEWORK + X509CertificateCollection ClientCertificates { get; set; } + int? MaxRedirects { get; set; } +#endif +#if !PocketPC + bool UseDefaultCredentials { get; set; } +#endif + IList Headers { get; } + IList Parameters { get; } + IList Files { get; } + IList Cookies { get; } + string RequestBody { get; set; } + string RequestContentType { get; set; } + bool PreAuthenticate { get; set; } + + /// + /// An alternative to RequestBody, for when the caller already has the byte array. + /// + byte[] RequestBodyBytes { get; set; } + + Uri Url { get; set; } + + HttpWebRequest DeleteAsync(Action action); + HttpWebRequest GetAsync(Action action); + HttpWebRequest HeadAsync(Action action); + HttpWebRequest OptionsAsync(Action action); + HttpWebRequest PostAsync(Action action); + HttpWebRequest PutAsync(Action action); HttpWebRequest PatchAsync(Action action); - HttpWebRequest MergeAsync(Action action); - HttpWebRequest AsPostAsync(Action action, string httpMethod); - HttpWebRequest AsGetAsync(Action action, string httpMethod); - -#if FRAMEWORK || PocketPC - HttpResponse Delete(); - HttpResponse Get(); - HttpResponse Head(); - HttpResponse Options(); - HttpResponse Post(); - HttpResponse Put(); + HttpWebRequest MergeAsync(Action action); + HttpWebRequest AsPostAsync(Action action, string httpMethod); + HttpWebRequest AsGetAsync(Action action, string httpMethod); + +#if FRAMEWORK || PocketPC + HttpResponse Delete(); + HttpResponse Get(); + HttpResponse Head(); + HttpResponse Options(); + HttpResponse Post(); + HttpResponse Put(); HttpResponse Patch(); - HttpResponse Merge(); - HttpResponse AsPost(string httpMethod); - HttpResponse AsGet(string httpMethod); - - IWebProxy Proxy { get; set; } -#endif - } + HttpResponse Merge(); + HttpResponse AsPost(string httpMethod); + HttpResponse AsGet(string httpMethod); + + IWebProxy Proxy { get; set; } +#endif + } } \ No newline at end of file diff --git a/RestSharp/RestClient.Async.cs b/RestSharp/RestClient.Async.cs index 20627f497..208738962 100644 --- a/RestSharp/RestClient.Async.cs +++ b/RestSharp/RestClient.Async.cs @@ -18,8 +18,8 @@ using System.Collections.Generic; using System.Linq; using System.Threading; -#if NET4 || MONODROID || MONOTOUCH || WP8 -using System.Threading.Tasks; +#if NET4 || MONODROID || MONOTOUCH || WP8 +using System.Threading.Tasks; #endif using System.Text; using System.Net; @@ -37,8 +37,8 @@ public partial class RestClient /// Callback function to be executed upon completion providing access to the async handle. public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action callback) { -#if PocketPC - string method = request.Method.ToString(); +#if PocketPC + string method = request.Method.ToString(); #else string method = Enum.GetName(typeof(Method), request.Method); #endif @@ -168,230 +168,230 @@ private void DeserializeResponse(IRestRequest request, Action - /// Executes a GET-style request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - public virtual Task> ExecuteGetTaskAsync(IRestRequest request) - { - return ExecuteGetTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes a GET-style request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// The cancellation token - public virtual Task> ExecuteGetTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - request.Method = Method.GET; - return ExecuteTaskAsync(request, token); - } - - /// - /// Executes a POST-style request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - public virtual Task> ExecutePostTaskAsync(IRestRequest request) - { - return ExecutePostTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes a POST-style request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// The cancellation token - public virtual Task> ExecutePostTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - request.Method = Method.POST; - return ExecuteTaskAsync(request, token); - } - - /// - /// Executes the request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - public virtual Task> ExecuteTaskAsync(IRestRequest request) - { - return ExecuteTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes the request asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// The cancellation token - public virtual Task> ExecuteTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - var taskCompletionSource = new TaskCompletionSource>(); - - try - { - var async = ExecuteAsync(request, (response, _) => - { - if (token.IsCancellationRequested) - { - taskCompletionSource.TrySetCanceled(); - } - else if (response.ErrorException != null) - { - taskCompletionSource.TrySetException(response.ErrorException); - } - else if (response.ResponseStatus != ResponseStatus.Completed) - { - taskCompletionSource.TrySetException(response.ResponseStatus.ToWebException()); - } - else - { - taskCompletionSource.TrySetResult(response); - } - }); - - token.Register(() => - { - async.Abort(); - taskCompletionSource.TrySetCanceled(); - }); - } - catch (Exception ex) - { - taskCompletionSource.TrySetException(ex); - } - - return taskCompletionSource.Task; - } - - /// - /// Executes the request asynchronously, authenticating if needed - /// - /// Request to be executed - public virtual Task ExecuteTaskAsync(IRestRequest request) - { - return ExecuteTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes a GET-style asynchronously, authenticating if needed - /// - /// Request to be executed - public virtual Task ExecuteGetTaskAsync(IRestRequest request) - { - return this.ExecuteGetTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes a GET-style asynchronously, authenticating if needed - /// - /// Request to be executed - /// The cancellation token - public virtual Task ExecuteGetTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - request.Method = Method.GET; - return ExecuteTaskAsync(request, token); - } - - /// - /// Executes a POST-style asynchronously, authenticating if needed - /// - /// Request to be executed - public virtual Task ExecutePostTaskAsync(IRestRequest request) - { - return this.ExecutePostTaskAsync(request, CancellationToken.None); - } - - /// - /// Executes a POST-style asynchronously, authenticating if needed - /// - /// Request to be executed - /// The cancellation token - public virtual Task ExecutePostTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - request.Method = Method.POST; - return ExecuteTaskAsync(request, token); - } - - /// - /// Executes the request asynchronously, authenticating if needed - /// - /// Request to be executed - /// The cancellation token - public virtual Task ExecuteTaskAsync(IRestRequest request, CancellationToken token) - { - if (request == null) - { - throw new ArgumentNullException("request"); - } - - var taskCompletionSource = new TaskCompletionSource(); - - try - { - var async = this.ExecuteAsync(request, (response, _) => - { - if (token.IsCancellationRequested) - { - taskCompletionSource.TrySetCanceled(); - } - else if (response.ErrorException != null) - { - taskCompletionSource.TrySetException(response.ErrorException); - } - else if (response.ResponseStatus != ResponseStatus.Completed) - { - taskCompletionSource.TrySetException(response.ResponseStatus.ToWebException()); - } - else - { - taskCompletionSource.TrySetResult(response); - } - }); - - token.Register(() => - { - async.Abort(); - taskCompletionSource.TrySetCanceled(); - }); - } - catch (Exception ex) - { - taskCompletionSource.TrySetException(ex); - } - - return taskCompletionSource.Task; - } +#if NET4 || MONODROID || MONOTOUCH || WP8 + /// + /// Executes a GET-style request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + public virtual Task> ExecuteGetTaskAsync(IRestRequest request) + { + return ExecuteGetTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes a GET-style request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// The cancellation token + public virtual Task> ExecuteGetTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + request.Method = Method.GET; + return ExecuteTaskAsync(request, token); + } + + /// + /// Executes a POST-style request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + public virtual Task> ExecutePostTaskAsync(IRestRequest request) + { + return ExecutePostTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes a POST-style request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// The cancellation token + public virtual Task> ExecutePostTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + request.Method = Method.POST; + return ExecuteTaskAsync(request, token); + } + + /// + /// Executes the request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + public virtual Task> ExecuteTaskAsync(IRestRequest request) + { + return ExecuteTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes the request asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// The cancellation token + public virtual Task> ExecuteTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + var taskCompletionSource = new TaskCompletionSource>(); + + try + { + var async = ExecuteAsync(request, (response, _) => + { + if (token.IsCancellationRequested) + { + taskCompletionSource.TrySetCanceled(); + } + else if (response.ErrorException != null) + { + taskCompletionSource.TrySetException(response.ErrorException); + } + else if (response.ResponseStatus != ResponseStatus.Completed) + { + taskCompletionSource.TrySetException(response.ResponseStatus.ToWebException()); + } + else + { + taskCompletionSource.TrySetResult(response); + } + }); + + token.Register(() => + { + async.Abort(); + taskCompletionSource.TrySetCanceled(); + }); + } + catch (Exception ex) + { + taskCompletionSource.TrySetException(ex); + } + + return taskCompletionSource.Task; + } + + /// + /// Executes the request asynchronously, authenticating if needed + /// + /// Request to be executed + public virtual Task ExecuteTaskAsync(IRestRequest request) + { + return ExecuteTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes a GET-style asynchronously, authenticating if needed + /// + /// Request to be executed + public virtual Task ExecuteGetTaskAsync(IRestRequest request) + { + return this.ExecuteGetTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes a GET-style asynchronously, authenticating if needed + /// + /// Request to be executed + /// The cancellation token + public virtual Task ExecuteGetTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + request.Method = Method.GET; + return ExecuteTaskAsync(request, token); + } + + /// + /// Executes a POST-style asynchronously, authenticating if needed + /// + /// Request to be executed + public virtual Task ExecutePostTaskAsync(IRestRequest request) + { + return this.ExecutePostTaskAsync(request, CancellationToken.None); + } + + /// + /// Executes a POST-style asynchronously, authenticating if needed + /// + /// Request to be executed + /// The cancellation token + public virtual Task ExecutePostTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + request.Method = Method.POST; + return ExecuteTaskAsync(request, token); + } + + /// + /// Executes the request asynchronously, authenticating if needed + /// + /// Request to be executed + /// The cancellation token + public virtual Task ExecuteTaskAsync(IRestRequest request, CancellationToken token) + { + if (request == null) + { + throw new ArgumentNullException("request"); + } + + var taskCompletionSource = new TaskCompletionSource(); + + try + { + var async = this.ExecuteAsync(request, (response, _) => + { + if (token.IsCancellationRequested) + { + taskCompletionSource.TrySetCanceled(); + } + else if (response.ErrorException != null) + { + taskCompletionSource.TrySetException(response.ErrorException); + } + else if (response.ResponseStatus != ResponseStatus.Completed) + { + taskCompletionSource.TrySetException(response.ResponseStatus.ToWebException()); + } + else + { + taskCompletionSource.TrySetResult(response); + } + }); + + token.Register(() => + { + async.Abort(); + taskCompletionSource.TrySetCanceled(); + }); + } + catch (Exception ex) + { + taskCompletionSource.TrySetException(ex); + } + + return taskCompletionSource.Task; + } #endif } } \ No newline at end of file diff --git a/RestSharp/RestClient.Sync.cs b/RestSharp/RestClient.Sync.cs index 2946d33d6..f4cfe70d4 100644 --- a/RestSharp/RestClient.Sync.cs +++ b/RestSharp/RestClient.Sync.cs @@ -30,8 +30,8 @@ public byte[] DownloadData(IRestRequest request) /// RestResponse public virtual IRestResponse Execute(IRestRequest request) { -#if PocketPC - var method = request.Method.ToString(); +#if PocketPC + var method = request.Method.ToString(); #else var method = Enum.GetName(typeof(Method), request.Method); #endif From be303eaeec701ea5b97589987533428f25d795c5 Mon Sep 17 00:00:00 2001 From: scidec Date: Thu, 17 Jul 2014 12:29:21 -0600 Subject: [PATCH 5/5] Fix spaces vs tabs. --- RestSharp/Enum.cs | 164 +++---- RestSharp/Http.Async.cs | 56 +-- RestSharp/Http.Sync.cs | 50 +- RestSharp/Http.cs | 838 +++++++++++++++++----------------- RestSharp/RestClient.Async.cs | 306 ++++++------- RestSharp/RestClient.Sync.cs | 200 ++++---- 6 files changed, 807 insertions(+), 807 deletions(-) diff --git a/RestSharp/Enum.cs b/RestSharp/Enum.cs index 7f9712d2d..942dac2e3 100644 --- a/RestSharp/Enum.cs +++ b/RestSharp/Enum.cs @@ -1,82 +1,82 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -namespace RestSharp -{ - /// - /// Types of parameters that can be added to requests - /// - public enum ParameterType - { - Cookie, - GetOrPost, - UrlSegment, - HttpHeader, - RequestBody, - QueryString - } - - /// - /// Data formats - /// - public enum DataFormat - { - Json, - Xml - } - - /// - /// HTTP method to use when making requests - /// - public enum Method - { - GET, - POST, - PUT, - DELETE, - HEAD, - OPTIONS, - PATCH, - MERGE, - } - - /// - /// Format strings for commonly-used date formats - /// - public struct DateFormat - { - /// - /// .NET format string for ISO 8601 date format - /// - public const string Iso8601 = "s"; - /// - /// .NET format string for roundtrip date format - /// - public const string RoundTrip = "u"; - } - - /// - /// Status for responses (surprised?) - /// - public enum ResponseStatus - { - None, - Completed, - Error, - TimedOut, - Aborted - } -} +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +namespace RestSharp +{ + /// + /// Types of parameters that can be added to requests + /// + public enum ParameterType + { + Cookie, + GetOrPost, + UrlSegment, + HttpHeader, + RequestBody, + QueryString + } + + /// + /// Data formats + /// + public enum DataFormat + { + Json, + Xml + } + + /// + /// HTTP method to use when making requests + /// + public enum Method + { + GET, + POST, + PUT, + DELETE, + HEAD, + OPTIONS, + PATCH, + MERGE, + } + + /// + /// Format strings for commonly-used date formats + /// + public struct DateFormat + { + /// + /// .NET format string for ISO 8601 date format + /// + public const string Iso8601 = "s"; + /// + /// .NET format string for roundtrip date format + /// + public const string RoundTrip = "u"; + } + + /// + /// Status for responses (surprised?) + /// + public enum ResponseStatus + { + None, + Completed, + Error, + TimedOut, + Aborted + } +} diff --git a/RestSharp/Http.Async.cs b/RestSharp/Http.Async.cs index ad06cfbed..4ccc7ed11 100644 --- a/RestSharp/Http.Async.cs +++ b/RestSharp/Http.Async.cs @@ -1,36 +1,36 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -using System; -using System.Net; -using System.Threading; -using RestSharp.Extensions; - +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +using System; +using System.Net; +using System.Threading; +using RestSharp.Extensions; + #if SILVERLIGHT using System.Windows.Browser; using System.Net.Browser; -#endif - +#endif + #if WINDOWS_PHONE using System.Windows.Threading; using System.Windows; -#endif - -#if (FRAMEWORK && !MONOTOUCH && !MONODROID && !PocketPC) -using System.Web; +#endif + +#if (FRAMEWORK && !MONOTOUCH && !MONODROID && !PocketPC) +using System.Web; #endif namespace RestSharp @@ -457,4 +457,4 @@ private class TimeOutState public HttpWebRequest Request { get; set; } } } -} +} \ No newline at end of file diff --git a/RestSharp/Http.Sync.cs b/RestSharp/Http.Sync.cs index 3773af25b..09a3acf49 100644 --- a/RestSharp/Http.Sync.cs +++ b/RestSharp/Http.Sync.cs @@ -1,27 +1,27 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -#if FRAMEWORK || PocketPC -using System; -using System.Net; - -#if !MONOTOUCH && !MONODROID && !PocketPC -using System.Web; -#endif - +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +#if FRAMEWORK || PocketPC +using System; +using System.Net; + +#if !MONOTOUCH && !MONODROID && !PocketPC +using System.Web; +#endif + using RestSharp.Extensions; namespace RestSharp @@ -305,5 +305,5 @@ private HttpWebRequest ConfigureWebRequest(string method, Uri url) return webRequest; } } -} +} #endif \ No newline at end of file diff --git a/RestSharp/Http.cs b/RestSharp/Http.cs index 8ccb78a80..136f1ca1f 100644 --- a/RestSharp/Http.cs +++ b/RestSharp/Http.cs @@ -1,215 +1,216 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -using System; -using System.Collections.Generic; -using System.IO; -using System.Linq; -using System.Net; -using System.Security.Cryptography.X509Certificates; -using System.Text; -using RestSharp.Extensions; - +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +using System; +using System.Collections.Generic; +using System.IO; +using System.Linq; +using System.Net; +using System.Security.Cryptography.X509Certificates; +using System.Text; +using RestSharp.Extensions; + #if WINDOWS_PHONE using RestSharp.Compression.ZLib; -#endif - -namespace RestSharp -{ - /// - /// HttpWebRequest wrapper - /// - public partial class Http : IHttp, IHttpFactory - { - private const string _lineBreak = "\r\n"; - private static readonly Encoding _defaultEncoding = Encoding.UTF8; - - /// - /// Creates an IHttp - /// - /// - public IHttp Create() - { - return new Http(); - } - - /// - /// True if this HTTP request has any HTTP parameters - /// - protected bool HasParameters - { - get - { - return Parameters.Any(); - } - } - - /// - /// True if this HTTP request has any HTTP cookies - /// - protected bool HasCookies - { - get - { - return Cookies.Any(); - } - } - - /// - /// True if a request body has been specified - /// - protected bool HasBody - { - get - { - return RequestBodyBytes != null || !string.IsNullOrEmpty(RequestBody); - } - } - - /// - /// True if files have been set to be uploaded - /// - protected bool HasFiles - { - get - { - return Files.Any(); - } - } - - /// - /// Always send a multipart/form-data request - even when no Files are present. - /// - public bool AlwaysMultipartFormData { get; set; } - - /// - /// UserAgent to be sent with request - /// - public string UserAgent { get; set; } - /// - /// Timeout in milliseconds to be used for the request - /// - public int Timeout { get; set; } - /// - /// The number of milliseconds before the writing or reading times out. - /// - public int ReadWriteTimeout { get; set; } - /// - /// System.Net.ICredentials to be sent with request - /// - public ICredentials Credentials { get; set; } - /// - /// The System.Net.CookieContainer to be used for the request - /// -#if !PocketPC - public CookieContainer CookieContainer { get; set; } -#endif - /// - /// The method to use to write the response instead of reading into RawBytes - /// - public Action ResponseWriter { get; set; } - /// - /// Collection of files to be sent with request - /// - public IList Files { get; private set; } -#if !SILVERLIGHT - /// - /// Whether or not HTTP 3xx response redirects should be automatically followed - /// - public bool FollowRedirects { get; set; } -#endif -#if FRAMEWORK - /// - /// X509CertificateCollection to be sent with request - /// - public X509CertificateCollection ClientCertificates { get; set; } -#endif -#if FRAMEWORK || PocketPC - /// - /// Maximum number of automatic redirects to follow if FollowRedirects is true - /// - public int? MaxRedirects { get; set; } -#endif - /// - /// Determine whether or not the "default credentials" (e.g. the user account under which the current process is running) - /// will be sent along to the server. - /// -#if !PocketPC - public bool UseDefaultCredentials { get; set; } -#endif - /// - /// HTTP headers to be sent with request - /// - public IList Headers { get; private set; } - /// - /// HTTP parameters (QueryString or Form values) to be sent with request - /// - public IList Parameters { get; private set; } - /// - /// HTTP cookies to be sent with request - /// - public IList Cookies { get; private set; } - /// - /// Request body to be sent with request - /// - public string RequestBody { get; set; } - /// - /// Content type of the request body. - /// - public string RequestContentType { get; set; } - /// - /// An alternative to RequestBody, for when the caller already has the byte array. - /// - public byte[] RequestBodyBytes { get; set; } - /// - /// URL to call for this request - /// +#endif + +namespace RestSharp +{ + /// + /// HttpWebRequest wrapper + /// + public partial class Http : IHttp, IHttpFactory + { + private const string _lineBreak = "\r\n"; + private static readonly Encoding _defaultEncoding = Encoding.UTF8; + + /// + /// Creates an IHttp + /// + /// + public IHttp Create() + { + return new Http(); + } + + /// + /// True if this HTTP request has any HTTP parameters + /// + protected bool HasParameters + { + get + { + return Parameters.Any(); + } + } + + /// + /// True if this HTTP request has any HTTP cookies + /// + protected bool HasCookies + { + get + { + return Cookies.Any(); + } + } + + /// + /// True if a request body has been specified + /// + protected bool HasBody + { + get + { + return RequestBodyBytes != null || !string.IsNullOrEmpty(RequestBody); + } + } + + /// + /// True if files have been set to be uploaded + /// + protected bool HasFiles + { + get + { + return Files.Any(); + } + } + + /// + /// Always send a multipart/form-data request - even when no Files are present. + /// + public bool AlwaysMultipartFormData { get; set; } + /// + /// UserAgent to be sent with request + /// + public string UserAgent { get; set; } + /// + /// Timeout in milliseconds to be used for the request + /// + public int Timeout { get; set; } + /// + /// The number of milliseconds before the writing or reading times out. + /// + public int ReadWriteTimeout { get; set; } + /// + /// System.Net.ICredentials to be sent with request + /// + public ICredentials Credentials { get; set; } + /// + /// The System.Net.CookieContainer to be used for the request + /// +#if !PocketPC + public CookieContainer CookieContainer { get; set; } +#endif + /// + /// The method to use to write the response instead of reading into RawBytes + /// + public Action ResponseWriter { get; set; } + /// + /// Collection of files to be sent with request + /// + public IList Files { get; private set; } +#if !SILVERLIGHT + /// + /// Whether or not HTTP 3xx response redirects should be automatically followed + /// + public bool FollowRedirects { get; set; } +#endif +#if FRAMEWORK + /// + /// X509CertificateCollection to be sent with request + /// + public X509CertificateCollection ClientCertificates { get; set; } +#endif +#if FRAMEWORK || PocketPC + /// + /// Maximum number of automatic redirects to follow if FollowRedirects is true + /// + public int? MaxRedirects { get; set; } +#endif + /// + /// Determine whether or not the "default credentials" (e.g. the user account under which the current process is running) + /// will be sent along to the server. + /// +#if !PocketPC + public bool UseDefaultCredentials { get; set; } +#endif + /// + /// HTTP headers to be sent with request + /// + public IList Headers { get; private set; } + /// + /// HTTP parameters (QueryString or Form values) to be sent with request + /// + public IList Parameters { get; private set; } + /// + /// HTTP cookies to be sent with request + /// + public IList Cookies { get; private set; } + /// + /// Request body to be sent with request + /// + public string RequestBody { get; set; } + /// + /// Content type of the request body. + /// + public string RequestContentType { get; set; } + /// + /// An alternative to RequestBody, for when the caller already has the byte array. + /// + public byte[] RequestBodyBytes { get; set; } + /// + /// URL to call for this request + /// public Uri Url { get; set; } - - public bool PreAuthenticate { get; set; } - -#if FRAMEWORK || PocketPC - /// - /// Proxy info to be sent with request - /// - public IWebProxy Proxy { get; set; } -#endif - /// - /// Default constructor - /// - public Http() - { - Headers = new List(); - Files = new List(); - Parameters = new List(); - Cookies = new List(); - - _restrictedHeaderActions = new Dictionary>(StringComparer.OrdinalIgnoreCase); - - AddSharedHeaderActions(); - AddSyncHeaderActions(); - } - - partial void AddSyncHeaderActions(); - partial void AddAsyncHeaderActions(); - private void AddSharedHeaderActions() - { - _restrictedHeaderActions.Add("Accept", (r, v) => r.Accept = v); - _restrictedHeaderActions.Add("Content-Type", (r, v) => r.ContentType = v); + /// Flag to send authorisation header with the HttpWebRequest + /// + public bool PreAuthenticate { get; set; } + +#if FRAMEWORK || PocketPC + /// + /// Proxy info to be sent with request + /// + public IWebProxy Proxy { get; set; } +#endif + + /// + /// Default constructor + /// + public Http() + { + Headers = new List(); + Files = new List(); + Parameters = new List(); + Cookies = new List(); + + _restrictedHeaderActions = new Dictionary>(StringComparer.OrdinalIgnoreCase); + + AddSharedHeaderActions(); + AddSyncHeaderActions(); + } + + partial void AddSyncHeaderActions(); + partial void AddAsyncHeaderActions(); + private void AddSharedHeaderActions() + { + _restrictedHeaderActions.Add("Accept", (r, v) => r.Accept = v); + _restrictedHeaderActions.Add("Content-Type", (r, v) => r.ContentType = v); #if NET4 _restrictedHeaderActions.Add("Date", (r, v) => { @@ -220,77 +221,77 @@ private void AddSharedHeaderActions() } }); _restrictedHeaderActions.Add("Host", (r, v) => r.Host = v); -#else - _restrictedHeaderActions.Add("Date", (r, v) => { /* Set by system */ }); - _restrictedHeaderActions.Add("Host", (r, v) => { /* Set by system */ }); -#endif -#if FRAMEWORK - _restrictedHeaderActions.Add("Range", (r, v) => { AddRange(r, v); }); -#endif - } - - private const string FormBoundary = "-----------------------------28947758029299"; - private static string GetMultipartFormContentType() - { - return string.Format("multipart/form-data; boundary={0}", FormBoundary); - } - - private static string GetMultipartFileHeader(HttpFile file) - { - return string.Format("--{0}{4}Content-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"{4}Content-Type: {3}{4}{4}", - FormBoundary, file.Name, file.FileName, file.ContentType ?? "application/octet-stream", _lineBreak); - } - - private static string GetMultipartFormData(HttpParameter param) - { - return string.Format("--{0}{3}Content-Disposition: form-data; name=\"{1}\"{3}{3}{2}{3}", - FormBoundary, param.Name, param.Value, _lineBreak); - } - - private static string GetMultipartFooter() - { - return string.Format("--{0}--{1}", FormBoundary, _lineBreak); - } - - private readonly IDictionary> _restrictedHeaderActions; - - // handle restricted headers the .NET way - thanks @dimebrain! - // http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx - private void AppendHeaders(HttpWebRequest webRequest) - { - foreach (var header in Headers) - { - if (_restrictedHeaderActions.ContainsKey(header.Name)) - { - _restrictedHeaderActions[header.Name].Invoke(webRequest, header.Value); - } - else - { -#if FRAMEWORK - webRequest.Headers.Add(header.Name, header.Value); +#else + _restrictedHeaderActions.Add("Date", (r, v) => { /* Set by system */ }); + _restrictedHeaderActions.Add("Host", (r, v) => { /* Set by system */ }); +#endif +#if FRAMEWORK + _restrictedHeaderActions.Add("Range", (r, v) => { AddRange(r, v); }); +#endif + } + + private const string FormBoundary = "-----------------------------28947758029299"; + private static string GetMultipartFormContentType() + { + return string.Format("multipart/form-data; boundary={0}", FormBoundary); + } + + private static string GetMultipartFileHeader (HttpFile file) + { + return string.Format ("--{0}{4}Content-Disposition: form-data; name=\"{1}\"; filename=\"{2}\"{4}Content-Type: {3}{4}{4}", + FormBoundary, file.Name, file.FileName, file.ContentType ?? "application/octet-stream", _lineBreak); + } + + private static string GetMultipartFormData (HttpParameter param) + { + return string.Format ("--{0}{3}Content-Disposition: form-data; name=\"{1}\"{3}{3}{2}{3}", + FormBoundary, param.Name, param.Value, _lineBreak); + } + + private static string GetMultipartFooter () + { + return string.Format ("--{0}--{1}", FormBoundary, _lineBreak); + } + + private readonly IDictionary> _restrictedHeaderActions; + + // handle restricted headers the .NET way - thanks @dimebrain! + // http://msdn.microsoft.com/en-us/library/system.net.httpwebrequest.headers.aspx + private void AppendHeaders(HttpWebRequest webRequest) + { + foreach (var header in Headers) + { + if (_restrictedHeaderActions.ContainsKey(header.Name)) + { + _restrictedHeaderActions[header.Name].Invoke(webRequest, header.Value); + } + else + { +#if FRAMEWORK + webRequest.Headers.Add(header.Name, header.Value); #else webRequest.Headers[header.Name] = header.Value; -#endif - } - } - } - - private void AppendCookies(HttpWebRequest webRequest) - { -#if !PocketPC - webRequest.CookieContainer = this.CookieContainer ?? new CookieContainer(); -#endif - foreach (var httpCookie in Cookies) - { -#if !PocketPC -#if FRAMEWORK - var cookie = new Cookie - { - Name = httpCookie.Name, - Value = httpCookie.Value, - Domain = webRequest.RequestUri.Host - }; - webRequest.CookieContainer.Add(cookie); +#endif + } + } + } + + private void AppendCookies(HttpWebRequest webRequest) + { +#if !PocketPC + webRequest.CookieContainer = this.CookieContainer ?? new CookieContainer(); +#endif + foreach (var httpCookie in Cookies) + { +#if !PocketPC +#if FRAMEWORK + var cookie = new Cookie + { + Name = httpCookie.Name, + Value = httpCookie.Value, + Domain = webRequest.RequestUri.Host + }; + webRequest.CookieContainer.Add(cookie); #else var cookie = new Cookie { @@ -299,78 +300,78 @@ private void AppendCookies(HttpWebRequest webRequest) }; var uri = webRequest.RequestUri; webRequest.CookieContainer.Add(new Uri(string.Format("{0}://{1}", uri.Scheme, uri.Host)), cookie); -#endif -#endif - } - } - - private string EncodeParameters() - { - var querystring = new StringBuilder(); - foreach (var p in Parameters) - { - if (querystring.Length > 1) - querystring.Append("&"); - querystring.AppendFormat("{0}={1}", p.Name.UrlEncode(), p.Value.UrlEncode()); - } - - return querystring.ToString(); - } - - private void PreparePostBody(HttpWebRequest webRequest) - { - if (HasFiles || AlwaysMultipartFormData) - { - webRequest.ContentType = GetMultipartFormContentType(); - } - else if (HasParameters) - { - webRequest.ContentType = "application/x-www-form-urlencoded"; - RequestBody = EncodeParameters(); - } - else if (HasBody) - { - webRequest.ContentType = RequestContentType; - } - } - - private static void WriteStringTo(Stream stream, string toWrite) - { - var bytes = _defaultEncoding.GetBytes(toWrite); - stream.Write(bytes, 0, bytes.Length); - } - - private void WriteMultipartFormData(Stream requestStream) - { - foreach (var param in Parameters) - { - WriteStringTo(requestStream, GetMultipartFormData(param)); - } - - foreach (var file in Files) - { - // Add just the first part of this param, since we will write the file data directly to the Stream - WriteStringTo(requestStream, GetMultipartFileHeader(file)); - - // Write the file data directly to the Stream, rather than serializing it to a string. - file.Writer(requestStream); - WriteStringTo(requestStream, _lineBreak); - } - - WriteStringTo(requestStream, GetMultipartFooter()); - } - - private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse) - { - using (webResponse) - { -#if FRAMEWORK - response.ContentEncoding = webResponse.ContentEncoding; - response.Server = webResponse.Server; -#endif - response.ContentType = webResponse.ContentType; - response.ContentLength = webResponse.ContentLength; - Stream webResponseStream = webResponse.GetResponseStream(); +#endif +#endif + } + } + + private string EncodeParameters() + { + var querystring = new StringBuilder(); + foreach (var p in Parameters) + { + if (querystring.Length > 1) + querystring.Append("&"); + querystring.AppendFormat("{0}={1}", p.Name.UrlEncode(), p.Value.UrlEncode()); + } + + return querystring.ToString(); + } + + private void PreparePostBody(HttpWebRequest webRequest) + { + if (HasFiles || AlwaysMultipartFormData) + { + webRequest.ContentType = GetMultipartFormContentType(); + } + else if(HasParameters) + { + webRequest.ContentType = "application/x-www-form-urlencoded"; + RequestBody = EncodeParameters(); + } + else if(HasBody) + { + webRequest.ContentType = RequestContentType; + } + } + + private static void WriteStringTo(Stream stream, string toWrite) + { + var bytes = _defaultEncoding.GetBytes(toWrite); + stream.Write(bytes, 0, bytes.Length); + } + + private void WriteMultipartFormData(Stream requestStream) + { + foreach (var param in Parameters) + { + WriteStringTo(requestStream, GetMultipartFormData(param)); + } + + foreach (var file in Files) + { + // Add just the first part of this param, since we will write the file data directly to the Stream + WriteStringTo(requestStream, GetMultipartFileHeader(file)); + + // Write the file data directly to the Stream, rather than serializing it to a string. + file.Writer(requestStream); + WriteStringTo(requestStream, _lineBreak); + } + + WriteStringTo(requestStream, GetMultipartFooter()); + } + + private void ExtractResponseData(HttpResponse response, HttpWebResponse webResponse) + { + using (webResponse) + { +#if FRAMEWORK + response.ContentEncoding = webResponse.ContentEncoding; + response.Server = webResponse.Server; +#endif + response.ContentType = webResponse.ContentType; + response.ContentLength = webResponse.ContentLength; + Stream webResponseStream = webResponse.GetResponseStream(); #if WINDOWS_PHONE if (String.Equals(webResponse.Headers[HttpRequestHeader.ContentEncoding], "gzip", StringComparison.OrdinalIgnoreCase)) { @@ -381,74 +382,73 @@ private void ExtractResponseData(HttpResponse response, HttpWebResponse webRespo { ProcessResponseStream(webResponseStream, response); } -#else - ProcessResponseStream(webResponseStream, response); -#endif - response.StatusCode = webResponse.StatusCode; - response.StatusDescription = webResponse.StatusDescription; - response.ResponseUri = webResponse.ResponseUri; - response.ResponseStatus = ResponseStatus.Completed; - -#if !PocketPC - if (webResponse.Cookies != null) - { - foreach (Cookie cookie in webResponse.Cookies) - { - response.Cookies.Add(new HttpCookie - { - Comment = cookie.Comment, - CommentUri = cookie.CommentUri, - Discard = cookie.Discard, - Domain = cookie.Domain, - Expired = cookie.Expired, - Expires = cookie.Expires, - HttpOnly = cookie.HttpOnly, - Name = cookie.Name, - Path = cookie.Path, - Port = cookie.Port, - Secure = cookie.Secure, - TimeStamp = cookie.TimeStamp, - Value = cookie.Value, - Version = cookie.Version - }); - } - } -#endif - foreach (var headerName in webResponse.Headers.AllKeys) - { - var headerValue = webResponse.Headers[headerName]; - response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue }); - } - - webResponse.Close(); - } - } - - private void ProcessResponseStream(Stream webResponseStream, HttpResponse response) - { - if (ResponseWriter == null) - { - response.RawBytes = webResponseStream.ReadAsBytes(); - } - else - { - ResponseWriter(webResponseStream); - } - } - -#if FRAMEWORK - private void AddRange(HttpWebRequest r, string range) - { - System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(range, "=(\\d+)-(\\d+)$"); - if (!m.Success) - { - return; - } - - int from = Convert.ToInt32(m.Groups[1].Value); - int to = Convert.ToInt32(m.Groups[2].Value); - r.AddRange(from, to); - } -#endif - } +#else + ProcessResponseStream(webResponseStream, response); +#endif + response.StatusCode = webResponse.StatusCode; + response.StatusDescription = webResponse.StatusDescription; + response.ResponseUri = webResponse.ResponseUri; + response.ResponseStatus = ResponseStatus.Completed; + +#if !PocketPC + if (webResponse.Cookies != null) + { + foreach (Cookie cookie in webResponse.Cookies) + { + response.Cookies.Add(new HttpCookie { + Comment = cookie.Comment, + CommentUri = cookie.CommentUri, + Discard = cookie.Discard, + Domain = cookie.Domain, + Expired = cookie.Expired, + Expires = cookie.Expires, + HttpOnly = cookie.HttpOnly, + Name = cookie.Name, + Path = cookie.Path, + Port = cookie.Port, + Secure = cookie.Secure, + TimeStamp = cookie.TimeStamp, + Value = cookie.Value, + Version = cookie.Version + }); + } + } +#endif + foreach (var headerName in webResponse.Headers.AllKeys) + { + var headerValue = webResponse.Headers[headerName]; + response.Headers.Add(new HttpHeader { Name = headerName, Value = headerValue }); + } + + webResponse.Close(); + } + } + + private void ProcessResponseStream(Stream webResponseStream, HttpResponse response) + { + if (ResponseWriter == null) + { + response.RawBytes = webResponseStream.ReadAsBytes(); + } + else + { + ResponseWriter(webResponseStream); + } + } + +#if FRAMEWORK + private void AddRange(HttpWebRequest r, string range) + { + System.Text.RegularExpressions.Match m = System.Text.RegularExpressions.Regex.Match(range, "=(\\d+)-(\\d+)$"); + if (!m.Success) + { + return; + } + + int from = Convert.ToInt32(m.Groups[1].Value); + int to = Convert.ToInt32(m.Groups[2].Value); + r.AddRange(from, to); + } +#endif + } } \ No newline at end of file diff --git a/RestSharp/RestClient.Async.cs b/RestSharp/RestClient.Async.cs index 208738962..670420cc5 100644 --- a/RestSharp/RestClient.Async.cs +++ b/RestSharp/RestClient.Async.cs @@ -1,44 +1,44 @@ -#region License -// Copyright 2010 John Sheehan -// -// Licensed under the Apache License, Version 2.0 (the "License"); -// you may not use this file except in compliance with the License. -// You may obtain a copy of the License at -// -// http://www.apache.org/licenses/LICENSE-2.0 -// -// Unless required by applicable law or agreed to in writing, software -// distributed under the License is distributed on an "AS IS" BASIS, -// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -// See the License for the specific language governing permissions and -// limitations under the License. -#endregion - -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading; +#region License +// Copyright 2010 John Sheehan +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. +#endregion + +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading; #if NET4 || MONODROID || MONOTOUCH || WP8 using System.Threading.Tasks; -#endif -using System.Text; -using System.Net; - -using RestSharp.Extensions; - -namespace RestSharp -{ - public partial class RestClient - { - /// - /// Executes the request and callback asynchronously, authenticating if needed - /// - /// Request to be executed +#endif +using System.Text; +using System.Net; + +using RestSharp.Extensions; + +namespace RestSharp +{ + public partial class RestClient + { + /// + /// Executes the request and callback asynchronously, authenticating if needed + /// + /// Request to be executed /// Callback function to be executed upon completion providing access to the async handle. public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action callback) { #if PocketPC - string method = request.Method.ToString(); + string method = request.Method.ToString(); #else string method = Enum.GetName(typeof(Method), request.Method); #endif @@ -52,122 +52,122 @@ public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action< default: return ExecuteAsync(request, callback, method, DoAsGetAsync); } - } - - /// - /// Executes a GET-style request and callback asynchronously, authenticating if needed - /// - /// Request to be executed - /// Callback function to be executed upon completion providing access to the async handle. - /// The HTTP method to execute - public virtual RestRequestAsyncHandle ExecuteAsyncGet(IRestRequest request, Action callback, string httpMethod) - { - return ExecuteAsync(request, callback, httpMethod, DoAsGetAsync); - } - - /// - /// Executes a POST-style request and callback asynchronously, authenticating if needed - /// - /// Request to be executed - /// Callback function to be executed upon completion providing access to the async handle. - /// The HTTP method to execute - public virtual RestRequestAsyncHandle ExecuteAsyncPost(IRestRequest request, Action callback, string httpMethod) - { - request.Method = Method.POST; // Required by RestClient.BuildUri... - return ExecuteAsync(request, callback, httpMethod, DoAsPostAsync); - } - - private RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action callback, string httpMethod, Func, string, HttpWebRequest> getWebRequest) - { - var http = HttpFactory.Create(); - AuthenticateIfNeeded(this, request); - - ConfigureHttp(request, http); - - var asyncHandle = new RestRequestAsyncHandle(); - - Action response_cb = r => ProcessResponse(request, r, asyncHandle, callback); - -#if !PocketPC - if (UseSynchronizationContext && SynchronizationContext.Current != null) - { - var ctx = SynchronizationContext.Current; - var cb = response_cb; - - response_cb = resp => ctx.Post(s => cb(resp), null); - } -#endif - asyncHandle.WebRequest = getWebRequest(http, response_cb, httpMethod); - return asyncHandle; - } - - private static HttpWebRequest DoAsGetAsync(IHttp http, Action response_cb, string method) - { - return http.AsGetAsync(response_cb, method); - } - - private static HttpWebRequest DoAsPostAsync(IHttp http, Action response_cb, string method) - { - return http.AsPostAsync(response_cb, method); - } - - private void ProcessResponse(IRestRequest request, HttpResponse httpResponse, RestRequestAsyncHandle asyncHandle, Action callback) - { - var restResponse = ConvertToRestResponse(request, httpResponse); - callback(restResponse, asyncHandle); - } - - /// - /// Executes the request and callback asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// Callback function to be executed upon completion - public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action, RestRequestAsyncHandle> callback) - { - return ExecuteAsync(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle)); - } - - /// - /// Executes a GET-style request and callback asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// Callback function to be executed upon completion - /// The HTTP method to execute - public virtual RestRequestAsyncHandle ExecuteAsyncGet(IRestRequest request, Action, RestRequestAsyncHandle> callback, string httpMethod) - { - return ExecuteAsyncGet(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle), httpMethod); - } - - /// - /// Executes a POST-style request and callback asynchronously, authenticating if needed - /// - /// Target deserialization type - /// Request to be executed - /// Callback function to be executed upon completion - /// The HTTP method to execute - public virtual RestRequestAsyncHandle ExecuteAsyncPost(IRestRequest request, Action, RestRequestAsyncHandle> callback, string httpMethod) - { - return ExecuteAsyncPost(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle), httpMethod); - } - - private void DeserializeResponse(IRestRequest request, Action, RestRequestAsyncHandle> callback, IRestResponse response, RestRequestAsyncHandle asyncHandle) - { - IRestResponse restResponse; - - try - { - restResponse = Deserialize(request, response); - } - catch (Exception ex) - { - restResponse = new RestResponse { Request = request, ResponseStatus = ResponseStatus.Error, ErrorMessage = ex.Message, ErrorException = ex }; - } - - callback(restResponse, asyncHandle); - } - + } + + /// + /// Executes a GET-style request and callback asynchronously, authenticating if needed + /// + /// Request to be executed + /// Callback function to be executed upon completion providing access to the async handle. + /// The HTTP method to execute + public virtual RestRequestAsyncHandle ExecuteAsyncGet(IRestRequest request, Action callback, string httpMethod) + { + return ExecuteAsync(request, callback, httpMethod, DoAsGetAsync); + } + + /// + /// Executes a POST-style request and callback asynchronously, authenticating if needed + /// + /// Request to be executed + /// Callback function to be executed upon completion providing access to the async handle. + /// The HTTP method to execute + public virtual RestRequestAsyncHandle ExecuteAsyncPost(IRestRequest request, Action callback, string httpMethod) + { + request.Method = Method.POST; // Required by RestClient.BuildUri... + return ExecuteAsync(request, callback, httpMethod, DoAsPostAsync); + } + + private RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action callback, string httpMethod, Func, string, HttpWebRequest> getWebRequest) + { + var http = HttpFactory.Create(); + AuthenticateIfNeeded(this, request); + + ConfigureHttp(request, http); + + var asyncHandle = new RestRequestAsyncHandle(); + + Action response_cb = r => ProcessResponse(request, r, asyncHandle, callback); + +#if !PocketPC + if (UseSynchronizationContext && SynchronizationContext.Current != null) + { + var ctx = SynchronizationContext.Current; + var cb = response_cb; + + response_cb = resp => ctx.Post(s => cb(resp), null); + } +#endif + asyncHandle.WebRequest = getWebRequest(http, response_cb, httpMethod); + return asyncHandle; + } + + private static HttpWebRequest DoAsGetAsync(IHttp http, Action response_cb, string method) + { + return http.AsGetAsync(response_cb, method); + } + + private static HttpWebRequest DoAsPostAsync(IHttp http, Action response_cb, string method) + { + return http.AsPostAsync(response_cb, method); + } + + private void ProcessResponse(IRestRequest request, HttpResponse httpResponse, RestRequestAsyncHandle asyncHandle, Action callback) + { + var restResponse = ConvertToRestResponse(request, httpResponse); + callback(restResponse, asyncHandle); + } + + /// + /// Executes the request and callback asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// Callback function to be executed upon completion + public virtual RestRequestAsyncHandle ExecuteAsync(IRestRequest request, Action, RestRequestAsyncHandle> callback) + { + return ExecuteAsync(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle)); + } + + /// + /// Executes a GET-style request and callback asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// Callback function to be executed upon completion + /// The HTTP method to execute + public virtual RestRequestAsyncHandle ExecuteAsyncGet(IRestRequest request, Action, RestRequestAsyncHandle> callback, string httpMethod) + { + return ExecuteAsyncGet(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle), httpMethod); + } + + /// + /// Executes a POST-style request and callback asynchronously, authenticating if needed + /// + /// Target deserialization type + /// Request to be executed + /// Callback function to be executed upon completion + /// The HTTP method to execute + public virtual RestRequestAsyncHandle ExecuteAsyncPost(IRestRequest request, Action, RestRequestAsyncHandle> callback, string httpMethod) + { + return ExecuteAsyncPost(request, (response, asyncHandle) => DeserializeResponse(request, callback, response, asyncHandle), httpMethod); + } + + private void DeserializeResponse(IRestRequest request, Action, RestRequestAsyncHandle> callback, IRestResponse response, RestRequestAsyncHandle asyncHandle) + { + IRestResponse restResponse; + + try + { + restResponse = Deserialize(request, response); + } + catch (Exception ex) + { + restResponse = new RestResponse { Request = request, ResponseStatus = ResponseStatus.Error, ErrorMessage = ex.Message, ErrorException = ex}; + } + + callback(restResponse, asyncHandle); + } + #if NET4 || MONODROID || MONOTOUCH || WP8 /// /// Executes a GET-style request asynchronously, authenticating if needed @@ -392,6 +392,6 @@ public virtual Task ExecuteTaskAsync(IRestRequest request, Cancel return taskCompletionSource.Task; } -#endif - } +#endif + } } \ No newline at end of file diff --git a/RestSharp/RestClient.Sync.cs b/RestSharp/RestClient.Sync.cs index f4cfe70d4..6a079c519 100644 --- a/RestSharp/RestClient.Sync.cs +++ b/RestSharp/RestClient.Sync.cs @@ -1,32 +1,32 @@ -#if FRAMEWORK || PocketPC -using System; -using System.Collections.Generic; -using System.Linq; -using System.Net; -using System.Text; -using RestSharp.Deserializers; - -namespace RestSharp -{ - public partial class RestClient - { - - - /// - /// Executes the specified request and downloads the response data - /// - /// Request to execute - /// Response data - public byte[] DownloadData(IRestRequest request) - { - var response = Execute(request); - return response.RawBytes; - } - - /// - /// Executes the request and returns a response, authenticating if needed - /// - /// Request to be executed +#if FRAMEWORK || PocketPC +using System; +using System.Collections.Generic; +using System.Linq; +using System.Net; +using System.Text; +using RestSharp.Deserializers; + +namespace RestSharp +{ + public partial class RestClient + { + + + /// + /// Executes the specified request and downloads the response data + /// + /// Request to execute + /// Response data + public byte[] DownloadData(IRestRequest request) + { + var response = Execute(request); + return response.RawBytes; + } + + /// + /// Executes the request and returns a response, authenticating if needed + /// + /// Request to be executed /// RestResponse public virtual IRestResponse Execute(IRestRequest request) { @@ -45,75 +45,75 @@ public virtual IRestResponse Execute(IRestRequest request) default: return Execute(request, method, DoExecuteAsGet); } - } - - public IRestResponse ExecuteAsGet(IRestRequest request, string httpMethod) - { - return Execute(request, httpMethod, DoExecuteAsGet); - } - - public IRestResponse ExecuteAsPost(IRestRequest request, string httpMethod) - { - request.Method = Method.POST; // Required by RestClient.BuildUri... - return Execute(request, httpMethod, DoExecuteAsPost); - } - - private IRestResponse Execute(IRestRequest request, string httpMethod, Func getResponse) - { - AuthenticateIfNeeded(this, request); - - IRestResponse response = new RestResponse(); - try - { - var http = HttpFactory.Create(); - - ConfigureHttp(request, http); - - response = ConvertToRestResponse(request, getResponse(http, httpMethod)); - response.Request = request; - response.Request.IncreaseNumAttempts(); - - } - catch (Exception ex) - { - response.ResponseStatus = ResponseStatus.Error; - response.ErrorMessage = ex.Message; - response.ErrorException = ex; - } - - return response; - } - - private static HttpResponse DoExecuteAsGet(IHttp http, string method) - { - return http.AsGet(method); - } - - private static HttpResponse DoExecuteAsPost(IHttp http, string method) - { - return http.AsPost(method); - } - - /// - /// Executes the specified request and deserializes the response content using the appropriate content handler - /// - /// Target deserialization type - /// Request to execute - /// RestResponse[[T]] with deserialized data in Data property - public virtual IRestResponse Execute(IRestRequest request) where T : new() - { - return Deserialize(request, Execute(request)); - } - - public IRestResponse ExecuteAsGet(IRestRequest request, string httpMethod) where T : new() - { - return Deserialize(request, ExecuteAsGet(request, httpMethod)); - } - - public IRestResponse ExecuteAsPost(IRestRequest request, string httpMethod) where T : new() - { - return Deserialize(request, ExecuteAsPost(request, httpMethod)); - } - } -} + } + + public IRestResponse ExecuteAsGet(IRestRequest request, string httpMethod) + { + return Execute(request, httpMethod, DoExecuteAsGet); + } + + public IRestResponse ExecuteAsPost(IRestRequest request, string httpMethod) + { + request.Method = Method.POST; // Required by RestClient.BuildUri... + return Execute(request, httpMethod, DoExecuteAsPost); + } + + private IRestResponse Execute(IRestRequest request, string httpMethod, Func getResponse) + { + AuthenticateIfNeeded(this, request); + + IRestResponse response = new RestResponse(); + try + { + var http = HttpFactory.Create(); + + ConfigureHttp(request, http); + + response = ConvertToRestResponse(request, getResponse(http, httpMethod)); + response.Request = request; + response.Request.IncreaseNumAttempts(); + + } + catch (Exception ex) + { + response.ResponseStatus = ResponseStatus.Error; + response.ErrorMessage = ex.Message; + response.ErrorException = ex; + } + + return response; + } + + private static HttpResponse DoExecuteAsGet(IHttp http, string method) + { + return http.AsGet(method); + } + + private static HttpResponse DoExecuteAsPost(IHttp http, string method) + { + return http.AsPost(method); + } + + /// + /// Executes the specified request and deserializes the response content using the appropriate content handler + /// + /// Target deserialization type + /// Request to execute + /// RestResponse[[T]] with deserialized data in Data property + public virtual IRestResponse Execute(IRestRequest request) where T : new() + { + return Deserialize(request, Execute(request)); + } + + public IRestResponse ExecuteAsGet(IRestRequest request, string httpMethod) where T : new() + { + return Deserialize(request, ExecuteAsGet(request, httpMethod)); + } + + public IRestResponse ExecuteAsPost(IRestRequest request, string httpMethod) where T : new() + { + return Deserialize(request, ExecuteAsPost(request, httpMethod)); + } + } +} #endif \ No newline at end of file