Skip to content

Commit

Permalink
Allows WikiClient to be constructed with custom HttpMessageHandler.
Browse files Browse the repository at this point in the history
This gives the freedom on cookies, authentication, and more.
  • Loading branch information
CXuesong committed Mar 7, 2017
1 parent b906366 commit 2ab4683
Showing 1 changed file with 26 additions and 26 deletions.
52 changes: 26 additions & 26 deletions WikiClientLibrary/Client/WikiClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -79,28 +79,16 @@ public int MaxRetries
/// <para>To persist user's login information, you can persist the value of this property.</para>
/// <para>You can use the same CookieContainer with different <see cref="WikiClient"/>s.</para>
/// </remarks>
/// <exception cref="NotSupportedException">You have initialized this Client with a HttpMessageHandler that is not a HttpClientHandler.</exception>
public CookieContainer CookieContainer
{
get { return _HttpClientHandler.CookieContainer; }
set { _HttpClientHandler.CookieContainer = value; }
}

/// <summary>
/// Gets/sets authentication information used by this client.
/// </summary>
public ICredentials Credentials
{
get { return _HttpClientHandler.Credentials; }
set { _HttpClientHandler.Credentials = value; }
}

/// <summary>
/// Gets/sets a value that controls whether default credentials are sent with requests by the client.
/// </summary>
public bool UseDefaultCredentials
{
get { return _HttpClientHandler.UseDefaultCredentials; }
set { _HttpClientHandler.UseDefaultCredentials = value; }
set
{
if (_HttpClientHandler == null)
throw new NotSupportedException("Not supported when working with a HttpMessageHandler that is not a HttpClientHandler.");
_HttpClientHandler.CookieContainer = value;
}
}

public ILogger Logger { get; set; }
Expand Down Expand Up @@ -178,14 +166,9 @@ public Task<JToken> GetJsonAsync(string endPointUrl, object queryParams, Cancell
return GetJsonAsync(endPointUrl, Utility.ToWikiStringValuePairs(queryParams), cancellationToken);
}

public WikiClient()
public WikiClient() : this(new HttpClientHandler(), true)
{
_HttpClientHandler = new HttpClientHandler
{
UseCookies = true
};
HttpClient = new HttpClient(_HttpClientHandler, true);
ClientUserAgent = null;
_HttpClientHandler.UseCookies = true;
// https://www.mediawiki.org/wiki/API:Client_code
// Please use GZip compression when making API calls (Accept-Encoding: gzip).
// Bots eat up a lot of bandwidth, which is not free.
Expand All @@ -195,6 +178,23 @@ public WikiClient()
}
}

/// <param name="handler">The HttpMessageHandler responsible for processing the HTTP response messages.</param>
/// <exception cref="ArgumentNullException"><paramref name="handler"/> is <c>null</c>.</exception>
public WikiClient(HttpMessageHandler handler) : this(handler, true)
{
}

/// <param name="handler">The HttpMessageHandler responsible for processing the HTTP response messages.</param>
/// <param name="disposeHandler">Whether to automatically dispose the handler when disposing this Client.</param>
/// <exception cref="ArgumentNullException"><paramref name="handler"/> is <c>null</c>.</exception>
public WikiClient(HttpMessageHandler handler, bool disposeHandler)
{
if (handler == null) throw new ArgumentNullException(nameof(handler));
HttpClient = new HttpClient(handler, disposeHandler);
ClientUserAgent = null;
_HttpClientHandler = handler as HttpClientHandler;
}

/// <summary>
/// 返回表示当前对象的字符串。
/// </summary>
Expand Down

0 comments on commit 2ab4683

Please sign in to comment.