From dd8406514518d71d50ec49400dceb1dd7f9cad89 Mon Sep 17 00:00:00 2001 From: oneRain Date: Tue, 10 Mar 2020 16:25:46 +0800 Subject: [PATCH] rename: LeanCloud -> LCApplication --- Storage/Internal/Http/LCHttpClient.cs | 8 ++----- Storage/{LeanCloud.cs => LCApplication.cs} | 21 ++++++++++++++++- Storage/LCCloud.cs | 4 ++-- Storage/LCFile.cs | 4 ++-- Storage/LCObject.cs | 14 ++++++------ Storage/LCQuery.cs | 4 ++-- Storage/LCUser.cs | 26 +++++++++++----------- 7 files changed, 48 insertions(+), 33 deletions(-) rename Storage/{LeanCloud.cs => LCApplication.cs} (75%) diff --git a/Storage/Internal/Http/LCHttpClient.cs b/Storage/Internal/Http/LCHttpClient.cs index 238e416..a272771 100644 --- a/Storage/Internal/Http/LCHttpClient.cs +++ b/Storage/Internal/Http/LCHttpClient.cs @@ -22,8 +22,6 @@ namespace LeanCloud.Storage.Internal.Http { readonly string apiVersion; - readonly AppRouter appRouter; - readonly HttpClient client; readonly MD5 md5; @@ -35,10 +33,8 @@ namespace LeanCloud.Storage.Internal.Http { this.sdkVersion = sdkVersion; this.apiVersion = apiVersion; - appRouter = new AppRouter(appId, server); - 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.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); client.DefaultRequestHeaders.Add("X-LC-Id", appId); @@ -175,7 +171,7 @@ namespace LeanCloud.Storage.Internal.Http { } async Task BuildUrl(string path, Dictionary queryParams = null) { - string apiServer = await appRouter.GetApiServer(); + string apiServer = await LCApplication.AppRouter.GetApiServer(); string url = $"{apiServer}/{apiVersion}/{path}"; if (queryParams != null) { IEnumerable queryPairs = queryParams.Select(kv => $"{kv.Key}={kv.Value}"); diff --git a/Storage/LeanCloud.cs b/Storage/LCApplication.cs similarity index 75% rename from Storage/LeanCloud.cs rename to Storage/LCApplication.cs index fd15751..99cad60 100644 --- a/Storage/LeanCloud.cs +++ b/Storage/LCApplication.cs @@ -1,4 +1,5 @@ using System; +using LeanCloud.Common; using LeanCloud.Storage; using LeanCloud.Storage.Internal.Http; @@ -6,17 +7,29 @@ namespace LeanCloud { /// /// LeanCloud 全局接口 /// - public class LeanCloud { + public class LCApplication { // SDK 版本号,用于 User-Agent 统计 internal const string SDKVersion = "0.2.2"; // 接口版本号,用于接口版本管理 internal const string APIVersion = "1.1"; + public static string AppId { + get; private set; + } + + public static string AppKey { + get; private set; + } + public static bool UseProduction { get; set; } + public static LCAppRouter AppRouter { + get; private set; + } + internal static LCHttpClient HttpClient { get; private set; } @@ -28,11 +41,17 @@ namespace LeanCloud { if (string.IsNullOrEmpty(appKey)) { throw new ArgumentException(nameof(appKey)); } + + AppId = appId; + AppKey = appKey; + // 注册 LeanCloud 内部子类化类型 LCObject.RegisterSubclass(LCUser.CLASS_NAME, () => new LCUser()); LCObject.RegisterSubclass(LCRole.CLASS_NAME, () => new LCRole()); LCObject.RegisterSubclass(LCFile.CLASS_NAME, () => new LCFile()); + AppRouter = new LCAppRouter(appId, server); + HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion); } } diff --git a/Storage/LCCloud.cs b/Storage/LCCloud.cs index 904e959..830c8e0 100644 --- a/Storage/LCCloud.cs +++ b/Storage/LCCloud.cs @@ -15,13 +15,13 @@ namespace LeanCloud.Storage { /// public static async Task> Run(string name, Dictionary parameters = null) { string path = $"functions/{name}"; - Dictionary response = await LeanCloud.HttpClient.Post>(path, data: parameters); + Dictionary response = await LCApplication.HttpClient.Post>(path, data: parameters); return response; } public static async Task RPC(string name, Dictionary parameters = null) { string path = $"call/{name}"; - Dictionary response = await LeanCloud.HttpClient.Post>(path, data: parameters); + Dictionary response = await LCApplication.HttpClient.Post>(path, data: parameters); return LCDecoder.Decode(response["result"]); } } diff --git a/Storage/LCFile.cs b/Storage/LCFile.cs index 7902e11..19e1964 100644 --- a/Storage/LCFile.cs +++ b/Storage/LCFile.cs @@ -101,7 +101,7 @@ namespace LeanCloud.Storage { return; } 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") { @@ -117,7 +117,7 @@ namespace LeanCloud.Storage { { "mime_type", MimeType }, { "metaData", MetaData } }; - return await LeanCloud.HttpClient.Post>("fileTokens", data: data); + return await LCApplication.HttpClient.Post>("fileTokens", data: data); } public static LCQuery GetQuery() { diff --git a/Storage/LCObject.cs b/Storage/LCObject.cs index 810ed9b..2b0e3e9 100644 --- a/Storage/LCObject.cs +++ b/Storage/LCObject.cs @@ -311,7 +311,7 @@ namespace LeanCloud.Storage { { "requests", LCEncoder.Encode(requestList) } }; - List> results = await LeanCloud.HttpClient.Post>>("batch", data: data); + List> results = await LCApplication.HttpClient.Post>>("batch", data: data); List resultList = results.Select(item => { if (item.TryGetValue("error", out object error)) { Dictionary err = error as Dictionary; @@ -349,8 +349,8 @@ namespace LeanCloud.Storage { queryParams["where"] = query.BuildWhere(); } Dictionary response = ObjectId == null ? - await LeanCloud.HttpClient.Post>(path, data: LCEncoder.Encode(operationDict) as Dictionary, queryParams: queryParams) : - await LeanCloud.HttpClient.Put>(path, data: LCEncoder.Encode(operationDict) as Dictionary, queryParams: queryParams); + await LCApplication.HttpClient.Post>(path, data: LCEncoder.Encode(operationDict) as Dictionary, queryParams: queryParams) : + await LCApplication.HttpClient.Put>(path, data: LCEncoder.Encode(operationDict) as Dictionary, queryParams: queryParams); LCObjectData data = LCObjectData.Decode(response); Merge(data); return this; @@ -375,7 +375,7 @@ namespace LeanCloud.Storage { return; } string path = $"classes/{ClassName}/{ObjectId}"; - await LeanCloud.HttpClient.Delete(path); + await LCApplication.HttpClient.Delete(path); } public static async Task DeleteAll(List objectList) { @@ -385,7 +385,7 @@ namespace LeanCloud.Storage { IEnumerable objects = objectList.Where(item => item.ObjectId != null); HashSet objectSet = new HashSet(objects); List> 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 { { "path", path }, { "method", "DELETE" } @@ -394,7 +394,7 @@ namespace LeanCloud.Storage { Dictionary data = new Dictionary { { "requests", LCEncoder.Encode(requestList) } }; - await LeanCloud.HttpClient.Post>("batch", data: data); + await LCApplication.HttpClient.Post>("batch", data: data); } public async Task Fetch(IEnumerable keys = null, IEnumerable includes = null) { @@ -406,7 +406,7 @@ namespace LeanCloud.Storage { queryParams["include"] = string.Join(",", includes); } string path = $"classes/{ClassName}/{ObjectId}"; - Dictionary response = await LeanCloud.HttpClient.Get>(path, queryParams: queryParams); + Dictionary response = await LCApplication.HttpClient.Get>(path, queryParams: queryParams); LCObjectData objectData = LCObjectData.Decode(response); Merge(objectData); return this; diff --git a/Storage/LCQuery.cs b/Storage/LCQuery.cs index dce8148..78ae9df 100644 --- a/Storage/LCQuery.cs +++ b/Storage/LCQuery.cs @@ -274,7 +274,7 @@ namespace LeanCloud.Storage { Dictionary parameters = BuildParams(); parameters["limit"] = 0; parameters["count"] = 1; - Dictionary ret = await LeanCloud.HttpClient.Get>(path, queryParams: parameters); + Dictionary ret = await LCApplication.HttpClient.Get>(path, queryParams: parameters); return (int)ret["count"]; } @@ -297,7 +297,7 @@ namespace LeanCloud.Storage { public async Task> Find() { string path = $"classes/{ClassName}"; Dictionary parameters = BuildParams(); - Dictionary response = await LeanCloud.HttpClient.Get>(path, queryParams: parameters); + Dictionary response = await LCApplication.HttpClient.Get>(path, queryParams: parameters); List results = response["results"] as List; List list = new List(); foreach (object item in results) { diff --git a/Storage/LCUser.cs b/Storage/LCUser.cs index b8355a6..ae24f13 100644 --- a/Storage/LCUser.cs +++ b/Storage/LCUser.cs @@ -116,7 +116,7 @@ namespace LeanCloud.Storage { Dictionary data = new Dictionary { { "mobilePhoneNumber", mobile } }; - await LeanCloud.HttpClient.Post>("requestLoginSmsCode", data: data); + await LCApplication.HttpClient.Post>("requestLoginSmsCode", data: data); } /// @@ -136,7 +136,7 @@ namespace LeanCloud.Storage { { "mobilePhoneNumber", mobile }, { "smsCode", code } }; - Dictionary response = await LeanCloud.HttpClient.Post>("usersByMobilePhone", data: data); + Dictionary response = await LCApplication.HttpClient.Post>("usersByMobilePhone", data: data); LCObjectData objectData = LCObjectData.Decode(response); currentUser = new LCUser(objectData); return currentUser; @@ -346,7 +346,7 @@ namespace LeanCloud.Storage { Dictionary data = new Dictionary { { "email", email } }; - await LeanCloud.HttpClient.Post>("requestEmailVerify", data: data); + await LCApplication.HttpClient.Post>("requestEmailVerify", data: data); } /// @@ -361,7 +361,7 @@ namespace LeanCloud.Storage { Dictionary data = new Dictionary { { "mobilePhoneNumber", mobile } }; - await LeanCloud.HttpClient.Post>("requestMobilePhoneVerify", data: data); + await LCApplication.HttpClient.Post>("requestMobilePhoneVerify", data: data); } /// @@ -381,7 +381,7 @@ namespace LeanCloud.Storage { Dictionary data = new Dictionary { { "mobilePhoneNumber", mobile } }; - await LeanCloud.HttpClient.Post>(path, data: data); + await LCApplication.HttpClient.Post>(path, data: data); } /// @@ -396,7 +396,7 @@ namespace LeanCloud.Storage { Dictionary headers = new Dictionary { { "X-LC-Session", sessionToken } }; - Dictionary response = await LeanCloud.HttpClient.Get>("users/me", + Dictionary response = await LCApplication.HttpClient.Get>("users/me", headers: headers); LCObjectData objectData = LCObjectData.Decode(response); currentUser = new LCUser(objectData); @@ -415,7 +415,7 @@ namespace LeanCloud.Storage { Dictionary data = new Dictionary { { "email", email } }; - await LeanCloud.HttpClient.Post>("requestPasswordReset", + await LCApplication.HttpClient.Post>("requestPasswordReset", data: data); } @@ -431,7 +431,7 @@ namespace LeanCloud.Storage { Dictionary data = new Dictionary { { "mobilePhoneNumber", mobile } }; - await LeanCloud.HttpClient.Post>("requestPasswordResetBySmsCode", + await LCApplication.HttpClient.Post>("requestPasswordResetBySmsCode", data: data); } @@ -456,7 +456,7 @@ namespace LeanCloud.Storage { { "mobilePhoneNumber", mobile }, { "password", newPassword } }; - await LeanCloud.HttpClient.Put>($"resetPasswordBySmsCode/{code}", + await LCApplication.HttpClient.Put>($"resetPasswordBySmsCode/{code}", data: data); } @@ -477,7 +477,7 @@ namespace LeanCloud.Storage { { "old_password", oldPassword }, { "new_password", newPassword } }; - Dictionary response = await LeanCloud.HttpClient.Put>( + Dictionary response = await LCApplication.HttpClient.Put>( $"users/{ObjectId}/updatePassword", data:data); LCObjectData objectData = LCObjectData.Decode(response); Merge(objectData); @@ -502,7 +502,7 @@ namespace LeanCloud.Storage { return false; } try { - await LeanCloud.HttpClient.Get>("users/me"); + await LCApplication.HttpClient.Get>("users/me"); return true; } catch (Exception) { return false; @@ -525,7 +525,7 @@ namespace LeanCloud.Storage { } static async Task Login(Dictionary data) { - Dictionary response = await LeanCloud.HttpClient.Post>("login", data: data); + Dictionary response = await LCApplication.HttpClient.Post>("login", data: data); LCObjectData objectData = LCObjectData.Decode(response); currentUser = new LCUser(objectData); return currentUser; @@ -536,7 +536,7 @@ namespace LeanCloud.Storage { { authType, data } }; string path = failOnNotExist ? "users?failOnNotExist=true" : "users"; - Dictionary response = await LeanCloud.HttpClient.Post>(path, data: new Dictionary { + Dictionary response = await LCApplication.HttpClient.Post>(path, data: new Dictionary { { "authData", authData } }); LCObjectData objectData = LCObjectData.Decode(response);