@@ -66,6 +66,58 @@ public static HttpClientInterceptorOptions RegisterBundle(
6666
6767 var bundle = BundleFactory . Create ( path ) ;
6868
69+ return options . RegisterBundle ( bundle , templateValues ) ;
70+ }
71+
72+ /// <summary>
73+ /// Registers a bundle of HTTP request interceptions from a specified JSON file.
74+ /// </summary>
75+ /// <param name="options">The <see cref="HttpClientInterceptorOptions"/> to register the bundle with.</param>
76+ /// <param name="path">The path of the JSON file containing the serialized bundle.</param>
77+ /// <param name="templateValues">The optional template values to specify.</param>
78+ /// <param name="cancellationToken">The optional <see cref="CancellationToken"/> to use.</param>
79+ /// <returns>
80+ /// The value specified by <paramref name="options"/>.
81+ /// </returns>
82+ /// <exception cref="ArgumentNullException">
83+ /// <paramref name="options"/>, <paramref name="path"/> or <paramref name="templateValues"/> is <see langword="null"/>.
84+ /// </exception>
85+ /// <exception cref="NotSupportedException">
86+ /// The version of the serialized bundle is not supported.
87+ /// </exception>
88+ public static async Task < HttpClientInterceptorOptions > RegisterBundleAsync (
89+ this HttpClientInterceptorOptions options ,
90+ string path ,
91+ IEnumerable < KeyValuePair < string , string > > ? templateValues = default ,
92+ CancellationToken cancellationToken = default )
93+ {
94+ if ( options is null )
95+ {
96+ throw new ArgumentNullException ( nameof ( options ) ) ;
97+ }
98+
99+ if ( path is null )
100+ {
101+ throw new ArgumentNullException ( nameof ( path ) ) ;
102+ }
103+
104+ templateValues ??= Array . Empty < KeyValuePair < string , string > > ( ) ;
105+
106+ var bundle = await BundleFactory . CreateAsync ( path , cancellationToken ) . ConfigureAwait ( false ) ;
107+
108+ return options . RegisterBundle ( bundle ! , templateValues ) ;
109+ }
110+
111+ private static HttpClientInterceptorOptions RegisterBundle (
112+ this HttpClientInterceptorOptions options ,
113+ Bundle ? bundle ,
114+ IEnumerable < KeyValuePair < string , string > > templateValues )
115+ {
116+ if ( bundle is null )
117+ {
118+ throw new InvalidOperationException ( "No HTTP request interception bundle was deserialized." ) ;
119+ }
120+
69121 if ( bundle . Version != 1 )
70122 {
71123 throw new NotSupportedException ( $ "HTTP request interception bundles of version { bundle . Version } are not supported.") ;
0 commit comments