All Downloads are FREE. Search and download functionalities are using the official Maven repository.

csharp.libraries.generichost.HostConfiguration.mustache Maven / Gradle / Ivy

There is a newer version: 7.9.0
Show newest version
{{>partial_header}}
{{#nrt}}
#nullable enable

{{/nrt}}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Net.Http;
using Microsoft.Extensions.DependencyInjection;
using {{packageName}}.{{apiPackage}};
using {{packageName}}.{{modelPackage}};

namespace {{packageName}}.{{clientPackage}}
{
    /// 
    /// Provides hosting configuration for {{packageName}}
    /// 
    {{>visibility}} class HostConfiguration
    {
        private readonly IServiceCollection _services;
        private readonly JsonSerializerOptions _jsonOptions = new JsonSerializerOptions();

        internal bool HttpClientsAdded { get; private set; }

        /// 
        /// Instantiates the class 
        /// 
        /// 
        public HostConfiguration(IServiceCollection services)
        {
            _services = services;
            _jsonOptions.Converters.Add(new JsonStringEnumConverter());
            _jsonOptions.Converters.Add(new DateTimeJsonConverter());
            _jsonOptions.Converters.Add(new DateTimeNullableJsonConverter());
            {{#supportsDateOnly}}
            _jsonOptions.Converters.Add(new DateOnlyJsonConverter());
            _jsonOptions.Converters.Add(new DateOnlyNullableJsonConverter());
            {{/supportsDateOnly}}
            {{#models}}
            {{#model}}
            {{#isEnum}}
            _jsonOptions.Converters.Add(new {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}JsonConverter());
            _jsonOptions.Converters.Add(new {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}NullableJsonConverter());
            {{/isEnum}}
            {{^isEnum}}
            _jsonOptions.Converters.Add(new {{classname}}JsonConverter());
            {{/isEnum}}
            {{/model}}
            {{/models}}
            JsonSerializerOptionsProvider jsonSerializerOptionsProvider = new{{^net60OrLater}} JsonSerializerOptionsProvider{{/net60OrLater}}(_jsonOptions);
            _services.AddSingleton(jsonSerializerOptionsProvider);
            {{#useSourceGeneration}}

            {{#models}}
            {{#-first}}
            _jsonOptions.TypeInfoResolver = System.Text.Json.Serialization.Metadata.JsonTypeInfoResolver.Combine(
            {{/-first}}
            {{/models}}
                {{#lambda.joinLinesWithComma}}
                {{#models}}
                {{#model}}
                new {{datatypeWithEnum}}{{^datatypeWithEnum}}{{classname}}{{/datatypeWithEnum}}SerializationContext(){{#-last}},{{/-last}}
                {{/model}}
                {{/models}}
                {{/lambda.joinLinesWithComma}}
            {{#models}}
            {{#-last}}

                new System.Text.Json.Serialization.Metadata.DefaultJsonTypeInfoResolver()
            );
            {{/-last}}
            {{/models}}

            {{/useSourceGeneration}}
            _services.AddSingleton();{{#apiInfo}}{{#apis}}
            _services.AddSingleton<{{classname}}Events>();
            _services.AddTransient<{{interfacePrefix}}{{classname}}, {{classname}}>();{{/apis}}{{/apiInfo}}
        }

        /// 
        /// Configures the HttpClients.
        /// 
        /// 
        /// 
        /// 
        public HostConfiguration Add{{apiName}}HttpClients
        (
            Action{{nrt?}} client = null, Action{{nrt?}} builder = null)
        {
            if (client == null)
                client = c => c.BaseAddress = new Uri(ClientUtils.BASE_ADDRESS);

            List builders = new List();

            {{#apiInfo}}{{#apis}}builders.Add(_services.AddHttpClient<{{interfacePrefix}}{{classname}}, {{classname}}>(client));
            {{/apis}}{{/apiInfo}}
            if (builder != null)
                foreach (IHttpClientBuilder instance in builders)
                    builder(instance);

            HttpClientsAdded = true;

            return this;
        }

        /// 
        /// Configures the JsonSerializerSettings
        /// 
        /// 
        /// 
        public HostConfiguration ConfigureJsonOptions(Action options)
        {
            options(_jsonOptions);

            return this;
        }

        /// 
        /// Adds tokens to your IServiceCollection
        /// 
        /// 
        /// 
        /// 
        public HostConfiguration AddTokens(TTokenBase token) where TTokenBase : TokenBase
        {
            return AddTokens(new TTokenBase[]{ token });
        }

        /// 
        /// Adds tokens to your IServiceCollection
        /// 
        /// 
        /// 
        /// 
        public HostConfiguration AddTokens(IEnumerable tokens) where TTokenBase : TokenBase
        {
            TokenContainer container = new TokenContainer(tokens);
            _services.AddSingleton(services => container);

            return this;
        }

        /// 
        /// Adds a token provider to your IServiceCollection
        /// 
        /// 
        /// 
        /// 
        public HostConfiguration UseProvider() 
            where TTokenProvider : TokenProvider
            where TTokenBase : TokenBase
        {
            _services.AddSingleton();
            _services.AddSingleton>(services => services.GetRequiredService());

            return this;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy