private async Task <IAuthenticationProvider> AuthenticationProviderAsync() => authenticationProvider ?? (authenticationProvider = await graphAuthenticationProviderFactory.GetByConfig(config));
Ejemplo n.º 2
0
        public async Task <CalendarConfiguration[]> GetConfigurations(string userid)
        {
            var storedConfigurations = await repository.GetConfigurations(userid);

            var res = new List <CalendarConfiguration>();

            if (null == storedConfigurations)
            {
                return(null);
            }
            foreach (var config in storedConfigurations)
            {
                if (config.Type == CalendarType.Microsoft)
                {
                    Feed[] feeds    = new Feed[0];
                    var    provider = await authenticationProviderFactory.GetByConfig(config);

                    var client = new GraphServiceClient(provider);
                    try
                    {
                        var calendars = await client.Me.Calendars.Request().GetAsync();

                        feeds = calendars.Select(calendar => new Feed()
                        {
                            Id         = calendar.Id,
                            Name       = calendar.Name,
                            Subscribed = config.SubscribedFeeds.Any(v => v.FeedId == calendar.Id)
                        }).ToArray();
                    }
                    catch (Exception e)
                    {
                        logger.LogError(e, "Could not retrieve feeds from MSGraph");
                    }
                    res.Add(new CalendarConfiguration()
                    {
                        Type       = config.Type,
                        Feeds      = feeds,
                        Id         = config.Id,
                        Identifier = "MS Calendar"
                    });
                }
                else if (config.Type == CalendarType.Google)
                {
                    Feed[] feeds  = new Feed[0];
                    var    client = new Google.Apis.Calendar.v3.CalendarService(new Google.Apis.Services.BaseClientService.Initializer()
                    {
                        HttpClientInitializer = await googleCredentialProvider.CreateByConfigAsync(config)
                    });
                    try
                    {
                        var calendars = await client.CalendarList.List().ExecuteAsync();

                        feeds = calendars.Items.Select(calendar => new Feed()
                        {
                            Id         = calendar.Id,
                            Name       = calendar.Summary,
                            Subscribed = config.SubscribedFeeds.Any(v => v.FeedId == calendar.Id)
                        }).ToArray();
                    }
                    catch (Exception e)
                    {
                        logger.LogError(e, "Could not retrieve feeds from Google");
                    }
                    res.Add(new CalendarConfiguration()
                    {
                        Type       = config.Type,
                        Feeds      = feeds,
                        Id         = config.Id,
                        Identifier = "Google"
                    });
                }
                else
                {
                    throw new NotImplementedException();
                }
            }
            return(res.ToArray());
        }