diff --git a/src/OnePeek.Api/AppRatingEndpoint.cs b/src/OnePeek.Api/AppRatingEndpoint.cs deleted file mode 100644 index 4b4c5bf..0000000 --- a/src/OnePeek.Api/AppRatingEndpoint.cs +++ /dev/null @@ -1,70 +0,0 @@ -using OnePeek.Api.Extensions; -using OnePeek.Entities; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using System.Xml.Linq; - -namespace OnePeek.Api -{ - public class AppRatingEndpoint : ApiBase - { - /// - /// Get a list of reviews (up to 20 per request) for the specified app. - /// - /// The ID of the app. Can be found in the dev portal or the store URI. - /// The store where the app is published. - /// Culture of the query (returns location specific metadata + ratings). - /// Sorting criteria. - /// - public async Task GetReviews(string appId, StoreType store, StoreCultureType storeCulture, StoreReviewSorting sorting, string prevPageMarkerId = null, string nextPageMarkerId = null) - { - if (storeCulture == StoreCultureType.Unknown || storeCulture == StoreCultureType.All) - { - throw new ArgumentException("Please provide a valid store culture"); - } - - string xml = await ApiHttpClient.Instance.Get( - EndpointUris.GetWindowsPhoneReviewsUri(appId, storeCulture.ToString(), sorting.ToString(), prevPageMarkerId, nextPageMarkerId) - ); - - IEnumerable xel = XDocument.Parse(xml).Elements().First().Descendants(); - - AppReviews result = new AppReviews() - { - Id = appId, - StoreType = store, - StoreCultureType = storeCulture, - Sorting = sorting, - StoreDataModifiedDate = DateTime.Parse(xel.Get("updated")) - }; - - // parse markers - result.PrevPageMarkerId = Utils.GetQueryPart(xel.FirstOrDefault(x => x.Name.LocalName == "link" && x.Attribute("rel").Value == "prev"), "href", "beforeMarker"); - result.NextPageMarkerId = Utils.GetQueryPart(xel.FirstOrDefault(x => x.Name.LocalName == "link" && x.Attribute("rel").Value == "next"), "href", "afterMarker"); - - // append reviews - result.Reviews = xel.Where(x => x.Name.LocalName == "entry").Select(x => - { - IEnumerable childs = x.Descendants(); - byte rating = (byte)childs.GetFloat("userRating"); - - return new AppReview() - { - Id = childs.Get("reviewId"), - CreatedDate = DateTime.Parse(childs.Get("updated")), - Author = childs.FirstOrDefault(c => c.Name.LocalName == "author").Descendants().Get("name"), - Text = childs.Get("content"), - Rating = Configuration.UseFiveStarSystem ? (byte)(rating * 0.5) : rating, - Device = childs.Get("device"), - AppVersion = childs.Get("productVersion") - }; - }); - - result.IsEmpty = result.Reviews == null || !result.Reviews.Any(); - - return result; - } - } -} \ No newline at end of file diff --git a/src/OnePeek.Api/AppMetadataEndpoint.cs b/src/OnePeek.Api/OnePeekApi.cs similarity index 74% rename from src/OnePeek.Api/AppMetadataEndpoint.cs rename to src/OnePeek.Api/OnePeekApi.cs index 717434e..eb1379e 100644 --- a/src/OnePeek.Api/AppMetadataEndpoint.cs +++ b/src/OnePeek.Api/OnePeekApi.cs @@ -7,12 +7,11 @@ using System.Threading.Tasks; using System.Xml.Linq; using System.Linq; using System.Collections.Generic; -using System.Diagnostics; using System.Threading; namespace OnePeek.Api { - public class AppMetadataEndpoint : ApiBase + public class OnePeekApi : ApiBase { /// /// Searches for apps based on a term (app name, keywords, ..) @@ -151,6 +150,65 @@ namespace OnePeek.Api + /// + /// Get a list of reviews (up to 20 per request) for the specified app. + /// + /// The ID of the app. Can be found in the dev portal or the store URI. + /// The store where the app is published. + /// Culture of the query (returns location specific metadata + ratings). + /// Sorting criteria. + /// + public async Task GetReviews(string appId, StoreType store, StoreCultureType storeCulture, StoreReviewSorting sorting, string prevPageMarkerId = null, string nextPageMarkerId = null) + { + if (storeCulture == StoreCultureType.Unknown || storeCulture == StoreCultureType.All) + { + throw new ArgumentException("Please provide a valid store culture"); + } + + string xml = await ApiHttpClient.Instance.Get( + EndpointUris.GetWindowsPhoneReviewsUri(appId, storeCulture.ToString(), sorting.ToString(), prevPageMarkerId, nextPageMarkerId) + ); + + IEnumerable xel = XDocument.Parse(xml).Elements().First().Descendants(); + + AppReviews result = new AppReviews() + { + Id = appId, + StoreType = store, + StoreCultureType = storeCulture, + Sorting = sorting, + StoreDataModifiedDate = DateTime.Parse(xel.Get("updated")) + }; + + // parse markers + result.PrevPageMarkerId = Utils.GetQueryPart(xel.FirstOrDefault(x => x.Name.LocalName == "link" && x.Attribute("rel").Value == "prev"), "href", "beforeMarker"); + result.NextPageMarkerId = Utils.GetQueryPart(xel.FirstOrDefault(x => x.Name.LocalName == "link" && x.Attribute("rel").Value == "next"), "href", "afterMarker"); + + // append reviews + result.Reviews = xel.Where(x => x.Name.LocalName == "entry").Select(x => + { + IEnumerable childs = x.Descendants(); + byte rating = (byte)childs.GetFloat("userRating"); + + return new AppReview() + { + Id = childs.Get("reviewId"), + CreatedDate = DateTime.Parse(childs.Get("updated")), + Author = childs.FirstOrDefault(c => c.Name.LocalName == "author").Descendants().Get("name"), + Text = childs.Get("content"), + Rating = Configuration.UseFiveStarSystem ? (byte)(rating * 0.5) : rating, + Device = childs.Get("device"), + AppVersion = childs.Get("productVersion") + }; + }); + + result.IsEmpty = result.Reviews == null || !result.Reviews.Any(); + + return result; + } + + + /// /// Creates an URI from an image urn (included in the AppImage POCO). /// diff --git a/src/OnePeek.WebConsole/Modules/ApiModule.cs b/src/OnePeek.WebConsole/Modules/ApiModule.cs index edde6a6..cf6cc65 100644 --- a/src/OnePeek.WebConsole/Modules/ApiModule.cs +++ b/src/OnePeek.WebConsole/Modules/ApiModule.cs @@ -10,33 +10,32 @@ namespace OnePeek.WebConsole.Modules { public ApiModule() : base("api/") { - AppRatingEndpoint ratingEndpoint = new AppRatingEndpoint(); - AppMetadataEndpoint metaEndpoint = new AppMetadataEndpoint(); + OnePeekApi api = new OnePeekApi(); Get["/meta/{id}", true] = async (ctx, token) => { - AppMetadata meta = await metaEndpoint.GetMetadata(ctx.id, StoreType.WindowsPhone8, StoreCultureType.EN_US); + AppMetadata meta = await api.GetMetadata(ctx.id, StoreType.WindowsPhone8, StoreCultureType.EN_US); return Response.AsJson(meta); }; Get["/reviews/{id}", true] = async (ctx, token) => { - AppReviews reviews = await ratingEndpoint.GetReviews(ctx.id, StoreType.WindowsPhone8, StoreCultureType.EN_US, StoreReviewSorting.Latest); + AppReviews reviews = await api.GetReviews(ctx.id, StoreType.WindowsPhone8, StoreCultureType.EN_US, StoreReviewSorting.Latest); return Response.AsJson(reviews); }; Get["/ratings/{id}", true] = async (ctx, token) => { - IEnumerable ratings = await metaEndpoint.GetRatingsForAllCultures(ctx.id, StoreType.WindowsPhone8, new System.Threading.CancellationTokenSource().Token, null); + IEnumerable ratings = await api.GetRatingsForAllCultures(ctx.id, StoreType.WindowsPhone8, new System.Threading.CancellationTokenSource().Token, null); return Response.AsJson(ratings); }; Get["/image/{id}.jpg", true] = async (ctx, token) => { - Stream imageStream = await metaEndpoint.GetImageAsStream(ctx.id, StoreImageType.None); + Stream imageStream = await api.GetImageAsStream(ctx.id, StoreImageType.None); return Response.FromStream(imageStream, "image/jpeg"); }; } diff --git a/src/OnePeek.WebConsole/Modules/HomeModule.cs b/src/OnePeek.WebConsole/Modules/HomeModule.cs index 36362e1..06d2ce8 100644 --- a/src/OnePeek.WebConsole/Modules/HomeModule.cs +++ b/src/OnePeek.WebConsole/Modules/HomeModule.cs @@ -12,8 +12,7 @@ namespace OnePeek.WebConsole.Modules public HomeModule() { Configuration.UseFiveStarSystem = true; - AppRatingEndpoint ratingEndpoint = new AppRatingEndpoint(); - AppMetadataEndpoint metaEndpoint = new AppMetadataEndpoint(); + OnePeekApi api = new OnePeekApi(); Get["/"] = ctx => { @@ -22,21 +21,21 @@ namespace OnePeek.WebConsole.Modules Get["/meta", true] = async (ctx, token) => { - AppMetadata meta = await metaEndpoint.GetMetadata(Request.Query["id"], StoreType.WindowsPhone8, StoreCultureType.EN_US); + AppMetadata meta = await api.GetMetadata(Request.Query["id"], StoreType.WindowsPhone8, StoreCultureType.EN_US); return View["Meta", meta]; }; Get["/reviews", true] = async (ctx, token) => { - AppReviews reviews = await ratingEndpoint.GetReviews(Request.Query["id"], StoreType.WindowsPhone8, StoreCultureType.EN_US, StoreReviewSorting.Latest, Request.Query["prev"], Request.Query["next"]); + AppReviews reviews = await api.GetReviews(Request.Query["id"], StoreType.WindowsPhone8, StoreCultureType.EN_US, StoreReviewSorting.Latest, Request.Query["prev"], Request.Query["next"]); return View["Reviews", reviews]; }; Get["/ratings", true] = async (ctx, token) => { var order = Request.Query["order"]; - IEnumerable ratings = await metaEndpoint.GetRatingsForAllCultures(Request.Query["id"], StoreType.WindowsPhone8, new System.Threading.CancellationTokenSource().Token, null); + IEnumerable ratings = await api.GetRatingsForAllCultures(Request.Query["id"], StoreType.WindowsPhone8, new System.Threading.CancellationTokenSource().Token, null); if (order == "rating") {