/// <summary>
        /// Adds a plain HttpClient for the given file upload handler which is hosted at the given baseAddress.
        /// The ApiFileHandler uses this HttpClient to communicate to the server.
        /// </summary>
        /// <typeparam name="TEntity"></typeparam>
        /// <param name="services"></param>
        /// <param name="baseAddress">Base address of the api, for example: https://example.com</param>
        /// <param name="collectionAlias"></param>
        /// <returns></returns>
        public static IHttpClientBuilder AddRapidCMSFileUploadApiHttpClient <THandler>(this IServiceCollection services, Uri baseUri)
            where THandler : IFileUploadHandler
        {
            var alias = AliasHelper.GetFileUploaderAlias(typeof(THandler));

            return(services.AddHttpClient <ApiFileUploadHandler <THandler> >(alias)
                   .ConfigureHttpClient(x => x.BaseAddress = new Uri(baseUri, $"api/_rapidcms/{alias}/")));
        }
Exemple #2
0
        public IApiConfig RegisterFileUploadHandler <THandler>() where THandler : IFileUploadHandler
        {
            FileUploadHandlers.Add(new FileUploadHandlerConfig
            {
                Alias       = AliasHelper.GetFileUploaderAlias(typeof(THandler)),
                HandlerType = typeof(THandler)
            });

            return(this);
        }
Exemple #3
0
 public ApiFileUploadHandler(
     IHttpClientFactory httpClientFactory)
 {
     _httpClientFactory = httpClientFactory;
     _handlerAlias      = AliasHelper.GetFileUploaderAlias(typeof(THandler));
 }
Exemple #4
0
        /// <summary>
        /// Use this method to setup the Repository APIs to support RapidCMS WebAssenbly on a separate server.
        /// </summary>
        /// <param name="services"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public static IServiceCollection AddRapidCMSApi(this IServiceCollection services, Action <IApiConfig>?config = null)
        {
            if (_routeConvention != null || _controllerFeatureProvider != null)
            {
                throw new InvalidOperationException("Cannot call AddRapidCMSApi twice.");
            }

            var rootConfig = GetRootConfig(config);

            services.AddSingleton <IApiConfig>(rootConfig);

            services.AddHttpContextAccessor();

            if (rootConfig.AllowAnonymousUsage)
            {
                services.AddSingleton <IAuthorizationHandler, AllowAllAuthorizationHandler>();
                services.AddSingleton <AuthenticationStateProvider, AnonymousAuthenticationStateProvider>();
            }
            else
            {
                services.AddSingleton <AuthenticationStateProvider, HttpContextAuthenticationStateProvider>();
            }

            services.AddSingleton <ISetupResolver <IEntityVariantSetup>, GlobalEntityVariantSetupResolver>();

            services.AddTransient <IDataViewResolver, ApiDataViewResolver>();
            services.AddTransient <IRepositoryResolver, RepositoryResolver>();
            services.AddSingleton <IRepositoryTypeResolver, ApiRepositoryTypeResolver>();

            services.AddTransient <IPresentationDispatcher, GetEntityDispatcher>();
            services.AddTransient <IPresentationDispatcher, GetEntitiesDispatcher>();
            services.AddTransient <IPresentationService, PresentationService>();

            services.AddTransient <IInteractionDispatcher, RelateEntityDispatcher>();
            services.AddTransient <IInteractionDispatcher, ReorderEntityDispatcher>();
            services.AddTransient <IInteractionDispatcher, PersistEntityDispatcher>();
            services.AddTransient <IInteractionDispatcher, DeleteEntityDispatcher>();
            services.AddTransient <IInteractionService, InteractionService>();

            services.AddTransient <IAuthService, ApiAuthService>();
            services.AddSingleton <IExceptionService, ExceptionService>();
            // TODO: message service
            services.AddTransient <IParentService, ParentService>();

            services.AddTransient <IEditContextFactory, ApiEditContextWrapperFactory>();

            var controllersToAdd = rootConfig.Repositories.ToDictionary(
                repository =>
            {
                if (repository.DatabaseType == default)
                {
                    return(typeof(ApiRepositoryController <,>)
                           .MakeGenericType(repository.EntityType, repository.RepositoryType)
                           .GetTypeInfo());
                }
                else
                {
                    return(typeof(MappedApiRepositoryController <, ,>)
                           .MakeGenericType(repository.EntityType, repository.DatabaseType, repository.RepositoryType)
                           .GetTypeInfo());
                }
            },
                kv => kv.Alias);

            if (rootConfig.FileUploadHandlers.Any())
            {
                foreach (var handler in rootConfig.FileUploadHandlers)
                {
                    var type  = typeof(ApiFileUploadController <>).MakeGenericType(handler).GetTypeInfo();
                    var alias = AliasHelper.GetFileUploaderAlias(type);

                    controllersToAdd.Add(type, alias);
                }
            }

            _controllerFeatureProvider = new CollectionControllerFeatureProvider(controllersToAdd.Keys);
            _routeConvention           = new CollectionControllerRouteConvention(controllersToAdd);

            return(services);
        }