forked from timheuer/alexa-skills-dotnet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor request deserialization to JsonConverter & add AudioPlayer r…
…equest
- Loading branch information
1 parent
6c7a2ab
commit 32c2a3e
Showing
11 changed files
with
128 additions
and
84 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
2 changes: 1 addition & 1 deletion
2
Alexa.NET/Request/Type/IIntentRequest.cs → Alexa.NET/Request/Type/IntentRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
Alexa.NET/Request/Type/ILaunchRequest.cs → Alexa.NET/Request/Type/LaunchRequest.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
namespace Alexa.NET.Request.Type | ||
{ | ||
public interface ILaunchRequest : IRequest | ||
public class LaunchRequest : Request | ||
{ | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; } | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<string>()); | ||
|
||
// 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}."); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters