Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

support multiple JPush clients #38

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 16 additions & 10 deletions Jiguang.JPush/DeviceClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// <summary>
/// <see cref="GetDeviceInfo(string)"/>
Expand All @@ -22,7 +28,7 @@ public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -51,7 +57,7 @@ public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -99,7 +105,7 @@ public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -130,7 +136,7 @@ public async Task<HttpResponse> 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, "");
}

Expand All @@ -153,7 +159,7 @@ public HttpResponse DeleteAlias(string alias, string platform)
public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -181,7 +187,7 @@ public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -221,7 +227,7 @@ public async Task<HttpResponse> AddDevicesToTagAsync(string tag, List<string> 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, "");
}

Expand Down Expand Up @@ -260,7 +266,7 @@ public async Task<HttpResponse> RemoveDevicesFromTagAsync(string tag, List<strin
};

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, "");
}

Expand Down Expand Up @@ -290,7 +296,7 @@ public async Task<HttpResponse> 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, "");
}

Expand Down Expand Up @@ -324,7 +330,7 @@ public async Task<HttpResponse> GetUserOnlineStatusAsync(List<string> 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);
}
Expand Down
26 changes: 11 additions & 15 deletions Jiguang.JPush/JPushClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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);
}

/// <summary>
Expand All @@ -68,7 +64,7 @@ public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -104,7 +100,7 @@ public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -150,7 +146,7 @@ public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -230,7 +226,7 @@ private async Task<HttpResponse> BatchPushAsync(String url, List<SinglePayload>
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);
}
Expand Down
18 changes: 12 additions & 6 deletions Jiguang.JPush/ReportClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
/// 设置 Report API 的调用地址。
Expand All @@ -35,7 +41,7 @@ public async Task<HttpResponse> GetMessageReportAsync(List<string> 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);
}
Expand All @@ -61,7 +67,7 @@ public async Task<HttpResponse> GetReceivedDetailReportAsync(List<string> 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);
}
Expand Down Expand Up @@ -101,7 +107,7 @@ public async Task<HttpResponse> GetMessageSendStatusAsync(string msgId, List<str
var url = BASE_URL + "/status/message";
var httpContent = new StringContent(body.ToString(), Encoding.UTF8);

HttpResponseMessage msg = await JPushClient.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);
}
Expand Down Expand Up @@ -130,7 +136,7 @@ public async Task<HttpResponse> GetMessageDetailReportAsync(List<string> 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);
}
Expand All @@ -157,7 +163,7 @@ public async Task<HttpResponse> GetMessagesDetailReportAsync(List<string> 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);
}
Expand Down Expand Up @@ -189,7 +195,7 @@ public async Task<HttpResponse> 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);
}
Expand Down
19 changes: 13 additions & 6 deletions Jiguang.JPush/ScheduleClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/// <summary>
/// 设置 Schedule API 的调用地址。
/// </summary>
Expand All @@ -42,7 +49,7 @@ public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -142,7 +149,7 @@ public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -172,7 +179,7 @@ public async Task<HttpResponse> 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);
}
Expand All @@ -198,7 +205,7 @@ public async Task<HttpResponse> 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);
}
Expand All @@ -224,7 +231,7 @@ public async Task<HttpResponse> 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);
}
Expand Down Expand Up @@ -338,7 +345,7 @@ public async Task<HttpResponse> 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);
}
Expand Down