chore: 支持切换云引擎环境

oneRain 2020-06-29 11:28:21 +08:00
parent 41abc2e4ff
commit 08ed1c240c
1 changed files with 17 additions and 0 deletions

View File

@ -7,6 +7,15 @@ namespace LeanCloud.Storage {
/// 云引擎 /// 云引擎
/// </summary> /// </summary>
public static class LCCloud { public static class LCCloud {
private const string PRODUCTION_KEY = "X-LC-Prod";
/// <summary>
/// 是否是生产环境,默认为 true
/// </summary>
public static bool IsProduction {
get; set;
} = true;
/// <summary> /// <summary>
/// 调用云函数 /// 调用云函数
/// </summary> /// </summary>
@ -16,8 +25,12 @@ namespace LeanCloud.Storage {
public static async Task<Dictionary<string, object>> Run(string name, public static async Task<Dictionary<string, object>> Run(string name,
Dictionary<string, object> parameters = null) { Dictionary<string, object> parameters = null) {
string path = $"functions/{name}"; string path = $"functions/{name}";
Dictionary<string, object> headers = new Dictionary<string, object> {
{ PRODUCTION_KEY, IsProduction ? 1 : 0 }
};
object encodeParams = LCEncoder.Encode(parameters); object encodeParams = LCEncoder.Encode(parameters);
Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path,
headers: headers,
data: encodeParams); data: encodeParams);
return response; return response;
} }
@ -30,8 +43,12 @@ namespace LeanCloud.Storage {
/// <returns>返回类型为 LCObject 容器类型</returns> /// <returns>返回类型为 LCObject 容器类型</returns>
public static async Task<object> RPC(string name, object parameters = null) { public static async Task<object> RPC(string name, object parameters = null) {
string path = $"call/{name}"; string path = $"call/{name}";
Dictionary<string, object> headers = new Dictionary<string, object> {
{ PRODUCTION_KEY, IsProduction ? 1 : 0 }
};
object encodeParams = LCEncoder.Encode(parameters); object encodeParams = LCEncoder.Encode(parameters);
Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path, Dictionary<string, object> response = await LCApplication.HttpClient.Post<Dictionary<string, object>>(path,
headers: headers,
data: encodeParams); data: encodeParams);
return LCDecoder.Decode(response["result"]); return LCDecoder.Decode(response["result"]);
} }