コード例 #1
0
        /// <summary>
        /// Serializes a <see cref="ApiKeySecurityScheme"/> value.
        /// </summary>
        /// <param name="value">The <see cref="ApiKeySecurityScheme"/> value to serialize.</param>
        /// <returns>The <see cref="JsonValue"/>.</returns>
        protected virtual JsonValue SerializeApiKeySecurityScheme(ApiKeySecurityScheme value)
        {
            if (value is null)
            {
                return(null);
            }

            var json = new JsonObject();

            SetJsonValue(json, PropertyConstants.Type, EnumConstants.ApiKey);
            SetJsonValue(json, PropertyConstants.Description, value.Description);
            SetJsonValue(json, PropertyConstants.Name, value.Name);
            SetJsonValue(json, PropertyConstants.In, ToJsonValue(value.Location));

            return(json);
        }
コード例 #2
0
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.Configure <ApiBehaviorOptions>(options =>
            {
                options.InvalidModelStateResponseFactory = DevKnowledgeBookModelStateValidator.ValidateModelState;
            });
            services.Configure <DevKnowledgeBookConfiguration>(Configuration.GetSection("DevKnowledgeBook"));
            services.Configure <IbmConfiguration>(Configuration.GetSection("IBM"));

            services.AddRouting(options =>
            {
                options.LowercaseUrls = true;
            });

            services.AddControllers(options =>
            {
                options.EnableEndpointRouting = false;
                options.Filters.Add <ApiKeyAuthorizationFilter>();
                options.Filters.Add <ApiExceptionFilter>();
            })
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
            .ConfigureApiBehaviorOptions(options =>
            {
                options.InvalidModelStateResponseFactory = DevKnowledgeBookModelStateValidator.ValidateModelState;
            })
            .AddJsonOptions(options =>
            {
                options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
            });

            //TODO: Read about singleton vs transient vs scoped https://docs.microsoft.com/en-us/aspnet/core/fundamentals/dependency-injection?view=aspnetcore-3.0
            services.AddSingleton <FirestoreDb>(serviceProvider =>
            {
                return(FirestoreDb.Create(Configuration["Firebase:ProjectID"])); //TODO: Add google environment variable https://cloud.google.com/docs/authentication/getting-started
            });

            services.AddSingleton <INaturalLanguageUnderstandingService>(serviceProvider =>
            {
                var IbmConfiguration       = serviceProvider.GetService <IOptions <IbmConfiguration> >().Value;
                var naturalLanguageService = new NaturalLanguageUnderstandingService
                {
                    UserName    = "******",
                    Password    = IbmConfiguration.ApiKey, //TODO: change for production
                    ApiKey      = IbmConfiguration.ApiKey, //TODO: change for production
                    VersionDate = IbmConfiguration.Version,
                };

                naturalLanguageService.SetEndpoint(IbmConfiguration.Url);

                return(naturalLanguageService);
            });

            services.AddSingleton <IFirestoreDbContext, FirestoreDbContext>();
            services.AddSingleton <INaturalLanguageService, NaturalLanguageService>();

            services.AddSwaggerGen(options =>
            {
                options.SwaggerDoc("v1", new OpenApiInfo {
                    Title = "DevKnowledgeBook API", Version = "v1"
                });
                options.AddSecurityDefinition("Api Key", ApiKeySecurityScheme.Instance());
                options.AddSecurityRequirement(new OpenApiSecurityRequirement
                {
                    { ApiKeySecurityScheme.Instance(), new List <string>() },
                });

                var xmlFile = $"{Assembly.GetExecutingAssembly().GetName().Name}.xml";
                var xmlPath = Path.Combine(AppContext.BaseDirectory, xmlFile);
                options.IncludeXmlComments(xmlPath);
            });
        }