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

View File

@ -44,10 +44,6 @@ namespace LeanCloud.Common {
get; set; get; set;
} }
internal static Dictionary<string, string> AdditionalHeaders {
get;
} = new Dictionary<string, string>();
public static void Initialize(string appId, public static void Initialize(string appId,
string appKey, string appKey,
string server = null, string server = null,
@ -67,9 +63,5 @@ namespace LeanCloud.Common {
HttpClient = new LCHttpClient(appId, appKey, server, SDKVersion, APIVersion); 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); string path = GetFileFullPath(filename);
LCLogger.Debug($"WRITE: {path}");
LCLogger.Debug($"WRITE: {text}");
using (FileStream fs = IOFile.OpenWrite(path)) { using (FileStream fs = IOFile.OpenWrite(path)) {
byte[] buffer = Encoding.UTF8.GetBytes(text); byte[] buffer = Encoding.UTF8.GetBytes(text);
await fs.WriteAsync(buffer, 0, buffer.Length); await fs.WriteAsync(buffer, 0, buffer.Length);
@ -33,7 +31,6 @@ namespace LeanCloud.Common {
} }
string path = GetFileFullPath(filename); string path = GetFileFullPath(filename);
LCLogger.Debug($"READ: {path}");
if (IOFile.Exists(path)) { if (IOFile.Exists(path)) {
string text; string text;
using (FileStream fs = IOFile.OpenRead(path)) { using (FileStream fs = IOFile.OpenRead(path)) {
@ -41,7 +38,6 @@ namespace LeanCloud.Common {
await fs.ReadAsync(buffer, 0, (int)fs.Length); await fs.ReadAsync(buffer, 0, (int)fs.Length);
text = Encoding.UTF8.GetString(buffer); text = Encoding.UTF8.GetString(buffer);
} }
LCLogger.Debug($"READ: {text}");
return text; return text;
} }
return null; return null;

View File

@ -99,7 +99,7 @@ namespace LeanCloud.Engine {
LCApplication.Initialize(Environment.GetEnvironmentVariable("LEANCLOUD_APP_ID"), LCApplication.Initialize(Environment.GetEnvironmentVariable("LEANCLOUD_APP_ID"),
Environment.GetEnvironmentVariable("LEANCLOUD_APP_KEY"), Environment.GetEnvironmentVariable("LEANCLOUD_APP_KEY"),
Environment.GetEnvironmentVariable("LEANCLOUD_API_SERVER")); 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(); Assembly assembly = Assembly.GetCallingAssembly();
ClassHooks = assembly.GetTypes() ClassHooks = assembly.GetTypes()