using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using RestSharp; using System.Net; namespace Couchbase { /// /// Default implementation of the using the Hammock REST library. /// internal class RestSharpHttpClient : IHttpClient { private static readonly Enyim.Caching.ILog log = Enyim.Caching.LogManager.GetLogger(typeof(HammockHttpClient)); private RestClient client; public RestSharpHttpClient(Uri baseUri, string username, string password, TimeSpan timeout, bool shouldInitConnection) { this.client = new RestClient(baseUri.ToString()); client.Timeout = timeout.Milliseconds; } IHttpRequest IHttpClient.CreateRequest(string path) { return new RestSharpRequestWrapper(this.client, path); } public int RetryCount { get; set; } #region [ RestSharpRequestWrapper ] private class RestSharpRequestWrapper : IHttpRequest { private RestRequest request; private RestClient client; private HttpMethod method; public RestSharpRequestWrapper(RestClient client, string path) { this.client = client; this.method = HttpMethod.Get; this.request = new RestRequest(path); request.AddHeader("Accept", "application/json"); request.AddHeader("Content-Type", "application/json; charset=utf-8"); } void IHttpRequest.AddParameter(string name, string value) { this.request.AddParameter(name, value); } IHttpResponse IHttpRequest.GetResponse() { switch (this.method) { case HttpMethod.Delete: this.request.Method = Method.DELETE; break; case HttpMethod.Get: this.request.Method = Method.GET; break; case HttpMethod.Head: this.request.Method = Method.HEAD; break; case HttpMethod.Options: this.request.Method = Method.OPTIONS; break; case HttpMethod.Post: this.request.Method = Method.POST; break; case HttpMethod.Put: this.request.Method = Method.PUT; break; default: throw new ArgumentOutOfRangeException("method: " + this.method); } var r = new RestSharpResponseWrapper(request); r.ExecuteWith(this.client); return r; } HttpMethod IHttpRequest.Method { get { return this.method; } set { this.method = value; } } } #endregion #region [ RestSharpResponseWrapper ] private class RestSharpResponseWrapper : IHttpResponse { private IRestRequest request; private IRestResponse response; public RestSharpResponseWrapper(RestRequest request) { this.request = request; } public void ExecuteWith(RestClient client) { this.response = client.Execute(request); if (response.ErrorException != null) throw response.ErrorException; if (response.StatusCode != System.Net.HttpStatusCode.OK) throw new InvalidOperationException(String.Format("Server returned {0}: {1}, {2}", response.StatusCode, response.StatusDescription, response.Content)); } Stream IHttpResponse.GetResponseStream() { return new MemoryStream(this.response.RawBytes); } } #endregion } /// /// Creates instances of the Hammock implementation of . /// public class RestSharpHttpClientFactory : IHttpClientFactory { public static readonly IHttpClientFactory Instance = new RestSharpHttpClientFactory(); IHttpClient IHttpClientFactory.Create(Uri baseUri, string username, string password, TimeSpan timeout, bool shouldInitializeConnection) { return new RestSharpHttpClient(baseUri, username, password, timeout, shouldInitializeConnection); } } }