diff --git a/Alexa.NET/Request/RequestBundle.cs b/Alexa.NET/Request/RequestBundle.cs deleted file mode 100644 index 16b4796..0000000 --- a/Alexa.NET/Request/RequestBundle.cs +++ /dev/null @@ -1,39 +0,0 @@ -using Alexa.NET.Request.Type; -using Newtonsoft.Json; -using System; - -namespace Alexa.NET.Request -{ - public class RequestBundle : IIntentRequest, ILaunchRequest, ISessionEndedRequest - { - [JsonProperty("type")] - public string Type { get; set; } - - [JsonProperty("requestId")] - public string RequestId { get; set; } - - [JsonProperty("timestamp")] - public DateTime Timestamp { get; set; } - - [JsonProperty("intent")] - public Intent Intent { get; set; } - - [JsonProperty("reason")] - public string Reason { get; set; } - - public System.Type GetRequestType() - { - switch (Type) - { - case "IntentRequest": - return typeof(IIntentRequest); - case "LaunchRequest": - return typeof(ILaunchRequest); - case "SessionEndedRequest": - return typeof(ISessionEndedRequest); - default: - throw new ArgumentOutOfRangeException(nameof(Type), $"Unknown request type: {Type}."); - } - } - } -} \ No newline at end of file diff --git a/Alexa.NET/Request/SkillRequest.cs b/Alexa.NET/Request/SkillRequest.cs index e2c4dfa..e1b6fe0 100644 --- a/Alexa.NET/Request/SkillRequest.cs +++ b/Alexa.NET/Request/SkillRequest.cs @@ -1,4 +1,5 @@ -using Newtonsoft.Json; +using Alexa.NET.Request.Type; +using Newtonsoft.Json; namespace Alexa.NET.Request { @@ -11,11 +12,12 @@ public class SkillRequest public Session Session { get; set; } [JsonProperty("request")] - public RequestBundle Request { get; set; } + [JsonConverter(typeof(RequestConverter))] + public Type.Request Request { get; set; } public System.Type GetRequestType() { - return Request?.GetRequestType(); + return Request?.GetType(); } } } \ No newline at end of file diff --git a/Alexa.NET/Request/Type/AudioPlayerRequest.cs b/Alexa.NET/Request/Type/AudioPlayerRequest.cs new file mode 100644 index 0000000..e7573ad --- /dev/null +++ b/Alexa.NET/Request/Type/AudioPlayerRequest.cs @@ -0,0 +1,26 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Alexa.NET.Request.Type +{ + public class AudioPlayerRequest: Request + { + [JsonProperty("token")] + string Token { get; set; } + + [JsonProperty("locale")] + string Locale { get; set; } + + [JsonProperty("offsetInMilliseconds")] + string OffsetInMilliseconds { get; set; } + + [JsonProperty("error")] + Error Error { get; set; } + + [JsonProperty("currentPlaybackState")] + PlaybackState CurrentPlaybackState { get; set; } + } +} diff --git a/Alexa.NET/Request/Type/Error.cs b/Alexa.NET/Request/Type/Error.cs new file mode 100644 index 0000000..39d3318 --- /dev/null +++ b/Alexa.NET/Request/Type/Error.cs @@ -0,0 +1,17 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Alexa.NET.Request.Type +{ + public class Error + { + [JsonProperty("type")] + string Type { get; set; } + + [JsonProperty("message")] + string Message { get; set; } + } +} diff --git a/Alexa.NET/Request/Type/IIntentRequest.cs b/Alexa.NET/Request/Type/IntentRequest.cs similarity index 64% rename from Alexa.NET/Request/Type/IIntentRequest.cs rename to Alexa.NET/Request/Type/IntentRequest.cs index 1d9a543..e5aa717 100644 --- a/Alexa.NET/Request/Type/IIntentRequest.cs +++ b/Alexa.NET/Request/Type/IntentRequest.cs @@ -1,6 +1,6 @@ namespace Alexa.NET.Request.Type { - public interface IIntentRequest : IRequest + public class IntentRequest : Request { Intent Intent { get; set; } } diff --git a/Alexa.NET/Request/Type/ILaunchRequest.cs b/Alexa.NET/Request/Type/LaunchRequest.cs similarity index 52% rename from Alexa.NET/Request/Type/ILaunchRequest.cs rename to Alexa.NET/Request/Type/LaunchRequest.cs index d73517a..e31c999 100644 --- a/Alexa.NET/Request/Type/ILaunchRequest.cs +++ b/Alexa.NET/Request/Type/LaunchRequest.cs @@ -1,6 +1,6 @@ namespace Alexa.NET.Request.Type { - public interface ILaunchRequest : IRequest + public class LaunchRequest : Request { } } \ No newline at end of file diff --git a/Alexa.NET/Request/Type/PlaybackState.cs b/Alexa.NET/Request/Type/PlaybackState.cs new file mode 100644 index 0000000..539a86d --- /dev/null +++ b/Alexa.NET/Request/Type/PlaybackState.cs @@ -0,0 +1,20 @@ +using Newtonsoft.Json; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Threading.Tasks; + +namespace Alexa.NET.Request.Type +{ + public class PlaybackState + { + [JsonProperty("token")] + string Token { get; set; } + + [JsonProperty("offsetInMilliseconds")] + string OffsetInMilliseconds { get; set; } + + [JsonProperty("playerActivity")] + string PlayerActivity { get; set; } + } +} diff --git a/Alexa.NET/Request/Type/IRequest.cs b/Alexa.NET/Request/Type/Request.cs similarity index 89% rename from Alexa.NET/Request/Type/IRequest.cs rename to Alexa.NET/Request/Type/Request.cs index f1e4fd9..171b426 100644 --- a/Alexa.NET/Request/Type/IRequest.cs +++ b/Alexa.NET/Request/Type/Request.cs @@ -3,7 +3,7 @@ namespace Alexa.NET.Request.Type { - public interface IRequest + public abstract class Request { [JsonProperty("type")] string Type { get; set; } diff --git a/Alexa.NET/Request/Type/RequestBundle.cs b/Alexa.NET/Request/Type/RequestBundle.cs deleted file mode 100644 index 46f30f9..0000000 --- a/Alexa.NET/Request/Type/RequestBundle.cs +++ /dev/null @@ -1,38 +0,0 @@ -using Newtonsoft.Json; -using System; - -namespace Alexa.NET.Request.Type -{ - public class RequestBundle : IIntentRequest, ILaunchRequest, ISessionEndedRequest - { - [JsonProperty("type")] - public string Type { get; set; } - - [JsonProperty("requestId")] - public string RequestId { get; set; } - - [JsonProperty("timestamp")] - public DateTime Timestamp { get; set; } - - [JsonProperty("intent")] - public Intent Intent { get; set; } - - [JsonProperty("reason")] - public string Reason { get; set; } - - public System.Type GetRequestType() - { - switch (Type) - { - case "IntentRequest": - return typeof(IIntentRequest); - case "LaunchRequest": - return typeof(ILaunchRequest); - case "SessionEndedRequest": - return typeof(ISessionEndedRequest); - default: - throw new ArgumentOutOfRangeException(nameof(Type), $"Unknown request type: {Type}."); - } - } - } -} \ No newline at end of file diff --git a/Alexa.NET/Request/Type/RequestConverter.cs b/Alexa.NET/Request/Type/RequestConverter.cs new file mode 100644 index 0000000..3bc8c2f --- /dev/null +++ b/Alexa.NET/Request/Type/RequestConverter.cs @@ -0,0 +1,56 @@ +using Newtonsoft.Json; +using Newtonsoft.Json.Linq; +using System; + +namespace Alexa.NET.Request.Type +{ + public class RequestConverter : JsonConverter + { + public override bool CanWrite => false; + + public override bool CanConvert(System.Type objectType) + { + return objectType == typeof(Request); + } + + public override void WriteJson(JsonWriter writer, object value, JsonSerializer serializer) + { + throw new NotImplementedException(); + } + + public override object ReadJson(JsonReader reader, System.Type objectType, object existingValue, JsonSerializer serializer) + { + // Load JObject from stream + var jObject = JObject.Load(reader); + + // Create target request object based on "type" property + var target = Create(jObject["type"].Value()); + + // Populate the object properties + serializer.Populate(jObject.CreateReader(), target); + + return target; + } + + public Request Create(string requestType) + { + //AudioPlayer requests are very similar, map to single type + if (requestType.StartsWith("AudioPlayer")) + requestType = "AudioPlayer"; + + switch (requestType) + { + case "IntentRequest": + return new IntentRequest(); + case "LaunchRequest": + return new LaunchRequest(); + case "SessionEndedRequest": + return new SessionEndedRequest(); + case "AudioPlayer": + return new AudioPlayerRequest(); + default: + throw new ArgumentOutOfRangeException(nameof(Type), $"Unknown request type: {requestType}."); + } + } + } +} \ No newline at end of file diff --git a/Alexa.NET/Request/Type/ISessionEndedRequest.cs b/Alexa.NET/Request/Type/SessionEndedRequest.cs similarity index 72% rename from Alexa.NET/Request/Type/ISessionEndedRequest.cs rename to Alexa.NET/Request/Type/SessionEndedRequest.cs index bd8b40a..7623e3c 100644 --- a/Alexa.NET/Request/Type/ISessionEndedRequest.cs +++ b/Alexa.NET/Request/Type/SessionEndedRequest.cs @@ -2,7 +2,7 @@ namespace Alexa.NET.Request.Type { - public interface ISessionEndedRequest : IRequest + public class SessionEndedRequest : Request { [JsonProperty("reason")] string Reason { get; set; }