public T ParseResponse <T>(AblyRequest request, AblyResponse response) where T : class { LogResponse(response); if (typeof(T) == typeof(PaginatedResource <Message>)) { var result = PaginatedResource.InitialisePartialResult <Message>(response.Headers, GetLimit(request)); result.AddRange(ParseMessagesResponse(response, request.ChannelOptions)); return(result as T); } if (typeof(T) == typeof(PaginatedResource <Stats>)) { var result = PaginatedResource.InitialisePartialResult <Stats>(response.Headers, GetLimit(request)); result.AddRange(ParseStatsResponse(response)); return(result as T); } if (typeof(T) == typeof(PaginatedResource <PresenceMessage>)) { var result = PaginatedResource.InitialisePartialResult <PresenceMessage>(response.Headers, GetLimit(request)); result.AddRange(ParsePresenceMessages(response)); return(result as T); } var responseText = response.TextResponse; if (_protocol == Protocol.MsgPack) { //A bit of a hack. Message pack serialiser does not like capability objects responseText = MsgPackHelper.DeSerialise(response.Body, typeof(MessagePackObject)).ToString(); } return((T)JsonConvert.DeserializeObject(responseText, typeof(T))); }
private IEnumerable <Stats> ParseStatsResponse(AblyResponse response) { var body = response.TextResponse; if (_protocol == Protocol.MsgPack) { body = ((MessagePackObject)MsgPackHelper.DeSerialise(response.Body, typeof(MessagePackObject))).ToString(); } return(JsonConvert.DeserializeObject <List <Stats> >(body)); }
public void CanSerialiseListOfMessagesAndDeserialiseThem() { var message = new Message("example", "The quick brown fox jumped over the lazy dog"); var serialised = MsgPackHelper.Serialise(new List <Message> { message }); var result = MsgPackHelper.DeSerialise(serialised, typeof(List <Message>)) as List <Message>; var resultMessage = result.First(); ((MessagePackObject)resultMessage.Data).ToObject().Should().Be(message.Data); resultMessage.Name.Should().Be(message.Name); }
private void LogResponse(AblyResponse response) { Logger.Info("Protocol:" + _protocol); try { var responseBody = response.TextResponse; if (_protocol == Protocol.MsgPack && response.Body != null) { responseBody = MsgPackHelper.DeSerialise(response.Body, typeof(MessagePackObject)).ToString(); } Logger.Debug("Response: " + responseBody); } catch (Exception ex) { Logger.Error("Error while logging response body.", ex); } }
public void CanSerialiseAndDeserialiseBase64ByteArray() { var message = new Message() { Name = "example", Data = "AAECAwQFBgcICQoLDA0ODw==".FromBase64() }; var serialised = MsgPackHelper.Serialise(new List <Message> { message }); Console.WriteLine(serialised.ToBase64()); var resultMessage = MsgPackHelper.DeSerialise(serialised, typeof(List <Message>)) as List <Message>; var data = ((MessagePackObject)resultMessage.First().Data).ToObject() as byte[]; data.Should().BeEquivalentTo(message.Data as byte[]); resultMessage.First().Name.Should().Be(message.Name); }
public IEnumerable <PresenceMessage> ParsePresenceMessages(AblyResponse response) { if (response.Type == ResponseType.Json) { var messages = JsonConvert.DeserializeObject <List <PresenceMessage> >(response.TextResponse); ProcessMessages(messages, new ChannelOptions()); return(messages); } var payloads = MsgPackHelper.DeSerialise(response.Body, typeof(List <PresenceMessage>)) as List <PresenceMessage>; foreach (var payload in payloads.Where(x => x.Data != null)) { //Unwrap the data objects because message pack leaves them as a MessagePackObject payload.Data = ((MessagePackObject)payload.Data).ToObject(); } ProcessMessages(payloads, new ChannelOptions()); return(payloads); }
public void CanDeserialiseTokenResponse() { var value = "gaxhY2Nlc3NfdG9rZW6GpXRva2Vu2YhnNFg2UVEuRHlCYzlMZUdvdy1saWVEcG4zTXRsd09uUEhoN2VtN3MyQ3JTZ1pLM2NUNkRvZUo1dlQxWXRwNDFvaTVWUUtNUkxuSVdDckFadHVOb3F5Q0lvVFphQjFfb1FFX0Utb3c2Y3hKX1EwcFUyZ3lpb2xRNGp1VDM1TjI0Qzgzd0p6aUI5p2tleU5hbWWtZzRYNlFRLnV0ekdsZ6Zpc3N1ZWTOVMEP1qdleHBpcmVzzlTBHeaqY2FwYWJpbGl0eYGhKpGhKqhjbGllbnRJZKMxMjM="; var decodedMessagePack = MsgPackHelper.DeSerialise(value.FromBase64(), typeof(MessagePackObject)).ToString(); Console.WriteLine(DateTimeOffset.Now.ToUnixTimeInMilliseconds()); var response = JsonConvert.DeserializeObject <TokenResponse>(decodedMessagePack); response.AccessToken.Should().NotBeNull(); response.AccessToken.KeyName.Should().Be("g4X6QQ.utzGlg"); response.AccessToken.Capability.ToJson().Should().Be("{ \"*\": [ \"*\" ] }"); response.AccessToken.ClientId.Should().Be("123"); response.AccessToken.Token.Should().Be("g4X6QQ.DyBc9LeGow-lieDpn3MtlwOnPHh7em7s2CrSgZK3cT6DoeJ5vT1Ytp41oi5VQKMRLnIWCrAZtuNoqyCIoTZaB1_oQE_E-ow6cxJ_Q0pU2gyiolQ4juT35N24C83wJziB9"); response.AccessToken.Issued.Should().Be(((long)1421938646).FromUnixTimeInMilliseconds()); response.AccessToken.Expires.Should().Be(((long)1421942246).FromUnixTimeInMilliseconds()); }