forked from FarmMaps/FarmMapsApiClient
		
	refactored program
This commit is contained in:
		@@ -1,38 +1,11 @@
 | 
			
		||||
using System;
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Threading;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using FarmmapsApi.HttpMessageHandlers;
 | 
			
		||||
using FarmmapsApi.Models;
 | 
			
		||||
using FarmmapsApi.Services;
 | 
			
		||||
using IdentityModel.Client;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
 | 
			
		||||
namespace FarmmapsApi
 | 
			
		||||
{
 | 
			
		||||
    public static class Extensions
 | 
			
		||||
    {
 | 
			
		||||
        public static IServiceCollection AddFarmmapsServices(this IServiceCollection serviceCollection,
 | 
			
		||||
            Configuration configuration)
 | 
			
		||||
        {
 | 
			
		||||
            return serviceCollection
 | 
			
		||||
                .AddSingleton(configuration)
 | 
			
		||||
                .AddSingleton<IDiscoveryCache>(sp =>
 | 
			
		||||
                {
 | 
			
		||||
                    var httpFactory = sp.GetRequiredService<IHttpClientFactory>();
 | 
			
		||||
                    return new DiscoveryCache(configuration.DiscoveryEndpointUrl,
 | 
			
		||||
                        () => httpFactory.CreateClient());
 | 
			
		||||
                })
 | 
			
		||||
                .AddSingleton<HttpClientSettings>()
 | 
			
		||||
                .AddSingleton<FarmmapsEventHub>()
 | 
			
		||||
                .AddTransient<OpenIdConnectService>()
 | 
			
		||||
                .AddTransient<FarmmapsAuthenticationHandler>()
 | 
			
		||||
                .AddTransient<GeneralService>()
 | 
			
		||||
                .AddHttpClient<FarmmapsApiService>()
 | 
			
		||||
                .AddHttpMessageHandler<FarmmapsAuthenticationHandler>()
 | 
			
		||||
                .Services;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        public static async Task PollTask(TimeSpan retryTime, Func<CancellationTokenSource, Task> callback)
 | 
			
		||||
        {
 | 
			
		||||
            var tokenSource = new CancellationTokenSource();
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										52
									
								
								FarmmapsApi/FarmmapsProgram.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										52
									
								
								FarmmapsApi/FarmmapsProgram.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,52 @@
 | 
			
		||||
using System.Net.Http;
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
using FarmmapsApi.HttpMessageHandlers;
 | 
			
		||||
using FarmmapsApi.Models;
 | 
			
		||||
using FarmmapsApi.Services;
 | 
			
		||||
using IdentityModel.Client;
 | 
			
		||||
using Microsoft.Extensions.Configuration;
 | 
			
		||||
using Microsoft.Extensions.DependencyInjection;
 | 
			
		||||
 | 
			
		||||
namespace FarmmapsApi
 | 
			
		||||
{
 | 
			
		||||
    public abstract class FarmmapsProgram<T> where T : class, IApplication
 | 
			
		||||
    {
 | 
			
		||||
        public async Task Start(string[] args)
 | 
			
		||||
        {
 | 
			
		||||
            IConfiguration config = new ConfigurationBuilder()
 | 
			
		||||
                .AddJsonFile("appsettings.json", false, true)
 | 
			
		||||
                .Build();
 | 
			
		||||
 | 
			
		||||
            var configuration = config.Get<Configuration>();
 | 
			
		||||
 | 
			
		||||
            var serviceCollection = new ServiceCollection()
 | 
			
		||||
                .AddSingleton(configuration)
 | 
			
		||||
                .AddSingleton<IDiscoveryCache>(sp =>
 | 
			
		||||
                {
 | 
			
		||||
                    var httpFactory = sp.GetRequiredService<IHttpClientFactory>();
 | 
			
		||||
                    return new DiscoveryCache(configuration.DiscoveryEndpointUrl,
 | 
			
		||||
                        () => httpFactory.CreateClient());
 | 
			
		||||
                })
 | 
			
		||||
                .AddSingleton<HttpClientSettings>()
 | 
			
		||||
                .AddSingleton<FarmmapsEventHub>()
 | 
			
		||||
                .AddSingleton<T>()
 | 
			
		||||
                .AddTransient<OpenIdConnectService>()
 | 
			
		||||
                .AddTransient<FarmmapsAuthenticationHandler>()
 | 
			
		||||
                .AddTransient<GeneralService>()
 | 
			
		||||
                .AddHttpClient<FarmmapsApiService>()
 | 
			
		||||
                .AddHttpMessageHandler<FarmmapsAuthenticationHandler>()
 | 
			
		||||
                .Services;
 | 
			
		||||
            
 | 
			
		||||
 | 
			
		||||
            Configure(serviceCollection);
 | 
			
		||||
 | 
			
		||||
            var serviceProvider = serviceCollection.BuildServiceProvider();
 | 
			
		||||
            await serviceProvider.GetService<FarmmapsApiService>().AuthenticateAsync();
 | 
			
		||||
//            await serviceProvider.GetService<FarmmapsEventHub>().StartEventHub();
 | 
			
		||||
 | 
			
		||||
            await serviceProvider.GetRequiredService<T>().RunAsync();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        protected abstract void Configure(IServiceCollection serviceCollection);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
							
								
								
									
										9
									
								
								FarmmapsApi/IApplication.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								FarmmapsApi/IApplication.cs
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,9 @@
 | 
			
		||||
using System.Threading.Tasks;
 | 
			
		||||
 | 
			
		||||
namespace FarmmapsApi
 | 
			
		||||
{
 | 
			
		||||
    public interface IApplication
 | 
			
		||||
    {
 | 
			
		||||
        Task RunAsync();
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -6,6 +6,7 @@ using System.Threading.Tasks;
 | 
			
		||||
using FarmmapsApi.Models;
 | 
			
		||||
using Google.Apis.Upload;
 | 
			
		||||
using Microsoft.Extensions.Logging;
 | 
			
		||||
using Newtonsoft.Json.Linq;
 | 
			
		||||
using static FarmmapsApi.Extensions;
 | 
			
		||||
using static FarmmapsApiSamples.Constants;
 | 
			
		||||
 | 
			
		||||
@@ -24,6 +25,23 @@ namespace FarmmapsApi.Services
 | 
			
		||||
            _farmmapsApiService = farmmapsApiService;
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public async Task<Item> CreateCropfieldItemAsync(string parentItemCode, string name, int year, string fieldGeomJson)
 | 
			
		||||
        {
 | 
			
		||||
            var currentYear = new DateTime(year, 1, 1);
 | 
			
		||||
            var cropfieldItemRequest = new ItemRequest()
 | 
			
		||||
            {
 | 
			
		||||
                ParentCode = parentItemCode,
 | 
			
		||||
                ItemType = CROPFIELD_ITEMTYPE,
 | 
			
		||||
                Name = name,
 | 
			
		||||
                DataDate = currentYear,
 | 
			
		||||
                DataEndDate = currentYear.AddYears(1).AddDays(-1),
 | 
			
		||||
                Data = JObject.Parse("{}"),
 | 
			
		||||
                Geometry = JObject.Parse(fieldGeomJson)
 | 
			
		||||
            };
 | 
			
		||||
 | 
			
		||||
            return await _farmmapsApiService.CreateItemAsync(cropfieldItemRequest);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
        public async Task<Item> UploadDataAsync(UserRoot root, string itemType, string filePath, string itemName)
 | 
			
		||||
        {
 | 
			
		||||
            var startUpload = DateTime.UtcNow;
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user