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

fea: use int for ttl #77

Merged
merged 2 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
36 changes: 31 additions & 5 deletions src/Sinch/Conversation/Messages/Send/SendMessageRequest.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;
using Sinch.Conversation.Common;
using Sinch.Conversation.Messages.Message;

Expand Down Expand Up @@ -118,11 +121,11 @@ public sealed class SendMessageRequest
/// The timeout allotted for sending the message, expressed in seconds.
/// Passed to channels which support it and emulated by the Conversation API for channels without ttl
/// support but with message retract/unsend functionality. Channel failover will not be performed for
/// messages with an expired TTL. The format is an integer with the suffix `s` (for seconds).
/// Valid integer range is 3 to 315,576,000,000 (inclusive). Example values include `10s`
/// (10 seconds) and `86400s` (24 hours).
/// messages with an expired TTL.
/// </summary>
public string? Ttl { get; set; }
[JsonPropertyName("ttl")]
[JsonConverter(typeof(TimeToLiveConverter))]
public int? TtlSeconds { get; set; }


/// <summary>
Expand Down Expand Up @@ -154,11 +157,34 @@ public override string ToString()
sb.Append(" ConversationMetadata: ").Append(ConversationMetadata).Append("\n");
sb.Append(" Queue: ").Append(Queue).Append("\n");
sb.Append(" Recipient: ").Append(Recipient).Append("\n");
sb.Append(" Ttl: ").Append(Ttl).Append("\n");
sb.Append(" Ttl: ").Append(TtlSeconds).Append("\n");
sb.Append(" ProcessingStrategy: ").Append(ProcessingStrategy).Append("\n");
sb.Append(" CorrelationId: ").Append(CorrelationId).Append("\n");
sb.Append("}\n");
return sb.ToString();
}
}

public class TimeToLiveConverter : JsonConverter<int?>
{
public override int? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var str = reader.GetString();
if (str is null) return null;
str = str.Trim();
var val = str.TakeWhile(x => x == 's').ToString();
asein-sinch marked this conversation as resolved.
Show resolved Hide resolved
asein-sinch marked this conversation as resolved.
Show resolved Hide resolved
if (int.TryParse(val, out int result))
{
return result;
}

return null;
}

public override void Write(Utf8JsonWriter writer, int? value, JsonSerializerOptions options)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also add a test for this one, in order to cover the case where the TTL is null (the e2e only covers the nominal case)? And maybe check if the value is in the correct range.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ttl null is checked in tests above as well, WithJson matcher makes a deep equal comparison, so if ttl is present, there will be an error. And any extended validation for other fields then obvious missing id is not supported by sdk

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's keep it this way then

{
string? result = value.HasValue ? value.Value + "s" : null;
JsonSerializer.Serialize(writer, result, options);
}
}
}
2 changes: 1 addition & 1 deletion tests/Sinch.Tests/Conversation/SendMessageTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -481,7 +481,7 @@ public async Task SendAllParams()
};
_baseRequest.CorrelationId = "cor_id";
_baseRequest.ProcessingStrategy = ProcessingStrategy.DispatchOnly;
_baseRequest.Ttl = "1800s";
_baseRequest.TtlSeconds = 1800;
_baseRequest.Queue = MessageQueue.HighPriority;
_baseRequest.MessageMetadata = "meta";
_baseRequest.Message.ExplicitChannelOmniMessage =
Expand Down
Loading