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 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<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}";
if (queryParams != null) {
IEnumerable<string> queryPairs = queryParams.Select(kv => $"{kv.Key}={kv.Value}");

View File

@ -1,4 +1,5 @@
using System;
using LeanCloud.Common;
using LeanCloud.Storage;
using LeanCloud.Storage.Internal.Http;
@ -6,17 +7,29 @@ namespace LeanCloud {
/// <summary>
/// LeanCloud 全局接口
/// </summary>
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>(LCUser.CLASS_NAME, () => new LCUser());
LCObject.RegisterSubclass<LCRole>(LCRole.CLASS_NAME, () => new LCRole());
LCObject.RegisterSubclass<LCFile>(LCFile.CLASS_NAME, () => new LCFile());
AppRouter = new LCAppRouter(appId, server);
HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion);
}
}

View File

@ -15,13 +15,13 @@ namespace LeanCloud.Storage {
/// <returns></returns>
public static async Task<Dictionary<string, object>> Run(string name, Dictionary<string, object> parameters = null) {
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;
}
public static async Task<object> RPC(string name, Dictionary<string, object> parameters = null) {
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"]);
}
}

View File

@ -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<Dictionary<string, object>>("fileTokens", data: data);
return await LCApplication.HttpClient.Post<Dictionary<string, object>>("fileTokens", data: data);
}
public static LCQuery<LCFile> GetQuery() {

View File

@ -311,7 +311,7 @@ namespace LeanCloud.Storage {
{ "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 => {
if (item.TryGetValue("error", out object error)) {
Dictionary<string, object> err = error as Dictionary<string, object>;
@ -349,8 +349,8 @@ namespace LeanCloud.Storage {
queryParams["where"] = query.BuildWhere();
}
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 LeanCloud.HttpClient.Put<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 LCApplication.HttpClient.Put<Dictionary<string, object>>(path, data: LCEncoder.Encode(operationDict) as Dictionary<string, object>, 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<LCObject> objectList) {
@ -385,7 +385,7 @@ namespace LeanCloud.Storage {
IEnumerable<LCObject> objects = objectList.Where(item => item.ObjectId != null);
HashSet<LCObject> objectSet = new HashSet<LCObject>(objects);
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> {
{ "path", path },
{ "method", "DELETE" }
@ -394,7 +394,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> {
{ "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) {
@ -406,7 +406,7 @@ namespace LeanCloud.Storage {
queryParams["include"] = string.Join(",", includes);
}
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);
Merge(objectData);
return this;

View File

@ -274,7 +274,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> parameters = BuildParams();
parameters["limit"] = 0;
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"];
}
@ -297,7 +297,7 @@ namespace LeanCloud.Storage {
public async Task<List<T>> Find() {
string path = $"classes/{ClassName}";
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<T> list = new List<T>();
foreach (object item in results) {

View File

@ -116,7 +116,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> {
{ "mobilePhoneNumber", mobile }
};
await LeanCloud.HttpClient.Post<Dictionary<string, object>>("requestLoginSmsCode", data: data);
await LCApplication.HttpClient.Post<Dictionary<string, object>>("requestLoginSmsCode", data: data);
}
/// <summary>
@ -136,7 +136,7 @@ namespace LeanCloud.Storage {
{ "mobilePhoneNumber", mobile },
{ "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);
currentUser = new LCUser(objectData);
return currentUser;
@ -346,7 +346,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> {
{ "email", email }
};
await LeanCloud.HttpClient.Post<Dictionary<string, object>>("requestEmailVerify", data: data);
await LCApplication.HttpClient.Post<Dictionary<string, object>>("requestEmailVerify", data: data);
}
/// <summary>
@ -361,7 +361,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> {
{ "mobilePhoneNumber", mobile }
};
await LeanCloud.HttpClient.Post<Dictionary<string, object>>("requestMobilePhoneVerify", data: data);
await LCApplication.HttpClient.Post<Dictionary<string, object>>("requestMobilePhoneVerify", data: data);
}
/// <summary>
@ -381,7 +381,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> {
{ "mobilePhoneNumber", mobile }
};
await LeanCloud.HttpClient.Post<Dictionary<string, object>>(path, data: data);
await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, data: data);
}
/// <summary>
@ -396,7 +396,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> headers = new Dictionary<string, object> {
{ "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);
LCObjectData objectData = LCObjectData.Decode(response);
currentUser = new LCUser(objectData);
@ -415,7 +415,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> {
{ "email", email }
};
await LeanCloud.HttpClient.Post<Dictionary<string, object>>("requestPasswordReset",
await LCApplication.HttpClient.Post<Dictionary<string, object>>("requestPasswordReset",
data: data);
}
@ -431,7 +431,7 @@ namespace LeanCloud.Storage {
Dictionary<string, object> data = new Dictionary<string, object> {
{ "mobilePhoneNumber", mobile }
};
await LeanCloud.HttpClient.Post<Dictionary<string, object>>("requestPasswordResetBySmsCode",
await LCApplication.HttpClient.Post<Dictionary<string, object>>("requestPasswordResetBySmsCode",
data: data);
}
@ -456,7 +456,7 @@ namespace LeanCloud.Storage {
{ "mobilePhoneNumber", mobile },
{ "password", newPassword }
};
await LeanCloud.HttpClient.Put<Dictionary<string, object>>($"resetPasswordBySmsCode/{code}",
await LCApplication.HttpClient.Put<Dictionary<string, object>>($"resetPasswordBySmsCode/{code}",
data: data);
}
@ -477,7 +477,7 @@ namespace LeanCloud.Storage {
{ "old_password", oldPassword },
{ "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);
LCObjectData objectData = LCObjectData.Decode(response);
Merge(objectData);
@ -502,7 +502,7 @@ namespace LeanCloud.Storage {
return false;
}
try {
await LeanCloud.HttpClient.Get<Dictionary<string, object>>("users/me");
await LCApplication.HttpClient.Get<Dictionary<string, object>>("users/me");
return true;
} catch (Exception) {
return false;
@ -525,7 +525,7 @@ namespace LeanCloud.Storage {
}
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);
currentUser = new LCUser(objectData);
return currentUser;
@ -536,7 +536,7 @@ namespace LeanCloud.Storage {
{ authType, data }
};
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 }
});
LCObjectData objectData = LCObjectData.Decode(response);