chore: headers

oneRain 2021-04-09 14:36:40 +08:00
parent 71e6699d0a
commit 61a177b0d6
4 changed files with 19 additions and 19 deletions

View File

@ -27,6 +27,8 @@ namespace LeanCloud.Common {
private Dictionary<string, Func<Task<string>>> runtimeHeaderTasks = new Dictionary<string, Func<Task<string>>>();
private Dictionary<string, string> additionalHeaders = new Dictionary<string, string>();
public LCHttpClient(string appId, string appKey, string server, string sdkVersion, string apiVersion) {
this.appId = appId;
this.appKey = appKey;
@ -44,7 +46,7 @@ namespace LeanCloud.Common {
}
public void AddRuntimeHeaderTask(string key, Func<Task<string>> task) {
if (key == null) {
if (string.IsNullOrEmpty(key)) {
return;
}
if (task == null) {
@ -53,6 +55,16 @@ namespace LeanCloud.Common {
runtimeHeaderTasks[key] = task;
}
public void AddAddtionalHeader(string key, string value) {
if (string.IsNullOrEmpty(key)) {
return;
}
if (string.IsNullOrEmpty(value)) {
return;
}
additionalHeaders[key] = value;
}
public Task<T> Get<T>(string path,
Dictionary<string, object> headers = null,
Dictionary<string, object> queryParams = null) {
@ -141,10 +153,10 @@ namespace LeanCloud.Common {
return url;
}
async Task FillHeaders(HttpRequestHeaders headers, Dictionary<string, object> additionalHeaders = null) {
async Task FillHeaders(HttpRequestHeaders headers, Dictionary<string, object> reqHeaders = null) {
// 额外 headers
if (additionalHeaders != null) {
foreach (KeyValuePair<string, object> kv in additionalHeaders) {
if (reqHeaders != null) {
foreach (KeyValuePair<string, object> kv in reqHeaders) {
headers.Add(kv.Key, kv.Value.ToString());
}
}
@ -159,8 +171,8 @@ namespace LeanCloud.Common {
string sign = $"{hash},{timestamp}";
headers.Add("X-LC-Sign", sign);
}
if (LCCore.AdditionalHeaders.Count > 0) {
foreach (KeyValuePair<string, string> kv in LCCore.AdditionalHeaders) {
if (additionalHeaders.Count > 0) {
foreach (KeyValuePair<string, string> kv in additionalHeaders) {
headers.Add(kv.Key, kv.Value);
}
}

View File

@ -44,10 +44,6 @@ namespace LeanCloud.Common {
get; set;
}
internal static Dictionary<string, string> AdditionalHeaders {
get;
} = new Dictionary<string, string>();
public static void Initialize(string appId,
string appKey,
string server = null,
@ -67,9 +63,5 @@ namespace LeanCloud.Common {
HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion);
}
public static void AddHeader(string key, string value) {
AdditionalHeaders.Add(key, value);
}
}
}

View File

@ -19,8 +19,6 @@ namespace LeanCloud.Common {
}
string path = GetFileFullPath(filename);
LCLogger.Debug($"WRITE: {path}");
LCLogger.Debug($"WRITE: {text}");
using (FileStream fs = IOFile.OpenWrite(path)) {
byte[] buffer = Encoding.UTF8.GetBytes(text);
await fs.WriteAsync(buffer, 0, buffer.Length);
@ -33,7 +31,6 @@ namespace LeanCloud.Common {
}
string path = GetFileFullPath(filename);
LCLogger.Debug($"READ: {path}");
if (IOFile.Exists(path)) {
string text;
using (FileStream fs = IOFile.OpenRead(path)) {
@ -41,7 +38,6 @@ namespace LeanCloud.Common {
await fs.ReadAsync(buffer, 0, (int)fs.Length);
text = Encoding.UTF8.GetString(buffer);
}
LCLogger.Debug($"READ: {text}");
return text;
}
return null;

View File

@ -99,7 +99,7 @@ namespace LeanCloud.Engine {
LCApplication.Initialize(Environment.GetEnvironmentVariable("LEANCLOUD_APP_ID"),
Environment.GetEnvironmentVariable("LEANCLOUD_APP_KEY"),
Environment.GetEnvironmentVariable("LEANCLOUD_API_SERVER"));
LCCore.AddHeader(LCHookKeyName, Environment.GetEnvironmentVariable("LEANCLOUD_APP_HOOK_KEY"));
LCCore.HttpClient.AddAddtionalHeader(LCHookKeyName, Environment.GetEnvironmentVariable("LEANCLOUD_APP_HOOK_KEY"));
Assembly assembly = Assembly.GetCallingAssembly();
ClassHooks = assembly.GetTypes()