avivbiton / BlizzardApiReader

.Net Core library to handle Blizzard public APIs
MIT License
35 stars 36 forks source link

Initial version with dependency injection #52

Closed redmars27 closed 5 years ago

redmars27 commented 5 years ago

Linked with #50 . Not a final pull request. DONT MERGE. I just wanted to let you know the current status of migrating to dependency injection and get your opinion. I have not touched yet the refactoring for the unit tests, so it will have errors.

Current changes:

with current version a command line client can be as simple as this:

    static void Main(string[] args)
    {
        var services = new ServiceCollection();

        services.Configure<ApiConfiguration>(Options => Options.SetClientId(clientId)
            .SetClientSecret(secret)
            .SetRegion(region)
            .SetLocale(locale));

        services.AddBlizzardApiReaderDiablo();
        services.AddBlizzardApiReaderWorldOfWarcraft();

        var provider = services.BuildServiceProvider();

        var api = provider.GetService<DiabloApi>();

        StoryAct act1 = api.GetActAsync(1).Result;
        Console.WriteLine($"act 1 name: {act1.Name}");
        Console.ReadLine();

        var wowApi = provider.GetService<WorldOfWarcraftApi>();
        Boss boss = wowApi.GetBossesAsync().Result[0];
        Console.WriteLine($"Name: {boss.Name} Level: {boss.Level}");

        Console.ReadLine();

and in a webclient even simpler, you just need to add 3 lines in the configure services and you can use the api in any controller:

    public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });                      

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.Configure<ApiConfiguration>(Options => Options.SetClientId(clientId)
            .SetClientSecret(secret)
            .SetRegion(region)
            .SetLocale(locale));

        services.AddBlizzardApiReaderDiablo();
        services.AddBlizzardApiReaderWorldOfWarcraft();

    }
avivbiton commented 5 years ago

Thank you for doing this. Maybe we can merge this into a development branch then the stable branch will not be affected. What do you say?

redmars27 commented 5 years ago

Sure, if anyone else want to contribute. It is always welcomed. I should be able to spend some time on the testing part to fix it in the next few days, then I guess it will be good to be merged into master

redmars27 commented 5 years ago

Correcting the unit tests and updating the example file to reflect the new structure

redmars27 commented 5 years ago

Separating the http part of the configuration from the api configuration so it can have a cleaner setup for dependency injection. Current version is now with all the functionality as before but with the addition of dependency injection. This is the final version to merge to master.