diff --git a/Jiguang.JPush/DeviceClient.cs b/Jiguang.JPush/DeviceClient.cs index 298ebab..8c50dbb 100644 --- a/Jiguang.JPush/DeviceClient.cs +++ b/Jiguang.JPush/DeviceClient.cs @@ -11,7 +11,13 @@ namespace Jiguang.JPush { public class DeviceClient { + public DeviceClient(HttpClient client) + { + _client = client; + } + private const string BASE_URL = "https://device.jpush.cn"; + private HttpClient _client; /// /// @@ -22,7 +28,7 @@ public async Task GetDeviceInfoAsync(string registrationId) throw new ArgumentNullException(registrationId); var url = BASE_URL + "/v3/devices/" + registrationId; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } @@ -51,7 +57,7 @@ public async Task UpdateDeviceInfoAsync(string registrationId, str var url = BASE_URL + "/v3/devices/" + registrationId; HttpContent requestContent = new StringContent(json, Encoding.UTF8); - HttpResponseMessage msg = await JPushClient.HttpClient.PostAsync(url, requestContent).ConfigureAwait(false); + HttpResponseMessage msg = await _client.PostAsync(url, requestContent).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } @@ -99,7 +105,7 @@ public async Task GetDevicesByAliasAsync(string alias, string plat if (!string.IsNullOrEmpty(platform)) url += "?platform=" + platform; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); string responseConetent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseConetent); } @@ -130,7 +136,7 @@ public async Task DeleteAliasAsync(string alias, string platform) if (!string.IsNullOrEmpty(platform)) url += "?platform=" + platform; - HttpResponseMessage msg = await JPushClient.HttpClient.DeleteAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.DeleteAsync(url).ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, ""); } @@ -153,7 +159,7 @@ public HttpResponse DeleteAlias(string alias, string platform) public async Task GetTagsAsync() { var url = BASE_URL + "/v3/tags/"; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } @@ -181,7 +187,7 @@ public async Task IsDeviceInTagAsync(string registrationId, string throw new ArgumentNullException(nameof(tag)); var url = BASE_URL + "/v3/tags/" + tag + "/registration_ids/" + registrationId; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } @@ -221,7 +227,7 @@ public async Task AddDevicesToTagAsync(string tag, List re }; var requestContent = new StringContent(jObj.ToString(), Encoding.UTF8); - HttpResponseMessage msg = await JPushClient.HttpClient.PostAsync(url, requestContent).ConfigureAwait(false); + HttpResponseMessage msg = await _client.PostAsync(url, requestContent).ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, ""); } @@ -260,7 +266,7 @@ public async Task RemoveDevicesFromTagAsync(string tag, List DeleteTagAsync(string tag, string platform) if (!string.IsNullOrEmpty(platform)) url += "?platform=" + platform; - HttpResponseMessage msg = await JPushClient.HttpClient.DeleteAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.DeleteAsync(url).ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, ""); } @@ -324,7 +330,7 @@ public async Task GetUserOnlineStatusAsync(List registrati var requestContent = new StringContent(jObj.ToString(), Encoding.UTF8); - HttpResponseMessage msg = await JPushClient.HttpClient.PostAsync(url, requestContent).ConfigureAwait(false); + HttpResponseMessage msg = await _client.PostAsync(url, requestContent).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } diff --git a/Jiguang.JPush/JPushClient.cs b/Jiguang.JPush/JPushClient.cs index 0972ab3..d1176e5 100644 --- a/Jiguang.JPush/JPushClient.cs +++ b/Jiguang.JPush/JPushClient.cs @@ -23,13 +23,7 @@ public class JPushClient public ReportClient Report { get => report; set => report = value; } - public static HttpClient HttpClient; - - static JPushClient() - { - HttpClient = new HttpClient(); - HttpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); - } + public HttpClient _client; public JPushClient(string appKey, string masterSecret) { @@ -39,12 +33,14 @@ public JPushClient(string appKey, string masterSecret) if (string.IsNullOrEmpty(masterSecret)) throw new ArgumentNullException(nameof(masterSecret)); + _client = new HttpClient(); + _client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); var auth = Convert.ToBase64String(Encoding.UTF8.GetBytes(appKey + ":" + masterSecret)); - HttpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth); + _client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Basic", auth); - Report = new ReportClient(); - Device = new DeviceClient(); - Schedule = new ScheduleClient(); + Report = new ReportClient(_client); + Device = new DeviceClient(_client); + Schedule = new ScheduleClient(_client); } /// @@ -68,7 +64,7 @@ public async Task SendPushAsync(string jsonBody) throw new ArgumentNullException(nameof(jsonBody)); HttpContent httpContent = new StringContent(jsonBody, Encoding.UTF8); - HttpResponseMessage msg = await HttpClient.PostAsync(BASE_URL, httpContent).ConfigureAwait(false); + HttpResponseMessage msg = await _client.PostAsync(BASE_URL, httpContent).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } @@ -104,7 +100,7 @@ public async Task IsPushValidAsync(string jsonBody) HttpContent httpContent = new StringContent(jsonBody, Encoding.UTF8); var url = BASE_URL + "/validate"; - HttpResponseMessage msg = await HttpClient.PostAsync(url, httpContent).ConfigureAwait(false); + HttpResponseMessage msg = await _client.PostAsync(url, httpContent).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } @@ -150,7 +146,7 @@ public async Task GetCIdListAsync(int? count, string type) url += ("&type=" + type); } - HttpResponseMessage msg = await HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } @@ -230,7 +226,7 @@ private async Task BatchPushAsync(String url, List batchPushPayload.Pushlist.Add((String) jArray[i], singlePayLoadList[i]); } HttpContent httpContent = new StringContent(batchPushPayload.ToString(), Encoding.UTF8); - HttpResponseMessage msg = await HttpClient.PostAsync(url, httpContent).ConfigureAwait(false); + HttpResponseMessage msg = await _client.PostAsync(url, httpContent).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } diff --git a/Jiguang.JPush/ReportClient.cs b/Jiguang.JPush/ReportClient.cs index d72b1ae..00b97c3 100644 --- a/Jiguang.JPush/ReportClient.cs +++ b/Jiguang.JPush/ReportClient.cs @@ -14,6 +14,12 @@ public class ReportClient public const string BASE_URL_REPORT_BEIJING = "https://bjapi.push.jiguang.cn/v3/report"; private string BASE_URL = BASE_URL_REPORT_DEFAULT; + private HttpClient _client; + + public ReportClient(HttpClient client) + { + _client = client; + } /// /// 设置 Report API 的调用地址。 @@ -35,7 +41,7 @@ public async Task GetMessageReportAsync(List msgIdList) var msgIds = string.Join(",", msgIdList); var url = BASE_URL + "/received?msg_ids=" + msgIds; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } @@ -61,7 +67,7 @@ public async Task GetReceivedDetailReportAsync(List msgIdL var msgIds = string.Join(",", msgIdList); var url = BASE_URL + "/received/detail?msg_ids=" + msgIds; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } @@ -101,7 +107,7 @@ public async Task GetMessageSendStatusAsync(string msgId, List GetMessageDetailReportAsync(List msgIdLi var msgIds = string.Join(",", msgIdList); var url = BASE_URL + "/messages?msg_ids=" + msgIds; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } @@ -157,7 +163,7 @@ public async Task GetMessagesDetailReportAsync(List msgIdL var msgIds = string.Join(",", msgIdList); var url = BASE_URL + "/messages/detail?msg_ids=" + msgIds; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } @@ -189,7 +195,7 @@ public async Task GetUserReportAsync(string timeUnit, string start throw new ArgumentOutOfRangeException(nameof(duration)); var url = BASE_URL + "/users?time_unit=" + timeUnit + "&start=" + startTime + "&duration=" + duration; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); var content = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, content); } diff --git a/Jiguang.JPush/ScheduleClient.cs b/Jiguang.JPush/ScheduleClient.cs index c0e366d..420c1d6 100644 --- a/Jiguang.JPush/ScheduleClient.cs +++ b/Jiguang.JPush/ScheduleClient.cs @@ -15,11 +15,18 @@ public class ScheduleClient private string BASE_URL = BASE_URL_SCHEDULE_DEFAULT; + private HttpClient _client; + private JsonSerializer jsonSerializer = new JsonSerializer { NullValueHandling = NullValueHandling.Ignore }; + public ScheduleClient(HttpClient httpClient) + { + _client = httpClient; + } + /// /// 设置 Schedule API 的调用地址。 /// @@ -42,7 +49,7 @@ public async Task CreateScheduleTaskAsync(string json) throw new ArgumentNullException(nameof(json)); HttpContent requestContent = new StringContent(json, Encoding.UTF8); - HttpResponseMessage msg = await JPushClient.HttpClient.PostAsync(BASE_URL, requestContent).ConfigureAwait(false); + HttpResponseMessage msg = await _client.PostAsync(BASE_URL, requestContent).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } @@ -142,7 +149,7 @@ public async Task GetValidScheduleTasksAsync(int page = 1) throw new ArgumentNullException(nameof(page)); var url = BASE_URL + "?page=" + page; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } @@ -172,7 +179,7 @@ public async Task GetScheduleTaskAsync(string scheduleId) throw new ArgumentNullException(nameof(scheduleId)); var url = BASE_URL + $"/{scheduleId}"; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } @@ -198,7 +205,7 @@ public async Task GetScheduleTaskMsgIdAsync(string scheduleId) throw new ArgumentNullException(nameof(scheduleId)); var url = BASE_URL + $"/{scheduleId}/msg_ids"; - HttpResponseMessage msg = await JPushClient.HttpClient.GetAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.GetAsync(url).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } @@ -224,7 +231,7 @@ public async Task UpdateScheduleTaskAsync(string scheduleId, strin var url = BASE_URL + $"/{scheduleId}"; HttpContent requestContent = new StringContent(json, Encoding.UTF8); - HttpResponseMessage msg = await JPushClient.HttpClient.PutAsync(url, requestContent).ConfigureAwait(false); + HttpResponseMessage msg = await _client.PutAsync(url, requestContent).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); } @@ -338,7 +345,7 @@ public async Task DeleteScheduleTaskAsync(string scheduleId) throw new ArgumentNullException(nameof(scheduleId)); var url = BASE_URL + $"/{scheduleId}"; - HttpResponseMessage msg = await JPushClient.HttpClient.DeleteAsync(url).ConfigureAwait(false); + HttpResponseMessage msg = await _client.DeleteAsync(url).ConfigureAwait(false); string responseContent = await msg.Content.ReadAsStringAsync().ConfigureAwait(false); return new HttpResponse(msg.StatusCode, msg.Headers, responseContent); }