1
1
using System ;
2
+ using System . Collections . Generic ;
3
+ using System . Linq ;
2
4
using System . Threading ;
3
5
using System . Threading . Tasks ;
4
6
using Octokit ;
@@ -9,6 +11,8 @@ class ApiClient : IApiClient
9
11
{
10
12
public static IApiClient Create ( UriString repositoryUrl , IKeychain keychain )
11
13
{
14
+ logger . Trace ( "Creating ApiClient: {0}" , repositoryUrl ) ;
15
+
12
16
var credentialStore = keychain . Connect ( repositoryUrl ) ;
13
17
var hostAddress = HostAddress . Create ( repositoryUrl ) ;
14
18
@@ -26,6 +30,9 @@ public static IApiClient Create(UriString repositoryUrl, IKeychain keychain)
26
30
private static readonly SemaphoreSlim sem = new SemaphoreSlim ( 1 ) ;
27
31
28
32
Octokit . Repository repositoryCache = new Octokit . Repository ( ) ;
33
+ IList < Organization > organizationsCache ;
34
+ Octokit . User userCache ;
35
+
29
36
string owner ;
30
37
bool ? isEnterprise ;
31
38
@@ -42,14 +49,14 @@ public ApiClient(UriString hostUrl, IKeychain keychain, IGitHubClient githubClie
42
49
loginManager = new LoginManager ( keychain , ApplicationInfo . ClientId , ApplicationInfo . ClientSecret ) ;
43
50
}
44
51
45
- public async void GetRepository ( Action < Octokit . Repository > callback )
52
+ public async Task GetRepository ( Action < Octokit . Repository > callback )
46
53
{
47
54
Guard . ArgumentNotNull ( callback , "callback" ) ;
48
55
var repo = await GetRepositoryInternal ( ) ;
49
56
callback ( repo ) ;
50
57
}
51
58
52
- public async void Logout ( UriString host )
59
+ public async Task Logout ( UriString host )
53
60
{
54
61
await LogoutInternal ( host ) ;
55
62
}
@@ -59,6 +66,26 @@ private async Task LogoutInternal(UriString host)
59
66
await loginManager . Logout ( host ) ;
60
67
}
61
68
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
+
62
89
public async Task Login ( string username , string password , Action < LoginResult > need2faCode , Action < bool , string > result )
63
90
{
64
91
Guard . ArgumentNotNull ( need2faCode , "need2faCode" ) ;
@@ -190,6 +217,74 @@ public async Task<bool> ContinueLoginAsync(LoginResult loginResult, Func<LoginRe
190
217
return repositoryCache ;
191
218
}
192
219
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
+
193
288
public async Task < bool > ValidateCredentials ( )
194
289
{
195
290
try
0 commit comments