Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

统一命名规范 #6

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 11 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,36 +56,29 @@ NuGet\Install-Package Ecnu.OpenApi.Sdk -Version 1.0.0
```

安装完成后,进行简单配置即可获得授权接口的数据。以下为简单示例
* 注意:开发者需要根据所需要数据的接口来编写实体类(如示例代码中的FakeData类需要根据fake或fakewithts接口数据自己编写
* 注意:开发者需要根据所需要数据的接口来编写实体类(如示例代码中的FakeData类需要根据fakewithts接口数据自己编写
```csharp
static void Main(string[] args)
{
//获取token所需要的配置
OauthConfig.ClientId = "yourid";
OauthConfig.ClientSecret = "yoursecret";
OAuth2Config.ClientId = "yourid";
OAuth2Config.ClientSecret = "yoursecret";
//初始化token
OauthToken.InitialOauthCredential(OauthConfig.ClientId, OauthConfig.ClientSecret);
SdkApi.InitOAuth2ClientCredentials(OAuth2Config.ClientId, OAuth2Config.ClientSecret);

//api配置
ApiConfig.ApiUrl = "/api/v1/sync/fakewithts";
ApiConfig.OutputFilePath = @"D:\newsync.csv";
ApiConfig.PageSize = 2000;
ApiConfig.BatchSize = 10000;
SdkApi.AddParameter("ts", "0");
APIConfig.ApiUrl = "/api/v1/sync/fakewithts";
APIConfig.PageSize = 10;
APIConfig.ApiParameters.Add("ts", "0");


//初始化ouath,获取token
OauthToken.InitialOauthCredential(OauthConfig.ClientId, OauthConfig.ClientSecret);


//调用api,获取数据
var res = SdkApi.CallApi("https://api.ecnu.edu.cn/api/v1/sync/fakewithts?pageSize=100&pageNum=1&ts=0", "get");
Console.WriteLine(res);
Console.ReadLine();
//导入模型,可以根据业务再进行筛查
List<FakeData> list = SdkApi.SyncToModel<FakeData>();
var result = list.Where(x=> x.colInt1 > 30).ToList();
}
```

详细用法请参考示例:
- [CallAPI](examples/exampleCallAPI.cs)
- [SyncToCSV](examples/exampleToCsv.cs)
- [SyncToModel](examples/exampleToModel.cs)
- [SyncToDB](examples/exampleToDb.cs)
Expand Down
7 changes: 6 additions & 1 deletion ecnu-openapi-sdk-csharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,13 @@
<Authors>ECNU</Authors>
<Company>ECNU</Company>
<PackageId>Ecnu.OpenApi.Sdk</PackageId>
<PackageReadmeFile>readme.md</PackageReadmeFile>
<PackageReadmeFile>README.md</PackageReadmeFile>
<Description>提供接入ECNU数据开发帮助,开发者仅需简单的配置即可获得ECNU授权的数据。</Description>
<RepositoryUrl>https://github.com/ECNU/ecnu-openapi-sdk-csharp</RepositoryUrl>
<AssemblyVersion>1.0.2</AssemblyVersion>
<FileVersion>1.0.2</FileVersion>
<Version>1.0.2</Version>
<Title>提供接入ECNU数据开发帮助,开发者仅需简单的配置即可获得ECNU授权的数据。</Title>
</PropertyGroup>


Expand Down
20 changes: 20 additions & 0 deletions examples/exampleCallAPI.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using DataSyncSdk;

namespace ecnu_openapi_sdk_csharp.examples
{
public class exampleCallAPI
{
static void Main(string[] args)
{
//获取token所需要的配置
OAuth2Config.ClientId = "yourid";
OAuth2Config.ClientSecret = "yoursecret";
//初始化token
SdkApi.InitOAuth2ClientCredentials(OAuth2Config.ClientId, OAuth2Config.ClientSecret);

//调用api,获取数据
var res = SdkApi.CallApi("https://api.ecnu.edu.cn/api/v1/sync/fakewithts?ts=0&pageNum=1&pageSize=1", "get");
Console.WriteLine(res);
}
}
}
80 changes: 40 additions & 40 deletions examples/examplePerformance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,51 +4,51 @@ public class examplePerformance
{
static void Main(string[] args)
{
//获取token所需要的配置
OauthConfig.ClientId = "yourid";
OauthConfig.ClientSecret = "yoursecret";
//初始化token
OauthToken.InitialOauthCredential(OauthConfig.ClientId, OauthConfig.ClientSecret);
//获取token所需要的配置
OAuth2Config.ClientId = "yourid";
OAuth2Config.ClientSecret = "yoursecret";
//初始化token
SdkApi.InitOAuth2ClientCredentials(OAuth2Config.ClientId, OAuth2Config.ClientSecret);

//api配置
ApiConfig.ApiUrl = "/api/v1/sync/fake";
ApiConfig.OutputFilePath = @"D:\newsync.csv";
ApiConfig.PageSize = 2000;
ApiConfig.BatchSize = 10000;
SdkApi.AddParameter("totalNum", "1000000");
//api配置
APIConfig.ApiUrl = "/api/v1/sync/fake";
APIConfig.OutputFilePath = @"D:\newsync.csv";
APIConfig.PageSize = 2000;
APIConfig.BatchSize = 10000;
APIConfig.ApiParameters.Add("totalNum", "1000000");

//同步至模型
Stopwatch st = new Stopwatch();
st.Start();
GC.Collect();
var list = SdkApi.SyncToModel<FakeData>();
double usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;
st.Stop();
Console.WriteLine("消耗内存:" + usedMemory + "MB");
Console.WriteLine("程序执行耗时:" + st.ElapsedMilliseconds / 1000 + "S");
//同步至模型
Stopwatch st = new Stopwatch();
st.Start();
GC.Collect();
var list = SdkApi.SyncToModel<FakeData>();
double usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;
st.Stop();
Console.WriteLine("消耗内存:" + usedMemory + "MB");
Console.WriteLine("程序执行耗时:" + st.ElapsedMilliseconds / 1000 + "S");

//同步至csv文件
st.Start();
GC.Collect();
SdkApi.SyncToCsv<FakeData>(ApiConfig.OutputFilePath);
usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;
st.Stop();
Console.WriteLine("消耗内存:" + usedMemory + "MB");
Console.WriteLine("程序执行耗时:" + st.ElapsedMilliseconds / 1000 + "S");
Console.ReadLine();
//同步至csv文件
st.Start();
GC.Collect();
SdkApi.SyncToCsv<FakeData>(APIConfig.OutputFilePath);
usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;
st.Stop();
Console.WriteLine("消耗内存:" + usedMemory + "MB");
Console.WriteLine("程序执行耗时:" + st.ElapsedMilliseconds / 1000 + "S");
Console.ReadLine();



//同步至数据库,数据库类型及链接字符串在SqlSugarDbContext类中配置
st.Start();
GC.Collect();
string conStr = "DataSource=D:\\syncdb.db";
SqlSugarDbContext db = new SqlSugarDbContext(conStr, SqlSugar.DbType.Sqlite);
SdkApi.SyncToDb<FakeData>(db);
usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;
st.Stop();
Console.WriteLine("消耗内存:" + usedMemory + "MB");
Console.WriteLine("程序执行耗时:" + st.ElapsedMilliseconds / 1000 + "S");
Console.ReadLine();
//同步至数据库,数据库类型及链接字符串在SqlSugarDbContext类中配置
st.Start();
GC.Collect();
string conStr = "DataSource=D:\\syncdb.db";
SqlSugarDbContext db = new SqlSugarDbContext(conStr, SqlSugar.DbType.Sqlite);
SdkApi.SyncToDb<FakeData>(db);
usedMemory = Process.GetCurrentProcess().WorkingSet64 / 1024.0 / 1024.0;
st.Stop();
Console.WriteLine("消耗内存:" + usedMemory + "MB");
Console.WriteLine("程序执行耗时:" + st.ElapsedMilliseconds / 1000 + "S");
Console.ReadLine();
}
}
16 changes: 8 additions & 8 deletions examples/exampleToCsv.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@ public class exampleToCsv
static void Main(string[] args)
{
//获取token所需要的配置
OauthConfig.ClientId = "yourid";
OauthConfig.ClientSecret = "yoursecret";
OAuth2Config.ClientId = "yourid";
OAuth2Config.ClientSecret = "yoursecret";
//初始化token
OauthToken.InitialOauthCredential(OauthConfig.ClientId, OauthConfig.ClientSecret);
SdkApi.InitOAuth2ClientCredentials(OAuth2Config.ClientId, OAuth2Config.ClientSecret);


//api配置
ApiConfig.ApiUrl = "/api/v1/sync/fakewithts";
ApiConfig.OutputFilePath = @"D:\newsync.csv";
ApiConfig.PageSize = 10;
ApiConfig.ApiParameters.Add("ts", "0");
APIConfig.ApiUrl = "/api/v1/sync/fakewithts";
APIConfig.OutputFilePath = @"D:\newsync.csv";
APIConfig.PageSize = 10;
APIConfig.ApiParameters.Add("ts", "0");

//写入csv文件
SdkApi.SyncToCsv<FakeData>(ApiConfig.OutputFilePath);
SdkApi.SyncToCsv<FakeData>(APIConfig.OutputFilePath);
}
}
12 changes: 6 additions & 6 deletions examples/exampleToDb.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@ public class exampleToDb
static void Main(string[] args)
{
//获取token所需要的配置
OauthConfig.ClientId = "yourid";
OauthConfig.ClientSecret = "yoursecret";
OAuth2Config.ClientId = "yourid";
OAuth2Config.ClientSecret = "yoursecret";
//初始化token
OauthToken.InitialOauthCredential(OauthConfig.ClientId, OauthConfig.ClientSecret);
SdkApi.InitOAuth2ClientCredentials(OAuth2Config.ClientId, OAuth2Config.ClientSecret);


//api配置
ApiConfig.ApiUrl = "/api/v1/sync/fakewithts";
ApiConfig.PageSize = 10;
ApiConfig.ApiParameters.Add("ts", "0");
APIConfig.ApiUrl = "/api/v1/sync/fakewithts";
APIConfig.PageSize = 10;
APIConfig.ApiParameters.Add("ts", "0");

/*
"SqlServer": "Server = yourserver; Initial Catalog = yourdb; UID = youruser; PWD = yourpwd",
Expand Down
14 changes: 7 additions & 7 deletions examples/exampleToModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ public class exampleToModel
{
static void Main(string[] args)
{
//获取token所需要的配置
OauthConfig.ClientId = "yourid";
OauthConfig.ClientSecret = "yoursecret";
//获取token所需要的配置
OAuth2Config.ClientId = "yourid";
OAuth2Config.ClientSecret = "yoursecret";
//初始化token
OauthToken.InitialOauthCredential(OauthConfig.ClientId, OauthConfig.ClientSecret);
SdkApi.InitOAuth2ClientCredentials(OAuth2Config.ClientId, OAuth2Config.ClientSecret);


//api配置
ApiConfig.ApiUrl = "/api/v1/sync/fakewithts";
ApiConfig.PageSize = 10;
ApiConfig.ApiParameters.Add("ts", "0");
APIConfig.ApiUrl = "/api/v1/sync/fakewithts";
APIConfig.PageSize = 10;
APIConfig.ApiParameters.Add("ts", "0");

//导入模型,可以根据业务再进行筛查
List<FakeData> list = SdkApi.SyncToModel<FakeData>();
Expand Down
10 changes: 5 additions & 5 deletions src/ConfigClass.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,17 @@
namespace DataSyncSdk
{

public static class OauthConfig
public static class OAuth2Config
{
public static string ClientId { get; set; }
public static string ClientSecret { get; set;}
public static string ClientSecret { get; set; }
public static string DefaultScope { get; set; } = "ECNU";
public static int DefaultTimeOut { get; set; } = 10;
}
public static class ApiConfig
public static class APIConfig
{
public static string DefaultBaseUrl { get; set; } = "https://api.ecnu.edu.cn";
public static string OutputFilePath { get; set; } = "D:\\syncresult.csv";
public static string OutputFilePath { get; set; } = "syncresult.csv";
public static string ApiUrl;
/// <summary>
/// 取api数据时每页取的数据量,默认2000
Expand All @@ -26,7 +26,7 @@ public static class ApiConfig
/// 建议最大不超过100000,且设置为PageSize的整数倍
/// </summary>
public static int BatchSize { get; set; } = 10000;
public static Dictionary<string, string> ApiParameters { get; set; }
public static Dictionary<string, string> ApiParameters { get; set; }
= new Dictionary<string, string>();
}

Expand Down
8 changes: 4 additions & 4 deletions src/HttpHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public static string HttpGetAsync(string url)
{
string retString = string.Empty;

if (!string.IsNullOrEmpty(OauthToken.token))
if (!string.IsNullOrEmpty(SdkApi.token))
{
using (HttpClient httpClient = new HttpClient())
{
httpClient.Timeout = TimeSpan.FromSeconds(OauthConfig.DefaultTimeOut);
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + OauthToken.token);
httpClient.Timeout = TimeSpan.FromSeconds(OAuth2Config.DefaultTimeOut);
httpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + SdkApi.token);
var response = httpClient.GetAsync(url).Result;
if (!response.IsSuccessStatusCode)
{
Expand All @@ -38,7 +38,7 @@ public static string HttpGetAsync(string url)
{
if (retry++ >= 3)
return "";
OauthToken.InitialOauthCredential(OauthConfig.ClientId, OauthConfig.ClientSecret);
SdkApi.InitOAuth2ClientCredentials(OAuth2Config.ClientId, OAuth2Config.ClientSecret);
HttpGetAsync(url);
}
return retString;
Expand Down
79 changes: 0 additions & 79 deletions src/OauthToken.cs

This file was deleted.

Loading