谷歌API的.NET - 比其他FileDataStore任何其他数据存储? [英] Google .NET APIs - any other DataStore other than the FileDataStore?

查看:175
本文介绍了谷歌API的.NET - 比其他FileDataStore任何其他数据存储?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是谷歌.NET API从谷歌分析获取分析数据。

I'm using the Google .NET API to get analytics data from google analytics.

这是我的code开始认证:

this is me code to start the authentication:

IAuthorizationCodeFlow flow =
    new GoogleAuthorizationCodeFlow(new GoogleAuthorizationCodeFlow.Initializer
        {
            ClientSecrets = new ClientSecrets
            {
                ClientId = googleApiClientId,
                ClientSecret = googleApiClientSecret
            },
            Scopes = new[] { 
                Google.Apis.Analytics.v3.AnalyticsService.Scope.AnalyticsReadonly
            },
            DataStore = new Google.Apis.Util.Store.FileDataStore("Test_GoogleApi")
        });

它的用户存储在本地用户配置文件作为一个文件FileDataStore。我运行一个ASP.NET应用程序这里面code,所以我真的不能使用FileDataStore所以我需要的是一些其他的方式来获得数据。

it users the FileDataStore which stores in local user profile as a file. I'm running this code inside an ASP.NET application so I can't really use that FileDataStore so what I will need is some other way to get the data.

Google.Apis.Util.Store仅包含FileDataStore和IDataStore的接口。我去实现我自己的数据存储之前 - 有没有其他任何数据存储对象,在那里可以下载

Google.Apis.Util.Store contains only the FileDataStore and an interface of IDataStore. Before I go and implement my own DataStore - are there any other DataStore objects out there available for download?

感谢

推荐答案

有关谷歌的FileDataStore提供源代码的here.

The source for Google's FileDataStore is available here.

我写了一个简单的实体框架(第6版)的实现IDataStore的,如下图所示。

I've written a simple Entity Framework (version 6) implementation of IDataStore as shown below.

如果你希望把这个单独的项目,以及EF则需要在安装Google.Apis.Core 的NuGet包。

If you're looking to put this in a separate project, as well as EF you'll need the Google.Apis.Core nuget package installed.

public class Item
{
    [Key]
    [MaxLength(100)]
    public string Key { get; set; }

    [MaxLength(500)]
    public string Value { get; set; }
}

public class GoogleAuthContext : DbContext
{
    public DbSet<Item> Items { get; set; }
}

public class EFDataStore : IDataStore
{
    public async Task ClearAsync()
    {
        using (var context = new GoogleAuthContext())
        {
            var objectContext = ((IObjectContextAdapter)context).ObjectContext;
            await objectContext.ExecuteStoreCommandAsync("TRUNCATE TABLE [Items]");
        }
    }

    public async Task DeleteAsync<T>(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentException("Key MUST have a value");
        }

        using (var context = new GoogleAuthContext())
        {
            var generatedKey = GenerateStoredKey(key, typeof(T));
            var item = context.Items.FirstOrDefault(x => x.Key == generatedKey);
            if (item != null)
            {
                context.Items.Remove(item);
                await context.SaveChangesAsync();
            }
        }
    }

    public Task<T> GetAsync<T>(string key)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentException("Key MUST have a value");
        }

        using (var context = new GoogleAuthContext())
        {
            var generatedKey = GenerateStoredKey(key, typeof(T));
            var item = context.Items.FirstOrDefault(x => x.Key == generatedKey);
            T value = item == null ? default(T) : JsonConvert.DeserializeObject<T>(item.Value);
            return Task.FromResult<T>(value);
        }
    }

    public async Task StoreAsync<T>(string key, T value)
    {
        if (string.IsNullOrEmpty(key))
        {
            throw new ArgumentException("Key MUST have a value");
        }

        using (var context = new GoogleAuthContext())
        {
            var generatedKey = GenerateStoredKey(key, typeof (T));
            string json = JsonConvert.SerializeObject(value);

            var item = await context.Items.SingleOrDefaultAsync(x => x.Key == generatedKey);

            if (item == null)
            {
                context.Items.Add(new Item { Key = generatedKey, Value = json});
            }
            else
            {
                item.Value = json;
            }

            await context.SaveChangesAsync();
        }
    }

    private static string GenerateStoredKey(string key, Type t)
    {
        return string.Format("{0}-{1}", t.FullName, key);
    }
}

这篇关于谷歌API的.NET - 比其他FileDataStore任何其他数据存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆