1- using Octokit ;
2- using UniGetUI . Core . Data ;
31using UniGetUI . Core . SecureSettings ;
42using UniGetUI . Core . Tools ;
3+ using UniGetUI . Interface ;
54
65namespace UniGetUI . Avalonia . Infrastructure ;
76
@@ -20,56 +19,48 @@ internal sealed class CloudBackupEntry
2019 public required string Display { get ; init ; }
2120 }
2221
23- public static GitHubClient ? CreateGitHubClient ( )
22+ public static GitHubApiClient ? CreateGitHubClient ( )
2423 {
2524 string ? token = SecureGHTokenManager . GetToken ( ) ;
2625 if ( string . IsNullOrWhiteSpace ( token ) )
2726 return null ;
2827
29- return new GitHubClient ( new ProductHeaderValue ( "UniGetUI" , CoreData . VersionName ) )
30- {
31- Credentials = new Credentials ( token ) ,
32- } ;
28+ return new GitHubApiClient ( token ) ;
3329 }
3430
3531 public static async Task < ( string Login , string DisplayName ) > GetCurrentUserAsync ( )
3632 {
37- var client = CreateGitHubClient ( )
33+ using var client = CreateGitHubClient ( )
3834 ?? throw new InvalidOperationException ( CoreTools . Translate ( "Log in to enable cloud backup" ) ) ;
3935
40- var user = await client . User . Current ( ) ;
36+ var user = await client . GetCurrentUserAsync ( ) ;
4137 string login = user . Login ?? string . Empty ;
4238 string displayName = string . IsNullOrWhiteSpace ( user . Name ) ? login : user . Name ;
4339 return ( login , displayName ) ;
4440 }
4541
4642 public static async Task UploadPackageBundleAsync ( string bundleContents )
4743 {
48- var client = CreateGitHubClient ( )
44+ using var client = CreateGitHubClient ( )
4945 ?? throw new InvalidOperationException ( CoreTools . Translate ( "Log in to enable cloud backup" ) ) ;
5046
51- var user = await client . User . Current ( ) ;
52- var backupGist = await GetBackupGistAsync ( client , user . Login , createIfMissing : true )
47+ var backupGist = await GetBackupGistAsync ( client , createIfMissing : true )
5348 ?? throw new InvalidOperationException ( CoreTools . Translate ( "Backup Failed" ) ) ;
5449
5550 string fileKey = BuildGistFileKey ( ) ;
56- var update = new GistUpdate { Description = GistDescription } ;
57-
58- if ( backupGist . Files . ContainsKey ( fileKey ) )
59- update . Files [ fileKey ] = new GistFileUpdate { Content = bundleContents } ;
60- else
61- update . Files . Add ( fileKey , new GistFileUpdate { Content = bundleContents } ) ;
62-
63- await client . Gist . Edit ( backupGist . Id , update ) ;
51+ await client . EditGistAsync (
52+ backupGist . Id ,
53+ GistDescription ,
54+ new Dictionary < string , string > { [ fileKey ] = bundleContents }
55+ ) ;
6456 }
6557
6658 public static async Task < IReadOnlyList < CloudBackupEntry > > GetAvailableBackupsAsync ( )
6759 {
68- var client = CreateGitHubClient ( )
60+ using var client = CreateGitHubClient ( )
6961 ?? throw new InvalidOperationException ( CoreTools . Translate ( "Log in to enable cloud backup" ) ) ;
7062
71- var user = await client . User . Current ( ) ;
72- var backupGist = await GetBackupGistAsync ( client , user . Login , createIfMissing : false ) ;
63+ var backupGist = await GetBackupGistAsync ( client , createIfMissing : false ) ;
7364 if ( backupGist is null )
7465 return [ ] ;
7566
@@ -85,16 +76,15 @@ public static async Task<IReadOnlyList<CloudBackupEntry>> GetAvailableBackupsAsy
8576
8677 public static async Task < string > GetBackupContentsAsync ( string backupKey )
8778 {
88- var client = CreateGitHubClient ( )
79+ using var client = CreateGitHubClient ( )
8980 ?? throw new InvalidOperationException ( CoreTools . Translate ( "Log in to enable cloud backup" ) ) ;
9081
91- var user = await client . User . Current ( ) ;
92- var backupGist = await GetBackupGistAsync ( client , user . Login , createIfMissing : false ) ;
82+ var backupGist = await GetBackupGistAsync ( client , createIfMissing : false ) ;
9383
9484 if ( backupGist is null )
9585 throw new KeyNotFoundException ( CoreTools . Translate ( "Log in to enable cloud backup" ) ) ;
9686
97- var fullGist = await client . Gist . Get ( backupGist . Id ) ;
87+ var fullGist = await client . GetGistAsync ( backupGist . Id ) ;
9888 var file = fullGist . Files . FirstOrDefault ( f =>
9989 f . Key . StartsWith ( PackageBackupStartingKey , StringComparison . Ordinal )
10090 && f . Key . EndsWith ( backupKey , StringComparison . Ordinal ) ) ;
@@ -105,18 +95,23 @@ public static async Task<string> GetBackupContentsAsync(string backupKey)
10595 return file . Value . Content ;
10696 }
10797
108- private static async Task < Gist ? > GetBackupGistAsync ( GitHubClient client , string userLogin , bool createIfMissing )
98+ private static async Task < GitHubGist ? > GetBackupGistAsync (
99+ GitHubApiClient client ,
100+ bool createIfMissing
101+ )
109102 {
110- var candidates = await client . Gist . GetAllForUser ( userLogin ) ;
103+ var candidates = await client . GetCurrentUserGistsAsync ( ) ;
111104 var backupGist = candidates . FirstOrDefault ( g =>
112105 g . Description ? . EndsWith ( GistDescriptionEndingKey , StringComparison . Ordinal ) == true ) ;
113106
114107 if ( backupGist is not null || ! createIfMissing )
115108 return backupGist ;
116109
117- var newGist = new NewGist { Description = GistDescription , Public = false } ;
118- newGist . Files . Add ( "- UniGetUI Package Backups" , ReadMeContents ) ;
119- return await client . Gist . Create ( newGist ) ;
110+ return await client . CreateGistAsync (
111+ GistDescription ,
112+ isPublic : false ,
113+ new Dictionary < string , string > { [ "- UniGetUI Package Backups" ] = ReadMeContents }
114+ ) ;
120115 }
121116
122117 private static string BuildGistFileKey ( )
0 commit comments