1- using System . Text . Json ;
2-
3- using Acmebot . Acme ;
1+ using Acmebot . Acme ;
42using Acmebot . Acme . Models ;
53using Acmebot . App . Infrastructure ;
64using Acmebot . App . Options ;
97
108namespace Acmebot . App . Acme ;
119
12- public class AcmeClientFactory ( IOptions < AcmebotOptions > options )
10+ public class AcmeClientFactory ( IOptions < AcmebotOptions > options , IAcmeStateStore stateStore )
1311{
1412 private readonly AcmebotOptions _options = options . Value ;
1513
16- private static readonly JsonSerializerOptions s_jsonSerializerOptions = new ( )
17- {
18- PropertyNameCaseInsensitive = true ,
19- WriteIndented = true
20- } ;
21-
2214 public async Task < AcmeClientContext > CreateClientAsync ( )
2315 {
24- var account = LoadState < AccountDetails > ( "account.json" ) ;
25- var accountKey = LoadState < AccountKey > ( "account_key.json" ) ;
16+ var account = await stateStore . LoadAsync < AccountDetails > ( "account.json" ) ;
17+ var accountKey = await stateStore . LoadAsync < AccountKey > ( "account_key.json" ) ;
2618 var contacts = GetContacts ( ) ;
2719 var isNewAccountKey = false ;
2820
@@ -66,12 +58,12 @@ public async Task<AcmeClientContext> CreateClientAsync()
6658 externalAccountBinding ) ;
6759 account = AccountDetails . FromAccountHandle ( accountHandle , directory . Metadata ? . TermsOfService ) ;
6860
69- SaveState ( account , "account.json" ) ;
70-
7161 if ( isNewAccountKey )
7262 {
73- SaveState ( accountKey , "account_key.json" ) ;
63+ await stateStore . SaveAsync ( accountKey , "account_key.json" ) ;
7464 }
65+
66+ await stateStore . SaveAsync ( account , "account.json" ) ;
7567 }
7668 else
7769 {
@@ -88,7 +80,7 @@ public async Task<AcmeClientContext> CreateClientAsync()
8880 } ) ;
8981 account = AccountDetails . FromAccountHandle ( accountHandle , directory . Metadata ? . TermsOfService ) ;
9082
91- SaveState ( account , "account.json" ) ;
83+ await stateStore . SaveAsync ( account , "account.json" ) ;
9284 }
9385
9486 return new AcmeClientContext
@@ -117,35 +109,4 @@ public async Task<AcmeClientContext> CreateClientAsync()
117109
118110 private static bool ContactsEqual ( IReadOnlyList < string > ? actualContacts , IReadOnlyList < string > expectedContacts )
119111 => actualContacts is not null && actualContacts . SequenceEqual ( expectedContacts , StringComparer . Ordinal ) ;
120-
121- private TState ? LoadState < TState > ( string path )
122- {
123- var fullPath = ResolveStateFullPath ( path ) ;
124-
125- if ( ! File . Exists ( fullPath ) )
126- {
127- return default ;
128- }
129-
130- var json = File . ReadAllText ( fullPath ) ;
131-
132- return JsonSerializer . Deserialize < TState > ( json , s_jsonSerializerOptions ) ;
133- }
134-
135- private void SaveState < TState > ( TState value , string path )
136- {
137- var fullPath = ResolveStateFullPath ( path ) ;
138- var directoryPath = Path . GetDirectoryName ( fullPath ) ;
139-
140- if ( ! string . IsNullOrEmpty ( directoryPath ) && ! Directory . Exists ( directoryPath ) )
141- {
142- Directory . CreateDirectory ( directoryPath ) ;
143- }
144-
145- var json = JsonSerializer . Serialize ( value , s_jsonSerializerOptions ) ;
146-
147- File . WriteAllText ( fullPath , json ) ;
148- }
149-
150- private string ResolveStateFullPath ( string path ) => Environment . ExpandEnvironmentVariables ( $ "%HOME%/data/.acmebot/{ _options . Endpoint . Host } /{ path } ") ;
151112}
0 commit comments