From 61a177b0d6f71536e0c94d931b4d777023e1c079 Mon Sep 17 00:00:00 2001 From: oneRain Date: Fri, 9 Apr 2021 14:36:40 +0800 Subject: [PATCH] chore: headers --- Common/Common/Http/LCHttpClient.cs | 24 ++++++++++++++----- Common/Common/LCCore.cs | 8 ------- .../Persistence/PersistenceController.cs | 4 ---- Engine/Public/LCEngine.cs | 2 +- 4 files changed, 19 insertions(+), 19 deletions(-) diff --git a/Common/Common/Http/LCHttpClient.cs b/Common/Common/Http/LCHttpClient.cs index bcd61de..6fba2fa 100644 --- a/Common/Common/Http/LCHttpClient.cs +++ b/Common/Common/Http/LCHttpClient.cs @@ -27,6 +27,8 @@ namespace LeanCloud.Common { private Dictionary>> runtimeHeaderTasks = new Dictionary>>(); + private Dictionary additionalHeaders = new Dictionary(); + 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) { - 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 Get(string path, Dictionary headers = null, Dictionary queryParams = null) { @@ -141,10 +153,10 @@ namespace LeanCloud.Common { return url; } - async Task FillHeaders(HttpRequestHeaders headers, Dictionary additionalHeaders = null) { + async Task FillHeaders(HttpRequestHeaders headers, Dictionary reqHeaders = null) { // 额外 headers - if (additionalHeaders != null) { - foreach (KeyValuePair kv in additionalHeaders) { + if (reqHeaders != null) { + foreach (KeyValuePair 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 kv in LCCore.AdditionalHeaders) { + if (additionalHeaders.Count > 0) { + foreach (KeyValuePair kv in additionalHeaders) { headers.Add(kv.Key, kv.Value); } } diff --git a/Common/Common/LCCore.cs b/Common/Common/LCCore.cs index 4fb54e4..46bd4d6 100644 --- a/Common/Common/LCCore.cs +++ b/Common/Common/LCCore.cs @@ -44,10 +44,6 @@ namespace LeanCloud.Common { get; set; } - internal static Dictionary AdditionalHeaders { - get; - } = new Dictionary(); - 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); - } } } diff --git a/Common/Common/Persistence/PersistenceController.cs b/Common/Common/Persistence/PersistenceController.cs index 285d1c9..46a76c1 100644 --- a/Common/Common/Persistence/PersistenceController.cs +++ b/Common/Common/Persistence/PersistenceController.cs @@ -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; diff --git a/Engine/Public/LCEngine.cs b/Engine/Public/LCEngine.cs index d507196..a71368e 100644 --- a/Engine/Public/LCEngine.cs +++ b/Engine/Public/LCEngine.cs @@ -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()