Skip to content

Commit

Permalink
Refactor request deserialization to JsonConverter & add AudioPlayer r…
Browse files Browse the repository at this point in the history
…equest
  • Loading branch information
tobiasviehweger committed Dec 29, 2016
1 parent 6c7a2ab commit 32c2a3e
Show file tree
Hide file tree
Showing 11 changed files with 128 additions and 84 deletions.
39 changes: 0 additions & 39 deletions Alexa.NET/Request/RequestBundle.cs

This file was deleted.

8 changes: 5 additions & 3 deletions Alexa.NET/Request/SkillRequest.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json;
using Alexa.NET.Request.Type;
using Newtonsoft.Json;

namespace Alexa.NET.Request
{
Expand All @@ -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();
}
}
}
26 changes: 26 additions & 0 deletions Alexa.NET/Request/Type/AudioPlayerRequest.cs
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; }
}
}
17 changes: 17 additions & 0 deletions Alexa.NET/Request/Type/Error.cs
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; }
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Alexa.NET.Request.Type
{
public interface IIntentRequest : IRequest
public class IntentRequest : Request
{
Intent Intent { get; set; }
}
Expand Down
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
{
}
}
20 changes: 20 additions & 0 deletions Alexa.NET/Request/Type/PlaybackState.cs
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; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace Alexa.NET.Request.Type
{
public interface IRequest
public abstract class Request
{
[JsonProperty("type")]
string Type { get; set; }
Expand Down
38 changes: 0 additions & 38 deletions Alexa.NET/Request/Type/RequestBundle.cs

This file was deleted.

56 changes: 56 additions & 0 deletions Alexa.NET/Request/Type/RequestConverter.cs
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}.");
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Alexa.NET.Request.Type
{
public interface ISessionEndedRequest : IRequest
public class SessionEndedRequest : Request
{
[JsonProperty("reason")]
string Reason { get; set; }
Expand Down

0 comments on commit 32c2a3e

Please sign in to comment.