11using System ;
2+ using System . Collections . Generic ;
3+ using System . Linq ;
24using System . Threading ;
35using System . Threading . Tasks ;
46using Octokit ;
@@ -9,6 +11,8 @@ class ApiClient : IApiClient
911 {
1012 public static IApiClient Create ( UriString repositoryUrl , IKeychain keychain )
1113 {
14+ logger . Trace ( "Creating ApiClient: {0}" , repositoryUrl ) ;
15+
1216 var credentialStore = keychain . Connect ( repositoryUrl ) ;
1317 var hostAddress = HostAddress . Create ( repositoryUrl ) ;
1418
@@ -26,6 +30,9 @@ public static IApiClient Create(UriString repositoryUrl, IKeychain keychain)
2630 private static readonly SemaphoreSlim sem = new SemaphoreSlim ( 1 ) ;
2731
2832 Octokit . Repository repositoryCache = new Octokit . Repository ( ) ;
33+ IList < Organization > organizationsCache ;
34+ Octokit . User userCache ;
35+
2936 string owner ;
3037 bool ? isEnterprise ;
3138
@@ -42,14 +49,14 @@ public ApiClient(UriString hostUrl, IKeychain keychain, IGitHubClient githubClie
4249 loginManager = new LoginManager ( keychain , ApplicationInfo . ClientId , ApplicationInfo . ClientSecret ) ;
4350 }
4451
45- public async void GetRepository ( Action < Octokit . Repository > callback )
52+ public async Task GetRepository ( Action < Octokit . Repository > callback )
4653 {
4754 Guard . ArgumentNotNull ( callback , "callback" ) ;
4855 var repo = await GetRepositoryInternal ( ) ;
4956 callback ( repo ) ;
5057 }
5158
52- public async void Logout ( UriString host )
59+ public async Task Logout ( UriString host )
5360 {
5461 await LogoutInternal ( host ) ;
5562 }
@@ -59,6 +66,26 @@ private async Task LogoutInternal(UriString host)
5966 await loginManager . Logout ( host ) ;
6067 }
6168
69+ public async Task CreateRepository ( NewRepository newRepository , Action < Octokit . Repository , Exception > callback , string organization = null )
70+ {
71+ Guard . ArgumentNotNull ( callback , "callback" ) ;
72+ await CreateRepositoryInternal ( newRepository , callback , organization ) ;
73+ }
74+
75+ public async Task GetOrganizations ( Action < IList < Organization > > callback )
76+ {
77+ Guard . ArgumentNotNull ( callback , "callback" ) ;
78+ var organizations = await GetOrganizationInternal ( ) ;
79+ callback ( organizations ) ;
80+ }
81+
82+ public async Task GetCurrentUser ( Action < Octokit . User > callback )
83+ {
84+ Guard . ArgumentNotNull ( callback , "callback" ) ;
85+ var user = await GetCurrentUserInternal ( ) ;
86+ callback ( user ) ;
87+ }
88+
6289 public async Task Login ( string username , string password , Action < LoginResult > need2faCode , Action < bool , string > result )
6390 {
6491 Guard . ArgumentNotNull ( need2faCode , "need2faCode" ) ;
@@ -190,6 +217,74 @@ public async Task<bool> ContinueLoginAsync(LoginResult loginResult, Func<LoginRe
190217 return repositoryCache ;
191218 }
192219
220+ private async Task CreateRepositoryInternal ( NewRepository newRepository , Action < Octokit . Repository , Exception > callback , string organization )
221+ {
222+ try
223+ {
224+ logger . Trace ( "Creating Repository" ) ;
225+
226+ Octokit . Repository repository ;
227+ if ( organization != null )
228+ {
229+ repository = await githubClient . Repository . Create ( organization , newRepository ) ;
230+ }
231+ else
232+ {
233+ repository = await githubClient . Repository . Create ( newRepository ) ;
234+ }
235+
236+ logger . Trace ( "Created Repository" ) ;
237+
238+ callback ( repository , null ) ;
239+ }
240+ catch ( Exception ex )
241+ {
242+ logger . Error ( ex , "Error Creating Repository" ) ;
243+ callback ( null , ex ) ;
244+ }
245+ }
246+
247+ private async Task < IList < Organization > > GetOrganizationInternal ( )
248+ {
249+ try
250+ {
251+ logger . Trace ( "Getting Organizations" ) ;
252+
253+ var organizations = await githubClient . Organization . GetAllForCurrent ( ) ;
254+
255+ logger . Trace ( "Obtained {0} Organizations" , organizations ? . Count . ToString ( ) ?? "NULL" ) ;
256+
257+ if ( organizations != null )
258+ {
259+ organizationsCache = organizations . ToArray ( ) ;
260+ }
261+ }
262+ catch ( Exception ex )
263+ {
264+ logger . Error ( ex , "Error Getting Organizations" ) ;
265+ throw ;
266+ }
267+
268+ return organizationsCache ;
269+ }
270+
271+ private async Task < Octokit . User > GetCurrentUserInternal ( )
272+ {
273+ try
274+ {
275+ logger . Trace ( "Getting Organizations" ) ;
276+
277+ userCache = await githubClient . User . Current ( ) ;
278+ }
279+ catch ( Exception ex )
280+ {
281+ logger . Error ( ex , "Error Getting Current User" ) ;
282+ throw ;
283+ }
284+
285+ return userCache ;
286+ }
287+
193288 public async Task < bool > ValidateCredentials ( )
194289 {
195290 try
0 commit comments