Skip to content

Commit

Permalink
Configure BearerTokenConnection timeout #102
Browse files Browse the repository at this point in the history
  • Loading branch information
maartenba committed Jan 25, 2021
1 parent b1ff796 commit cf58dc0
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion src/YouTrackSharp/BearerTokenConnection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using JetBrains.Annotations;
using YouTrackSharp.Internal;

namespace YouTrackSharp
Expand All @@ -10,6 +11,7 @@ namespace YouTrackSharp
/// A class that represents a connection against a YouTrack server instance and provides an authenticated
/// <see cref="T:System.Net.Http.HttpClient" /> that uses a bearer token.
/// </summary>
[PublicAPI]
public class BearerTokenConnection
: Connection
{
Expand All @@ -19,6 +21,8 @@ public class BearerTokenConnection
private readonly string _bearerToken;

private readonly Action<HttpClientHandler> _configureHandler;

private TimeSpan _timeout = TimeSpan.FromSeconds(100);

/// <summary>
/// Creates an instance of the <see cref="BearerTokenConnection" /> class.
Expand All @@ -35,6 +39,25 @@ public BearerTokenConnection(string serverUrl, string bearerToken, Action<HttpCl
_bearerToken = bearerToken;
_configureHandler = configureHandler;
}

/// <summary>
/// Gets or sets the timespan to wait before the request times out.
/// </summary>
/// <remarks>
/// The default value is 100,000 milliseconds (100 seconds).
/// </remarks>
public TimeSpan Timeout
{
get => _httpClient?.Timeout ?? _timeout;
set
{
_timeout = value;
if (_httpClient != null)
{
_httpClient.Timeout = _timeout;
}
}
}

/// <inheritdoc />
public override async Task<HttpClient> GetAuthenticatedHttpClient()
Expand All @@ -48,7 +71,8 @@ public override async Task<HttpClient> GetAuthenticatedHttpClient()

_httpClient = new HttpClient(handler)
{
BaseAddress = ServerUri
BaseAddress = ServerUri,
Timeout = _timeout
};

_httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(Constants.HttpContentTypes.ApplicationJson));
Expand Down

0 comments on commit cf58dc0

Please sign in to comment.