/// <summary> /// Adds a Refit client to the DI container /// </summary> /// <param name="services">container</param> /// <param name="refitInterfaceType">Type of the Refit interface</param> /// <param name="settings">Optional. Settings to configure the instance with</param> /// <returns></returns> public static IHttpClientBuilder AddRefitClient(this IServiceCollection services, Type refitInterfaceType, RefitSettings settings = null) { return(services.AddHttpClient(UniqueName.ForType(refitInterfaceType)) .ConfigureHttpMessageHandlerBuilder(builder => { // check to see if user provided custom auth token HttpMessageHandler innerHandler = null; if (settings != null) { if (settings.HttpMessageHandlerFactory != null) { innerHandler = settings.HttpMessageHandlerFactory(); } if (settings.AuthorizationHeaderValueGetter != null) { innerHandler = new AuthenticatedHttpClientHandler(settings.AuthorizationHeaderValueGetter, innerHandler); } else if (settings.AuthorizationHeaderValueWithParamGetter != null) { innerHandler = new AuthenticatedParameterizedHttpClientHandler(settings.AuthorizationHeaderValueWithParamGetter, innerHandler); } } if (innerHandler != null) { builder.PrimaryHandler = innerHandler; } }) .AddTypedClient(refitInterfaceType, (client, serviceProvider) => RestService.For(refitInterfaceType, client, settings))); }
/// <summary> /// Adds a Refit client to the DI container /// </summary> /// <typeparam name="T">Type of the Refit interface</typeparam> /// <param name="services">container</param> /// <param name="settings">Optional. Settings to configure the instance with</param> /// <returns></returns> public static IHttpClientBuilder AddRefitClient <T>(this IServiceCollection services, RefitSettings settings = null) where T : class { services.AddSingleton(provider => RequestBuilder.ForType <T>(settings)); return(services.AddHttpClient(UniqueName.ForType <T>()) .ConfigureHttpMessageHandlerBuilder(builder => { // check to see if user provided custom auth token HttpMessageHandler innerHandler = null; if (settings != null) { if (settings.HttpMessageHandlerFactory != null) { innerHandler = settings.HttpMessageHandlerFactory(); } if (settings.AuthorizationHeaderValueGetter != null) { innerHandler = new AuthenticatedHttpClientHandler(settings.AuthorizationHeaderValueGetter, innerHandler); } else if (settings.AuthorizationHeaderValueWithParamGetter != null) { innerHandler = new AuthenticatedParameterizedHttpClientHandler(settings.AuthorizationHeaderValueWithParamGetter, innerHandler); } } if (innerHandler != null) { builder.PrimaryHandler = innerHandler; } }) .AddTypedClient((client, serviceProvider) => RestService.For <T>(client, serviceProvider.GetService <IRequestBuilder <T> >()))); }
/// <summary> /// Adds a Refit client to the DI container /// </summary> /// <typeparam name="T">Type of the Refit interface</typeparam> /// <param name="services">container</param> /// <param name="settings">Optional. Settings to configure the instance with</param> /// <returns></returns> public static IHttpClientBuilder AddRefitClient <T>(this IServiceCollection services, RefitSettings settings = null) where T : class { services.AddSingleton(provider => RequestBuilder.ForType <T>(settings)); return(services.AddHttpClient(UniqueName.ForType <T>()) .AddTypedClient((client, serviceProvider) => RestService.For <T>(client, serviceProvider.GetService <IRequestBuilder <T> >()))); }
static Type GetGeneratedType(Type refitInterfaceType) { string typeName = UniqueName.ForType(refitInterfaceType); var generatedType = Type.GetType(typeName); if (generatedType == null) { var message = refitInterfaceType.Name + " doesn't look like a Refit interface. Make sure it has at least one " + "method with a Refit HTTP method attribute and Refit is installed in the project."; throw new InvalidOperationException(message); } return(generatedType); }
/// <summary> /// Adds a Refit client to the DI container /// </summary> /// <typeparam name="T">Type of the Refit interface</typeparam> /// <param name="services">container</param> /// <param name="settingsAction">Optional. Action to configure refit settings. This method is called once and only once, avoid using any scoped dependencies that maybe be disposed automatically.</param> /// <returns></returns> public static IHttpClientBuilder AddRefitClient <T>(this IServiceCollection services, Func <IServiceProvider, RefitSettings?>?settingsAction) where T : class { services.AddSingleton(provider => new SettingsFor <T>(settingsAction?.Invoke(provider))); services.AddSingleton(provider => RequestBuilder.ForType <T>(provider.GetRequiredService <SettingsFor <T> >().Settings)); return(services .AddHttpClient(UniqueName.ForType <T>()) .ConfigureHttpMessageHandlerBuilder(builder => { // check to see if user provided custom auth token if (CreateInnerHandlerIfProvided(builder.Services.GetRequiredService <SettingsFor <T> >().Settings) is {} innerHandler) { builder.PrimaryHandler = innerHandler; } }) .AddTypedClient((client, serviceProvider) => RestService.For <T>((ApproovHttpClient)client, serviceProvider.GetService <IRequestBuilder <T> >() !))); }
/// <summary> /// Adds a Refit client to the DI container /// </summary> /// <param name="services">container</param> /// <param name="refitInterfaceType">Type of the Refit interface</param> /// <param name="settingsAction">Optional. Action to configure refit settings. This method is called once and only once, avoid using any scoped dependencies that maybe be disposed automatically.</param> /// <returns></returns> public static IHttpClientBuilder AddRefitClient(this IServiceCollection services, Type refitInterfaceType, Func <IServiceProvider, RefitSettings?>?settingsAction) { var settingsType = typeof(SettingsFor <>).MakeGenericType(refitInterfaceType); var requestBuilderType = typeof(IRequestBuilder <>).MakeGenericType(refitInterfaceType); services.AddSingleton(settingsType, provider => Activator.CreateInstance(typeof(SettingsFor <>).MakeGenericType(refitInterfaceType) !, settingsAction?.Invoke(provider)) !); services.AddSingleton(requestBuilderType, provider => RequestBuilderGenericForTypeMethod.MakeGenericMethod(refitInterfaceType).Invoke(null, new object?[] { ((ISettingsFor)provider.GetRequiredService(settingsType)).Settings }) !); return(services .AddHttpClient(UniqueName.ForType(refitInterfaceType)) .ConfigureHttpMessageHandlerBuilder(builder => { // check to see if user provided custom auth token if (CreateInnerHandlerIfProvided(((ISettingsFor)builder.Services.GetRequiredService(settingsType)).Settings) is { } innerHandler) { builder.PrimaryHandler = innerHandler; } }) .AddTypedClient(refitInterfaceType, (client, serviceProvider) => RestService.For(refitInterfaceType, client, (IRequestBuilder)serviceProvider.GetRequiredService(requestBuilderType)))); }
/// <summary> /// Adds a Refit client to the DI container /// </summary> /// <param name="services">container</param> /// <param name="refitInterfaceType">Type of the Refit interface</typeparam> /// <param name="settings">Optional. Settings to configure the instance with</param> /// <returns></returns> public static IHttpClientBuilder AddRefitClient(this IServiceCollection services, Type refitInterfaceType, RefitSettings settings = null) { return(services.AddHttpClient(UniqueName.ForType(refitInterfaceType)) .AddTypedClient((client, serviceProvider) => RestService.For(refitInterfaceType, client, settings))); }