rename: LeanCloud -> LCApplication

oneRain 2020-03-10 16:25:46 +08:00
parent 392d7a4ee4
commit dd84065145
7 changed files with 48 additions and 33 deletions

View File

@ -22,8 +22,6 @@ namespace LeanCloud.Storage.Internal.Http {
readonly string apiVersion; readonly string apiVersion;
readonly AppRouter appRouter;
readonly HttpClient client; readonly HttpClient client;
readonly MD5 md5; readonly MD5 md5;
@ -35,10 +33,8 @@ namespace LeanCloud.Storage.Internal.Http {
this.sdkVersion = sdkVersion; this.sdkVersion = sdkVersion;
this.apiVersion = apiVersion; this.apiVersion = apiVersion;
appRouter = new AppRouter(appId, server);
client = new HttpClient(); client = new HttpClient();
ProductHeaderValue product = new ProductHeaderValue("LeanCloud-CSharp-SDK", LeanCloud.SDKVersion); ProductHeaderValue product = new ProductHeaderValue("LeanCloud-CSharp-SDK", sdkVersion);
client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(product)); client.DefaultRequestHeaders.UserAgent.Add(new ProductInfoHeaderValue(product));
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
client.DefaultRequestHeaders.Add("X-LC-Id", appId); client.DefaultRequestHeaders.Add("X-LC-Id", appId);
@ -175,7 +171,7 @@ namespace LeanCloud.Storage.Internal.Http {
} }
async Task<string> BuildUrl(string path, Dictionary<string, object> queryParams = null) { async Task<string> BuildUrl(string path, Dictionary<string, object> queryParams = null) {
string apiServer = await appRouter.GetApiServer(); string apiServer = await LCApplication.AppRouter.GetApiServer();
string url = $"{apiServer}/{apiVersion}/{path}"; string url = $"{apiServer}/{apiVersion}/{path}";
if (queryParams != null) { if (queryParams != null) {
IEnumerable<string> queryPairs = queryParams.Select(kv => $"{kv.Key}={kv.Value}"); IEnumerable<string> queryPairs = queryParams.Select(kv => $"{kv.Key}={kv.Value}");

View File

@ -1,4 +1,5 @@
using System; using System;
using LeanCloud.Common;
using LeanCloud.Storage; using LeanCloud.Storage;
using LeanCloud.Storage.Internal.Http; using LeanCloud.Storage.Internal.Http;
@ -6,17 +7,29 @@ namespace LeanCloud {
/// <summary> /// <summary>
/// LeanCloud 全局接口 /// LeanCloud 全局接口
/// </summary> /// </summary>
public class LeanCloud { public class LCApplication {
// SDK 版本号,用于 User-Agent 统计 // SDK 版本号,用于 User-Agent 统计
internal const string SDKVersion = "0.2.2"; internal const string SDKVersion = "0.2.2";
// 接口版本号,用于接口版本管理 // 接口版本号,用于接口版本管理
internal const string APIVersion = "1.1"; internal const string APIVersion = "1.1";
public static string AppId {
get; private set;
}
public static string AppKey {
get; private set;
}
public static bool UseProduction { public static bool UseProduction {
get; set; get; set;
} }
public static LCAppRouter AppRouter {
get; private set;
}
internal static LCHttpClient HttpClient { internal static LCHttpClient HttpClient {
get; private set; get; private set;
} }
@ -28,11 +41,17 @@ namespace LeanCloud {
if (string.IsNullOrEmpty(appKey)) { if (string.IsNullOrEmpty(appKey)) {
throw new ArgumentException(nameof(appKey)); throw new ArgumentException(nameof(appKey));
} }
AppId = appId;
AppKey = appKey;
// 注册 LeanCloud 内部子类化类型 // 注册 LeanCloud 内部子类化类型
LCObject.RegisterSubclass<LCUser>(LCUser.CLASS_NAME, () => new LCUser()); LCObject.RegisterSubclass<LCUser>(LCUser.CLASS_NAME, () => new LCUser());
LCObject.RegisterSubclass<LCRole>(LCRole.CLASS_NAME, () => new LCRole()); LCObject.RegisterSubclass<LCRole>(LCRole.CLASS_NAME, () => new LCRole());
LCObject.RegisterSubclass<LCFile>(LCFile.CLASS_NAME, () => new LCFile()); LCObject.RegisterSubclass<LCFile>(LCFile.CLASS_NAME, () => new LCFile());
AppRouter = new LCAppRouter(appId, server);
HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion); HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion);
} }
} }

View File

@ -15,13 +15,13 @@ namespace LeanCloud.Storage {
/// <returns></returns> /// <returns></returns>
public static async Task<Dictionary<string, object>> Run(string name, Dictionary<string, object> parameters = null) { public static async Task<Dictionary<string, object>> Run(string name, Dictionary<string, object> parameters = null) {
string path = $"functions/{name}"; string path = $"functions/{name}";
Dictionary<string, object> response = await LeanCloud.HttpClient.Post<Dictionary<string, object>>(path, data: parameters); Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: parameters);
return response; return response;
} }
public static async Task<object> RPC(string name, Dictionary<string, object> parameters = null) { public static async Task<object> RPC(string name, Dictionary<string, object> parameters = null) {
string path = $"call/{name}"; string path = $"call/{name}";
Dictionary<string, object> response = await LeanCloud.HttpClient.Post<Dictionary<string, object>>(path, data: parameters); Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: parameters);
return LCDecoder.Decode(response["result"]); return LCDecoder.Decode(response["result"]);
} }
} }

View File

@ -101,7 +101,7 @@ namespace LeanCloud.Storage {
return; return;
} }
string path = $"files/{ObjectId}"; string path = $"files/{ObjectId}";
await LeanCloud.HttpClient.Delete(path); await LCApplication.HttpClient.Delete(path);
} }
public string GetThumbnailUrl(int width, int height, int quality = 100, bool scaleToFit = true, string format = "png") { public string GetThumbnailUrl(int width, int height, int quality = 100, bool scaleToFit = true, string format = "png") {
@ -117,7 +117,7 @@ namespace LeanCloud.Storage {
{ "mime_type", MimeType }, { "mime_type", MimeType },
{ "metaData", MetaData } { "metaData", MetaData }
}; };
return await LeanCloud.HttpClient.Post<Dictionary<string, object>>("fileTokens", data: data); return await LCApplication.HttpClient.Post<Dictionary<string, object>>("fileTokens", data: data);
} }
public static LCQuery<LCFile> GetQuery() { public static LCQuery<LCFile> GetQuery() {

View File

@ -311,7 +311,7 @@ namespace LeanCloud.Storage {
{ "requests", LCEncoder.Encode(requestList) } { "requests", LCEncoder.Encode(requestList) }
}; };
List<Dictionary<string, object>> results = await LeanCloud.HttpClient.Post<List<Dictionary<string, object>>>("batch", data: data); List<Dictionary<string, object>> results = await LCApplication.HttpClient.Post<List<Dictionary<string, object>>>("batch", data: data);
List<LCObjectData> resultList = results.Select(item => { List<LCObjectData> resultList = results.Select(item => {
if (item.TryGetValue("error", out object error)) { if (item.TryGetValue("error", out object error)) {
Dictionary<string, object> err = error as Dictionary<string, object>; Dictionary<string, object> err = error as Dictionary<string, object>;
@ -349,8 +349,8 @@ namespace LeanCloud.Storage {
queryParams["where"] = query.BuildWhere(); queryParams["where"] = query.BuildWhere();
} }
Dictionary<string, object> response = ObjectId == null ? Dictionary<string, object> response = ObjectId == null ?
await LeanCloud.HttpClient.Post<Dictionary<string, object>>(path, data: LCEncoder.Encode(operationDict) as Dictionary<string, object>, queryParams: queryParams) : await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: LCEncoder.Encode(operationDict) as Dictionary<string, object>, queryParams: queryParams) :
await LeanCloud.HttpClient.Put<Dictionary<string, object>>(path, data: LCEncoder.Encode(operationDict) as Dictionary<string, object>, queryParams: queryParams); await LCApplication.HttpClient.Put<Dictionary<string, object>>(path, data: LCEncoder.Encode(operationDict) as Dictionary<string, object>, queryParams: queryParams);
LCObjectData data = LCObjectData.Decode(response); LCObjectData data = LCObjectData.Decode(response);
Merge(data); Merge(data);
return this; return this;
@ -375,7 +375,7 @@ namespace LeanCloud.Storage {
return; return;
} }
string path = $"classes/{ClassName}/{ObjectId}"; string path = $"classes/{ClassName}/{ObjectId}";
await LeanCloud.HttpClient.Delete(path); await LCApplication.HttpClient.Delete(path);
} }
public static async Task DeleteAll(List<LCObject> objectList) { public static async Task DeleteAll(List<LCObject> objectList) {
@ -385,7 +385,7 @@ namespace LeanCloud.Storage {
IEnumerable<LCObject> objects = objectList.Where(item => item.ObjectId != null); IEnumerable<LCObject> objects = objectList.Where(item => item.ObjectId != null);
HashSet<LCObject> objectSet = new HashSet<LCObject>(objects); HashSet<LCObject> objectSet = new HashSet<LCObject>(objects);
List<Dictionary<string, object>> requestList = objectSet.Select(item => { List<Dictionary<string, object>> requestList = objectSet.Select(item => {
string path = $"/{LeanCloud.APIVersion}/classes/{item.ClassName}/{item.ObjectId}"; string path = $"/{LCApplication.APIVersion}/classes/{item.ClassName}/{item.ObjectId}";
return new Dictionary<string, object> { return new Dictionary<string, object> {
{ "path", path }, { "path", path },
{ "method", "DELETE" } { "method", "DELETE" }
@ -394,7 +394,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> { Dictionary<string, object> data = new Dictionary<string, object> {
{ "requests", LCEncoder.Encode(requestList) } { "requests", LCEncoder.Encode(requestList) }
}; };
await LeanCloud.HttpClient.Post<List<object>>("batch", data: data); await LCApplication.HttpClient.Post<List<object>>("batch", data: data);
} }
public async Task<LCObject> Fetch(IEnumerable<string> keys = null, IEnumerable<string> includes = null) { public async Task<LCObject> Fetch(IEnumerable<string> keys = null, IEnumerable<string> includes = null) {
@ -406,7 +406,7 @@ namespace LeanCloud.Storage {
queryParams["include"] = string.Join(",", includes); queryParams["include"] = string.Join(",", includes);
} }
string path = $"classes/{ClassName}/{ObjectId}"; string path = $"classes/{ClassName}/{ObjectId}";
Dictionary<string, object> response = await LeanCloud.HttpClient.Get<Dictionary<string, object>>(path, queryParams: queryParams); Dictionary<string, object> response = await LCApplication.HttpClient.Get<Dictionary<string, object>>(path, queryParams: queryParams);
LCObjectData objectData = LCObjectData.Decode(response); LCObjectData objectData = LCObjectData.Decode(response);
Merge(objectData); Merge(objectData);
return this; return this;

View File

@ -274,7 +274,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> parameters = BuildParams(); Dictionary<string, object> parameters = BuildParams();
parameters["limit"] = 0; parameters["limit"] = 0;
parameters["count"] = 1; parameters["count"] = 1;
Dictionary<string, object> ret = await LeanCloud.HttpClient.Get<Dictionary<string, object>>(path, queryParams: parameters); Dictionary<string, object> ret = await LCApplication.HttpClient.Get<Dictionary<string, object>>(path, queryParams: parameters);
return (int)ret["count"]; return (int)ret["count"];
} }
@ -297,7 +297,7 @@ namespace LeanCloud.Storage {
public async Task<List<T>> Find() { public async Task<List<T>> Find() {
string path = $"classes/{ClassName}"; string path = $"classes/{ClassName}";
Dictionary<string, object> parameters = BuildParams(); Dictionary<string, object> parameters = BuildParams();
Dictionary<string, object> response = await LeanCloud.HttpClient.Get<Dictionary<string, object>>(path, queryParams: parameters); Dictionary<string, object> response = await LCApplication.HttpClient.Get<Dictionary<string, object>>(path, queryParams: parameters);
List<object> results = response["results"] as List<object>; List<object> results = response["results"] as List<object>;
List<T> list = new List<T>(); List<T> list = new List<T>();
foreach (object item in results) { foreach (object item in results) {

View File

@ -116,7 +116,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> { Dictionary<string, object> data = new Dictionary<string, object> {
{ "mobilePhoneNumber", mobile } { "mobilePhoneNumber", mobile }
}; };
await LeanCloud.HttpClient.Post<Dictionary<string, object>>("requestLoginSmsCode", data: data); await LCApplication.HttpClient.Post<Dictionary<string, object>>("requestLoginSmsCode", data: data);
} }
/// <summary> /// <summary>
@ -136,7 +136,7 @@ namespace LeanCloud.Storage {
{ "mobilePhoneNumber", mobile }, { "mobilePhoneNumber", mobile },
{ "smsCode", code } { "smsCode", code }
}; };
Dictionary<string, object> response = await LeanCloud.HttpClient.Post<Dictionary<string, object>>("usersByMobilePhone", data: data); Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>("usersByMobilePhone", data: data);
LCObjectData objectData = LCObjectData.Decode(response); LCObjectData objectData = LCObjectData.Decode(response);
currentUser = new LCUser(objectData); currentUser = new LCUser(objectData);
return currentUser; return currentUser;
@ -346,7 +346,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> { Dictionary<string, object> data = new Dictionary<string, object> {
{ "email", email } { "email", email }
}; };
await LeanCloud.HttpClient.Post<Dictionary<string, object>>("requestEmailVerify", data: data); await LCApplication.HttpClient.Post<Dictionary<string, object>>("requestEmailVerify", data: data);
} }
/// <summary> /// <summary>
@ -361,7 +361,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> { Dictionary<string, object> data = new Dictionary<string, object> {
{ "mobilePhoneNumber", mobile } { "mobilePhoneNumber", mobile }
}; };
await LeanCloud.HttpClient.Post<Dictionary<string, object>>("requestMobilePhoneVerify", data: data); await LCApplication.HttpClient.Post<Dictionary<string, object>>("requestMobilePhoneVerify", data: data);
} }
/// <summary> /// <summary>
@ -381,7 +381,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> { Dictionary<string, object> data = new Dictionary<string, object> {
{ "mobilePhoneNumber", mobile } { "mobilePhoneNumber", mobile }
}; };
await LeanCloud.HttpClient.Post<Dictionary<string, object>>(path, data: data); await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: data);
} }
/// <summary> /// <summary>
@ -396,7 +396,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> headers = new Dictionary<string, object> { Dictionary<string, object> headers = new Dictionary<string, object> {
{ "X-LC-Session", sessionToken } { "X-LC-Session", sessionToken }
}; };
Dictionary<string, object> response = await LeanCloud.HttpClient.Get<Dictionary<string, object>>("users/me", Dictionary<string, object> response = await LCApplication.HttpClient.Get<Dictionary<string, object>>("users/me",
headers: headers); headers: headers);
LCObjectData objectData = LCObjectData.Decode(response); LCObjectData objectData = LCObjectData.Decode(response);
currentUser = new LCUser(objectData); currentUser = new LCUser(objectData);
@ -415,7 +415,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> { Dictionary<string, object> data = new Dictionary<string, object> {
{ "email", email } { "email", email }
}; };
await LeanCloud.HttpClient.Post<Dictionary<string, object>>("requestPasswordReset", await LCApplication.HttpClient.Post<Dictionary<string, object>>("requestPasswordReset",
data: data); data: data);
} }
@ -431,7 +431,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> { Dictionary<string, object> data = new Dictionary<string, object> {
{ "mobilePhoneNumber", mobile } { "mobilePhoneNumber", mobile }
}; };
await LeanCloud.HttpClient.Post<Dictionary<string, object>>("requestPasswordResetBySmsCode", await LCApplication.HttpClient.Post<Dictionary<string, object>>("requestPasswordResetBySmsCode",
data: data); data: data);
} }
@ -456,7 +456,7 @@ namespace LeanCloud.Storage {
{ "mobilePhoneNumber", mobile }, { "mobilePhoneNumber", mobile },
{ "password", newPassword } { "password", newPassword }
}; };
await LeanCloud.HttpClient.Put<Dictionary<string, object>>($"resetPasswordBySmsCode/{code}", await LCApplication.HttpClient.Put<Dictionary<string, object>>($"resetPasswordBySmsCode/{code}",
data: data); data: data);
} }
@ -477,7 +477,7 @@ namespace LeanCloud.Storage {
{ "old_password", oldPassword }, { "old_password", oldPassword },
{ "new_password", newPassword } { "new_password", newPassword }
}; };
Dictionary<string, object> response = await LeanCloud.HttpClient.Put<Dictionary<string, object>>( Dictionary<string, object> response = await LCApplication.HttpClient.Put<Dictionary<string, object>>(
$"users/{ObjectId}/updatePassword", data:data); $"users/{ObjectId}/updatePassword", data:data);
LCObjectData objectData = LCObjectData.Decode(response); LCObjectData objectData = LCObjectData.Decode(response);
Merge(objectData); Merge(objectData);
@ -502,7 +502,7 @@ namespace LeanCloud.Storage {
return false; return false;
} }
try { try {
await LeanCloud.HttpClient.Get<Dictionary<string, object>>("users/me"); await LCApplication.HttpClient.Get<Dictionary<string, object>>("users/me");
return true; return true;
} catch (Exception) { } catch (Exception) {
return false; return false;
@ -525,7 +525,7 @@ namespace LeanCloud.Storage {
} }
static async Task<LCUser> Login(Dictionary<string, object> data) { static async Task<LCUser> Login(Dictionary<string, object> data) {
Dictionary<string, object> response = await LeanCloud.HttpClient.Post<Dictionary<string, object>>("login", data: data); Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>("login", data: data);
LCObjectData objectData = LCObjectData.Decode(response); LCObjectData objectData = LCObjectData.Decode(response);
currentUser = new LCUser(objectData); currentUser = new LCUser(objectData);
return currentUser; return currentUser;
@ -536,7 +536,7 @@ namespace LeanCloud.Storage {
{ authType, data } { authType, data }
}; };
string path = failOnNotExist ? "users?failOnNotExist=true" : "users"; string path = failOnNotExist ? "users?failOnNotExist=true" : "users";
Dictionary<string, object> response = await LeanCloud.HttpClient.Post<Dictionary<string, object>>(path, data: new Dictionary<string, object> { Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: new Dictionary<string, object> {
{ "authData", authData } { "authData", authData }
}); });
LCObjectData objectData = LCObjectData.Decode(response); LCObjectData objectData = LCObjectData.Decode(response);