Commit d77cbb16 authored by Maodashu's avatar Maodashu

增加api请求授权机制

parent 73f7f358
...@@ -19,6 +19,7 @@ namespace OTWebSocket.API.Services ...@@ -19,6 +19,7 @@ namespace OTWebSocket.API.Services
{ {
public class ApiService public class ApiService
{ {
private readonly string psw;
private readonly string _apiBaseUrl; private readonly string _apiBaseUrl;
private readonly ILogger<ApiService> _logger; private readonly ILogger<ApiService> _logger;
private readonly HttpClient _httpClient; private readonly HttpClient _httpClient;
...@@ -27,6 +28,7 @@ namespace OTWebSocket.API.Services ...@@ -27,6 +28,7 @@ namespace OTWebSocket.API.Services
_apiBaseUrl = options.Value.ApiBaseUrl; _apiBaseUrl = options.Value.ApiBaseUrl;
_logger = logger; _logger = logger;
_httpClient = new HttpClient(); _httpClient = new HttpClient();
psw = "nixiaode";
} }
public async Task<List<AccountInfo>> GetKeyList() public async Task<List<AccountInfo>> GetKeyList()
...@@ -35,6 +37,7 @@ namespace OTWebSocket.API.Services ...@@ -35,6 +37,7 @@ namespace OTWebSocket.API.Services
List<AccountInfo> ret = null; List<AccountInfo> ret = null;
string endpoint = $"/key"; string endpoint = $"/key";
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, _apiBaseUrl + endpoint); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, _apiBaseUrl + endpoint);
req.Headers.Add("Authorization", psw);
HttpResponseMessage resp = await _httpClient.SendAsync(req); HttpResponseMessage resp = await _httpClient.SendAsync(req);
string content = await resp.Content.ReadAsStringAsync(); string content = await resp.Content.ReadAsStringAsync();
_logger.Debug($"[请求账户列表] API返回:{content}"); _logger.Debug($"[请求账户列表] API返回:{content}");
...@@ -82,7 +85,7 @@ namespace OTWebSocket.API.Services ...@@ -82,7 +85,7 @@ namespace OTWebSocket.API.Services
{ "ot_symbol", ot_symbol } { "ot_symbol", ot_symbol }
}; };
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, _apiBaseUrl + endpoint); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, _apiBaseUrl + endpoint);
req.Headers.Add("Authorization", psw);
req.Content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json"); req.Content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
HttpResponseMessage resp = await _httpClient.SendAsync(req); HttpResponseMessage resp = await _httpClient.SendAsync(req);
string content = await resp.Content.ReadAsStringAsync(); string content = await resp.Content.ReadAsStringAsync();
...@@ -120,6 +123,7 @@ namespace OTWebSocket.API.Services ...@@ -120,6 +123,7 @@ namespace OTWebSocket.API.Services
string endpoint = $"/mao/mongo/query/{orderId}"; string endpoint = $"/mao/mongo/query/{orderId}";
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, _apiBaseUrl + endpoint); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Get, _apiBaseUrl + endpoint);
req.Headers.Add("Authorization", psw);
HttpResponseMessage resp = await _httpClient.SendAsync(req); HttpResponseMessage resp = await _httpClient.SendAsync(req);
string content = await resp.Content.ReadAsStringAsync(); string content = await resp.Content.ReadAsStringAsync();
_logger.Debug($"[请求MongoDB订单信息] API返回:{content}"); _logger.Debug($"[请求MongoDB订单信息] API返回:{content}");
...@@ -149,6 +153,7 @@ namespace OTWebSocket.API.Services ...@@ -149,6 +153,7 @@ namespace OTWebSocket.API.Services
string endpoint = $"/mao/mongo/update/{order.OrderId}"; string endpoint = $"/mao/mongo/update/{order.OrderId}";
JObject payload = JObject.FromObject(order); JObject payload = JObject.FromObject(order);
HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Put, _apiBaseUrl + endpoint); HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Put, _apiBaseUrl + endpoint);
req.Headers.Add("Authorization", psw);
req.Content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json"); req.Content = new StringContent(payload.ToString(), Encoding.UTF8, "application/json");
HttpResponseMessage resp = await _httpClient.SendAsync(req); HttpResponseMessage resp = await _httpClient.SendAsync(req);
string content = await resp.Content.ReadAsStringAsync(); string content = await resp.Content.ReadAsStringAsync();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment