Skip to content

Commit 153ec4e

Browse files
authored
Merge pull request #21 from pubnub/CE-3355-Message-Counts-API
Message Counts API
2 parents f4168cc + 2b59e28 commit 153ec4e

23 files changed

+847
-7
lines changed

.pubnub.yml

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
11
---
22
changelog:
3+
-
4+
changes:
5+
-
6+
text: "Message Counts API"
7+
type: improvement
8+
date: "Mar 5, 19"
9+
version: v4.1.1
310
-
411
changes:
512
-
@@ -358,4 +365,4 @@ supported-platforms:
358365
- "Ubuntu 12.04+, with Graphics card DX9 (shader model 3.0) or DX11 with feature level 9.3 capabilities; and CPU SSE2 instruction set support."
359366
- "Mac OS X 10.8+, with Graphics card DX9 (shader model 3.0) or DX11 with feature level 9.3 capabilities; and CPU SSE2 instruction set support."
360367
version: "PubNub Unity SDK"
361-
version: v4.1.0
368+
version: v4.1.1
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
using System;
2+
using System.Linq;
3+
using System.Collections;
4+
using System.Collections.Generic;
5+
using UnityEngine;
6+
7+
namespace PubNubAPI
8+
{
9+
public class MessageCountsRequestBuilder: PubNubNonSubBuilder<MessageCountsRequestBuilder, PNMessageCountsResult>, IPubNubNonSubscribeBuilder<MessageCountsRequestBuilder, PNMessageCountsResult>
10+
{
11+
public MessageCountsRequestBuilder(PubNubUnity pn):base(pn, PNOperationType.PNMessageCountsOperation){
12+
13+
}
14+
15+
private string TimetokenToUse {get; set;}
16+
17+
private List<string> ChannelTimetokensToUse {get; set;}
18+
19+
public MessageCountsRequestBuilder Channels(List<string> channelNames){
20+
ChannelsToUse = channelNames;
21+
return this;
22+
}
23+
24+
public MessageCountsRequestBuilder ChannelsTimetoken(List<string> channelTimetoken){
25+
ChannelTimetokensToUse = channelTimetoken;
26+
return this;
27+
}
28+
29+
public MessageCountsRequestBuilder Timetoken(string timetoken){
30+
TimetokenToUse = timetoken;
31+
return this;
32+
}
33+
34+
#region IPubNubBuilder implementation
35+
36+
public void Async(Action<PNMessageCountsResult, PNStatus> callback)
37+
{
38+
this.Callback = callback;
39+
if((this.ChannelsToUse == null) || ((this.ChannelsToUse != null) && (this.ChannelsToUse.Count <= 0))){
40+
PNStatus pnStatus = base.CreateErrorResponseFromMessage("HistoryChannel is null or empty", null, PNStatusCategory.PNBadRequestCategory);
41+
Callback(null, pnStatus);
42+
43+
return;
44+
}
45+
46+
base.Async(this);
47+
}
48+
#endregion
49+
50+
protected override void RunWebRequest(QueueManager qm){
51+
RequestState requestState = new RequestState ();
52+
requestState.OperationType = base.OperationType;
53+
54+
#if (ENABLE_PUBNUB_LOGGING)
55+
this.PubNubInstance.PNLog.WriteToLog(string.Format ("MessageCountsRequestBuilder: \nChannel {0} \nChannelTimetokens: {1} \nTimetokenToUse:{2}", string.Join(",", this.ChannelsToUse.ToArray()), (ChannelTimetokensToUse!=null)?string.Join(",", this.ChannelTimetokensToUse.ToArray()):"", this.TimetokenToUse), PNLoggingMethod.LevelInfo);
56+
#endif
57+
58+
Uri request = BuildRequests.BuildMessageCountsRequest(
59+
ChannelsToUse.ToArray(),
60+
(ChannelTimetokensToUse!=null)?ChannelTimetokensToUse.ToArray():null,
61+
TimetokenToUse,
62+
this.PubNubInstance,
63+
this.QueryParams
64+
);
65+
base.RunWebRequest(qm, request, requestState, this.PubNubInstance.PNConfig.NonSubscribeTimeout, 0, this);
66+
67+
}
68+
69+
protected override void CreatePubNubResponse(object deSerializedResult, RequestState requestState){
70+
//Returned JSON: `{"status": 200, "error": false, "error_message": "", "channels": {"my-channel1":1,"my-channel":2}}`
71+
PNMessageCountsResult pnMessageCountsResult = new PNMessageCountsResult();
72+
PNStatus pnStatus = new PNStatus();
73+
try{
74+
Dictionary<string, object> dictionary = deSerializedResult as Dictionary<string, object>;
75+
76+
if(dictionary != null) {
77+
string message = Utility.ReadMessageFromResponseDictionary(dictionary, "error_message");
78+
if(Utility.CheckDictionaryForError(dictionary, "error")){
79+
pnMessageCountsResult = null;
80+
pnStatus = base.CreateErrorResponseFromMessage(message, requestState, PNStatusCategory.PNUnknownCategory);
81+
} else {
82+
object objChannelsDict;
83+
dictionary.TryGetValue("channels", out objChannelsDict);
84+
if(objChannelsDict != null){
85+
Dictionary<string, object> channelsDict = objChannelsDict as Dictionary<string, object>;
86+
if(channelsDict==null){
87+
pnMessageCountsResult = null;
88+
pnStatus = base.CreateErrorResponseFromMessage("channelsResult dictionary is null", requestState, PNStatusCategory.PNUnknownCategory);
89+
} else {
90+
Dictionary<string, int> resultDict = new Dictionary<string, int>();
91+
foreach(KeyValuePair<string, object> kvp in channelsDict){
92+
resultDict.Add(kvp.Key, Convert.ToInt32(kvp.Value));
93+
}
94+
pnMessageCountsResult.Channels = resultDict;
95+
}
96+
}
97+
}
98+
} else {
99+
pnMessageCountsResult = null;
100+
pnStatus = base.CreateErrorResponseFromMessage("Response dictionary is null", requestState, PNStatusCategory.PNUnknownCategory);
101+
}
102+
} catch (Exception ex) {
103+
pnMessageCountsResult = null;
104+
pnStatus = base.CreateErrorResponseFromException(ex, requestState, PNStatusCategory.PNUnknownCategory);
105+
}
106+
Callback(pnMessageCountsResult, pnStatus);
107+
}
108+
}
109+
}
110+

PubNubUnity/Assets/PubNub/Builders/History/MessageCountsRequestBuilder.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PubNubUnity/Assets/PubNub/Editor/FetchMessagesBuildRequestsTests.cs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,86 @@
22
using PubNubAPI;
33
using NUnit.Framework;
44
using System.Text;
5+
using System.Collections.Generic;
56

67
namespace PubNubAPI.Tests
78
{
89
[TestFixture]
910
public class FetchMessagesBuildRequestsTests
1011
{
1112
#if DEBUG
13+
[Test]
14+
public void TestBuildFetchMessagesRequest ()
15+
{
16+
long startTime = 14498416434364941;
17+
long endTime = 14498416799269095;
18+
19+
TestFetchMessagesBuildRequestCommon (false, false, false, "authKey", startTime, endTime, 25, false);
20+
}
21+
22+
public void TestFetchMessagesBuildRequestCommon(bool ssl, bool reverse, bool includeTimetoken,
23+
string authKey, long startTime, long endTime, int count, bool sendQueryParams){
24+
string[] channels = new[] {"history_channel", "history_channel2"};
25+
string uuid = "customuuid";
26+
Dictionary<string,string> queryParams = new Dictionary<string, string>();
27+
string queryParamString = "";
28+
if(sendQueryParams){
29+
queryParams.Add("d","f");
30+
queryParamString="&d=f";
31+
} else {
32+
queryParams = null;
33+
}
34+
35+
PNConfiguration pnConfiguration = new PNConfiguration ();
36+
pnConfiguration.Origin = EditorCommon.Origin;
37+
pnConfiguration.SubscribeKey = EditorCommon.SubscribeKey;
38+
pnConfiguration.PublishKey = EditorCommon.PublishKey;
39+
pnConfiguration.Secure = ssl;
40+
pnConfiguration.CipherKey = "enigma";
41+
pnConfiguration.LogVerbosity = PNLogVerbosity.BODY;
42+
pnConfiguration.PresenceTimeout = 60;
43+
pnConfiguration.PresenceInterval= 30;
44+
pnConfiguration.AuthKey = authKey;
45+
pnConfiguration.UUID = uuid;
46+
47+
PubNubUnity pnUnity = new PubNubUnity(pnConfiguration, null, null);
48+
49+
string authKeyString = "";
50+
if (!string.IsNullOrEmpty(authKey)) {
51+
authKeyString = string.Format ("&auth={0}", pnConfiguration.AuthKey);
52+
}
53+
54+
string startTimeString = "";
55+
string endTimeString = "";
56+
if (startTime != -1) {
57+
startTimeString = string.Format ("&start={0}", startTime.ToString ());
58+
}
59+
if (endTime != -1) {
60+
endTimeString = string.Format ("&end={0}", endTime.ToString ());
61+
}
62+
63+
if(count == -1){
64+
count = 100;
65+
}
66+
67+
Uri uri = BuildRequests.BuildFetchRequest (channels, startTime, endTime, (uint)count, reverse,
68+
includeTimetoken, pnUnity, queryParams
69+
);
70+
71+
if (count == -1) {
72+
count = 100;
73+
}
74+
//http://ps.pndsn.com/v3/history/sub-key/demo/channel/history_channel,history_channel2?max=90&start=14498416434364941&end=14498416799269095&auth=authKey&uuid=customuuid&pnsdk=PubNub-CSharp-UnityOSX%2F4.1.1
75+
string expected = string.Format ("http{0}://{1}/v3/history/sub-key/{2}/channel/{3}?max={4}{5}{6}{7}{8}{9}&uuid={10}&pnsdk={11}{12}",
76+
ssl?"s":"", pnConfiguration.Origin, EditorCommon.SubscribeKey, string.Join(",", channels), count,
77+
includeTimetoken?"&include_token=true":"", reverse?"&reverse=true":"",
78+
startTimeString, endTimeString, authKeyString, uuid,
79+
Utility.EncodeUricomponent(pnUnity.Version, PNOperationType.PNHistoryOperation, false, true),
80+
queryParamString
81+
);
82+
string received = uri.OriginalString;
83+
EditorCommon.LogAndCompare (expected, received);
84+
}
1285
#endif
1386
}
1487
}
Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
using System;
2+
using PubNubAPI;
3+
using NUnit.Framework;
4+
using System.Text;
5+
using System.Collections.Generic;
6+
7+
namespace PubNubAPI.Tests
8+
{
9+
[TestFixture]
10+
public class MessageCountsBuildRequestsTests
11+
{
12+
#if DEBUG
13+
[Test]
14+
public void TestBuildMessageCountsRequestMultiChannelAuthSSL ()
15+
{
16+
string[] channels = { "test", "test2" };
17+
string[] channelsTimetoken = { "15499825804610610", "15499925804610615" };
18+
string timetoken = "15499825804610609";
19+
TestMessageCountsBuildRequestCommon (channels, channelsTimetoken, timetoken, true, "authKey", false);
20+
}
21+
22+
[Test]
23+
public void TestBuildMessageCountsRequestMultiChannelTTAuthSSL ()
24+
{
25+
string[] channels = { "test", "test2" };
26+
string[] channelsTimetoken = { };
27+
string timetoken = "15499825804610609";
28+
TestMessageCountsBuildRequestCommon (channels, channelsTimetoken, timetoken, true, "authKey", false);
29+
}
30+
31+
[Test]
32+
public void TestBuildMessageCountsRequestMultiChannelCTAuthSSL ()
33+
{
34+
string[] channels = { "test", "test2" };
35+
string[] channelsTimetoken = { "15499825804610610", "15499925804610615" };
36+
string timetoken = "";
37+
TestMessageCountsBuildRequestCommon (channels, channelsTimetoken, timetoken, true, "authKey", false);
38+
}
39+
40+
[Test]
41+
public void TestBuildMessageCountsRequestMultiChannelAuthSSLQP ()
42+
{
43+
string[] channels = { "test", "test2" };
44+
string[] channelsTimetoken = { "15499825804610610", "15499925804610615" };
45+
string timetoken = "15499825804610609";
46+
TestMessageCountsBuildRequestCommon (channels, channelsTimetoken, timetoken, true, "authKey", true);
47+
}
48+
49+
[Test]
50+
public void TestBuildMessageCountsRequestMultiChannelTTAuthSSLQP ()
51+
{
52+
string[] channels = { "test", "test2" };
53+
string[] channelsTimetoken = { };
54+
string timetoken = "15499825804610609";
55+
TestMessageCountsBuildRequestCommon (channels, channelsTimetoken, timetoken, true, "authKey", true);
56+
}
57+
58+
[Test]
59+
public void TestBuildMessageCountsRequestMultiChannelCTAuthSSLQP ()
60+
{
61+
string[] channels = { "test", "test2" };
62+
string[] channelsTimetoken = { "15499825804610610", "15499925804610615" };
63+
string timetoken = "";
64+
TestMessageCountsBuildRequestCommon (channels, channelsTimetoken, timetoken, true, "authKey", true);
65+
}
66+
67+
public void TestMessageCountsBuildRequestCommon(string[] channels, string[] channelsTimetoken, string timetoken, bool ssl, string authKey, bool sendQueryParams)
68+
{
69+
Dictionary<string,string> queryParams = new Dictionary<string, string>();
70+
string queryParamString = "";
71+
if(sendQueryParams){
72+
queryParams.Add("d","f");
73+
queryParamString="&d=f";
74+
} else {
75+
queryParams = null;
76+
}
77+
string uuid = "customuuid";
78+
PNConfiguration pnConfiguration = new PNConfiguration ();
79+
pnConfiguration.Origin = EditorCommon.Origin;
80+
pnConfiguration.SubscribeKey = EditorCommon.SubscribeKey;
81+
pnConfiguration.PublishKey = EditorCommon.PublishKey;
82+
pnConfiguration.Secure = ssl;
83+
pnConfiguration.CipherKey = "enigma";
84+
pnConfiguration.LogVerbosity = PNLogVerbosity.BODY;
85+
86+
pnConfiguration.AuthKey = authKey;
87+
pnConfiguration.UUID = uuid;
88+
89+
90+
PubNubUnity pnUnity = new PubNubUnity(pnConfiguration, null, null);
91+
92+
string authKeyString = "";
93+
if (!string.IsNullOrEmpty(authKey)) {
94+
authKeyString = string.Format ("&auth={0}", pnConfiguration.AuthKey);
95+
}
96+
97+
string channelsTimetokenStr = "" , chStr = "";
98+
if(channels != null){
99+
chStr = string.Join(",", channels);
100+
}
101+
102+
if(channelsTimetoken != null){
103+
channelsTimetokenStr = string.Join(",", channelsTimetoken);
104+
}
105+
106+
Uri uri = BuildRequests.BuildMessageCountsRequest (channels, channelsTimetoken, timetoken, pnUnity, queryParams);
107+
108+
//https://ps.pndsn.com/v3/history/sub-key/demo/message-counts/test,test2?timetoken=15499825804610609&channelsTimetoken=15499825804610610,15499925804610615&auth=authKey&uuid=customuuid&pnsdk=PubNub-CSharp-UnityOSX%2F4.1.1
109+
string expected = string.Format ("http{0}://{1}/v3/history/sub-key/{2}/message-counts/{3}?timetoken={4}&channelsTimetoken={5}{6}&uuid={7}&pnsdk={8}{9}",
110+
ssl?"s":"", pnConfiguration.Origin, EditorCommon.SubscribeKey, chStr, timetoken,
111+
channelsTimetokenStr, authKeyString,
112+
uuid, Utility.EncodeUricomponent(pnUnity.Version, PNOperationType.PNMessageCountsOperation, false, true),
113+
queryParamString
114+
);
115+
string received = uri.OriginalString;
116+
received.Trim().Equals(expected.Trim());
117+
EditorCommon.LogAndCompare (expected, received);
118+
}
119+
#endif
120+
}
121+
}

PubNubUnity/Assets/PubNub/Editor/MessageCountsBuildRequestsTests.cs.meta

Lines changed: 11 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)