Skip to content

Commit

Permalink
update SolrPostConnection to use IHttpWebRequestFactory
Browse files Browse the repository at this point in the history
  • Loading branch information
YevhenLukomskyi committed Sep 27, 2019
1 parent 0784483 commit 9fecd12
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 8 deletions.
13 changes: 12 additions & 1 deletion SolrNet.Tests.Common/Mocks/HttpWebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;
using HttpWebAdapters;

namespace SolrNet.Tests.Mocks {
Expand Down Expand Up @@ -207,5 +208,15 @@ public IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state)
public Stream EndGetRequestStream(IAsyncResult result) {
throw new NotImplementedException();
}

public Task<Stream> GetRequestStreamAsync()
{
throw new NotImplementedException();
}

public Task<WebResponse> GetResponseAsync()
{
throw new NotImplementedException();
}
}
}
}
8 changes: 6 additions & 2 deletions SolrNet/HttpWebAdapters/IHttpWebRequest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace HttpWebAdapters {
public interface IHttpWebRequest {
Expand Down Expand Up @@ -454,5 +455,8 @@ public interface IHttpWebRequest {
IHttpWebResponse EndGetResponse(IAsyncResult result);
IAsyncResult BeginGetRequestStream(AsyncCallback callback, object state);
Stream EndGetRequestStream(IAsyncResult result);
}
}

Task<Stream> GetRequestStreamAsync();
Task<WebResponse> GetResponseAsync();
}
}
15 changes: 13 additions & 2 deletions SolrNet/HttpWebAdapters/Impl/HttpWebRequestAdapter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
using System.IO;
using System.Net;
using System.Security.Cryptography.X509Certificates;
using System.Threading.Tasks;

namespace HttpWebAdapters.Adapters {
public class HttpWebRequestAdapter : IHttpWebRequest {
Expand Down Expand Up @@ -52,7 +53,17 @@ public Stream EndGetRequestStream(IAsyncResult result) {
return request.EndGetRequestStream(result);
}

///<summary>
public Task<Stream> GetRequestStreamAsync()
{
return request.GetRequestStreamAsync();
}

public Task<WebResponse> GetResponseAsync()
{
return request.GetResponseAsync();
}

///<summary>
///Gets a <see cref="T:System.IO.Stream"></see> object to use to write request data.
///</summary>
///
Expand Down Expand Up @@ -588,4 +599,4 @@ public DateTime IfModifiedSince {
set { request.IfModifiedSince = value; }
}
}
}
}
13 changes: 10 additions & 3 deletions SolrNet/Impl/SolrPostConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using SolrNet.Utils;
using System.Threading.Tasks;
using System.Threading;
using HttpWebAdapters;

namespace SolrNet.Impl
{
Expand All @@ -19,10 +20,16 @@ public class PostSolrConnection : ISolrConnection
private readonly ISolrConnection conn;
private readonly string serverUrl;

/// <summary>
/// HTTP request factory
/// </summary>
public IHttpWebRequestFactory HttpWebRequestFactory { get; set; }

public PostSolrConnection(ISolrConnection conn, string serverUrl)
{
this.conn = conn;
this.serverUrl = serverUrl;
HttpWebRequestFactory = new HttpWebRequestFactory();
}

/// <summary>
Expand All @@ -43,12 +50,12 @@ public Task<string> PostAsync(string relativeUrl, string s)
return conn.PostAsync(relativeUrl, s);
}

public (HttpWebRequest request, string queryString) PrepareGet(string relativeUrl, IEnumerable<KeyValuePair<string, string>> parameters)
public (IHttpWebRequest request, string queryString) PrepareGet(string relativeUrl, IEnumerable<KeyValuePair<string, string>> parameters)
{
var u = new UriBuilder(serverUrl);
u.Path += relativeUrl;
var request = (HttpWebRequest)WebRequest.Create(u.Uri);
request.Method = "POST";
var request = HttpWebRequestFactory.Create(u.Uri);
request.Method = HttpWebRequestMethod.POST;
request.ContentType = "application/x-www-form-urlencoded";

var param = new List<KeyValuePair<string, string>>();
Expand Down

0 comments on commit 9fecd12

Please sign in to comment.