This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Refactoring ConnectionManager/RepositoryHost(s) #1280
Merged
shana
merged 7 commits into
refactor/connections-master
from
refactor/connection-manager
Nov 9, 2017
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
d98ec51
Refactored ConnectionManager/Connection.
grokys aa78f03
Refactored RepositoryHost(s).
grokys 7cf89c9
Added DebuggerDisplay back in.
grokys 90c35c5
Track login metrics.
grokys 9db05a7
Fix failing tests.
grokys 217e415
Merge branch 'refactor/connections/local-repositories' into refactor/…
grokys 32f073a
Merge branch 'refactor/connections/local-repositories' into refactor/…
grokys File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,13 @@ | ||
using System; | ||
using System.Diagnostics; | ||
using System.Diagnostics.CodeAnalysis; | ||
using System.Globalization; | ||
using System.Linq; | ||
using System.Reactive; | ||
using System.Reactive.Linq; | ||
using System.Reactive.Threading.Tasks; | ||
using System.Threading.Tasks; | ||
using GitHub.Api; | ||
using GitHub.Authentication; | ||
using GitHub.Caches; | ||
using GitHub.Extensions; | ||
using GitHub.Logging; | ||
using GitHub.Primitives; | ||
using GitHub.Services; | ||
using Octokit; | ||
using ReactiveUI; | ||
using Serilog; | ||
using static System.FormattableString; | ||
|
||
namespace GitHub.Models | ||
{ | ||
|
@@ -25,145 +16,28 @@ public class RepositoryHost : ReactiveObject, IRepositoryHost | |
{ | ||
static readonly ILogger log = LogManager.ForContext<RepositoryHosts>(); | ||
|
||
readonly ILoginManager loginManager; | ||
readonly HostAddress hostAddress; | ||
readonly IKeychain keychain; | ||
readonly IUsageTracker usage; | ||
|
||
bool isLoggedIn; | ||
readonly IConnection connection; | ||
|
||
public RepositoryHost( | ||
IConnection connection, | ||
IApiClient apiClient, | ||
IModelService modelService, | ||
ILoginManager loginManager, | ||
IKeychain keychain, | ||
IUsageTracker usage) | ||
IModelService modelService) | ||
{ | ||
Guard.ArgumentNotNull(connection, nameof(connection)); | ||
Guard.ArgumentNotNull(apiClient, nameof(apiClient)); | ||
Guard.ArgumentNotNull(modelService, nameof(modelService)); | ||
|
||
this.connection = connection; | ||
ApiClient = apiClient; | ||
ModelService = modelService; | ||
this.loginManager = loginManager; | ||
this.keychain = keychain; | ||
this.usage = usage; | ||
|
||
log.Assert(apiClient.HostAddress != null, "HostAddress of an api client shouldn't be null"); | ||
Address = apiClient.HostAddress; | ||
hostAddress = apiClient.HostAddress; | ||
Title = apiClient.HostAddress.Title; | ||
} | ||
|
||
public HostAddress Address { get; private set; } | ||
|
||
public IApiClient ApiClient { get; private set; } | ||
|
||
public bool IsLoggedIn | ||
{ | ||
get { return isLoggedIn; } | ||
private set { this.RaiseAndSetIfChanged(ref isLoggedIn, value); } | ||
} | ||
|
||
public string Title { get; private set; } | ||
|
||
[SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")] | ||
public IObservable<AuthenticationResult> LogInFromCache() | ||
{ | ||
Func<Task<AuthenticationResult>> f = async () => | ||
{ | ||
try | ||
{ | ||
var user = await loginManager.LoginFromCache(Address, ApiClient.GitHubClient); | ||
var accountCacheItem = new AccountCacheItem(user); | ||
usage.IncrementLoginCount().Forget(); | ||
await ModelService.InsertUser(accountCacheItem); | ||
|
||
if (user != null) | ||
{ | ||
IsLoggedIn = true; | ||
return AuthenticationResult.Success; | ||
} | ||
else | ||
{ | ||
return AuthenticationResult.VerificationFailure; | ||
} | ||
} | ||
catch (AuthorizationException) | ||
{ | ||
return AuthenticationResult.CredentialFailure; | ||
} | ||
}; | ||
|
||
return f().ToObservable(); | ||
} | ||
|
||
public IObservable<AuthenticationResult> LogIn(string usernameOrEmail, string password) | ||
{ | ||
Guard.ArgumentNotEmptyString(usernameOrEmail, nameof(usernameOrEmail)); | ||
Guard.ArgumentNotEmptyString(password, nameof(password)); | ||
|
||
return Observable.Defer(async () => | ||
{ | ||
var user = await loginManager.Login(Address, ApiClient.GitHubClient, usernameOrEmail, password); | ||
var accountCacheItem = new AccountCacheItem(user); | ||
usage.IncrementLoginCount().Forget(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. TODO: Need to put this in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done this now in |
||
await ModelService.InsertUser(accountCacheItem); | ||
|
||
if (user != null) | ||
{ | ||
IsLoggedIn = true; | ||
return Observable.Return(AuthenticationResult.Success); | ||
} | ||
else | ||
{ | ||
return Observable.Return(AuthenticationResult.VerificationFailure); | ||
} | ||
}); | ||
} | ||
|
||
public IObservable<Unit> LogOut() | ||
{ | ||
if (!IsLoggedIn) return Observable.Return(Unit.Default); | ||
|
||
log.Information("Logged off of host '{ApiUri}'", hostAddress.ApiUri); | ||
|
||
return keychain.Delete(Address).ToObservable() | ||
.Catch<Unit, Exception>(e => | ||
{ | ||
log.Warning(e, "ASSERT! Failed to erase login. Going to invalidate cache anyways"); | ||
return Observable.Return(Unit.Default); | ||
}) | ||
.SelectMany(_ => ModelService.InvalidateAll()) | ||
.Catch<Unit, Exception>(e => | ||
{ | ||
log.Warning(e, "ASSERT! Failed to invaldiate caches"); | ||
return Observable.Return(Unit.Default); | ||
}) | ||
.ObserveOn(RxApp.MainThreadScheduler) | ||
.Finally(() => | ||
{ | ||
IsLoggedIn = false; | ||
}); | ||
} | ||
|
||
protected virtual void Dispose(bool disposing) | ||
{} | ||
public HostAddress Address => ApiClient.HostAddress; | ||
public IApiClient ApiClient { get;} | ||
public bool IsLoggedIn => connection.IsLoggedIn; | ||
public IModelService ModelService { get; } | ||
public string Title => ApiClient.HostAddress.Title; | ||
|
||
public void Dispose() | ||
{ | ||
Dispose(true); | ||
GC.SuppressFinalize(this); | ||
} | ||
|
||
internal string DebuggerDisplay | ||
{ | ||
get | ||
{ | ||
return string.Format(CultureInfo.InvariantCulture, "RepositoryHost: {0} {1}", Title, hostAddress.ApiUri); | ||
} | ||
} | ||
|
||
public IModelService ModelService | ||
{ | ||
get; | ||
private set; | ||
} | ||
internal string DebuggerDisplay => Invariant($"RepositoryHost: {Title} {Address.ApiUri}"); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
TODO: Need to work out if a login from cache counts as a login, and if so put this in `ConnectionManager.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done this now in
ConnectionManager
.