move methods into single class

This commit is contained in:
2015-04-20 23:51:31 +02:00
parent a5dde84207
commit 33ae03a4b0
4 changed files with 69 additions and 83 deletions
-70
View File
@@ -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
{
/// <summary>
/// Get a list of reviews (up to 20 per request) for the specified app.
/// </summary>
/// <param name="appId">The ID of the app. Can be found in the dev portal or the store URI.</param>
/// <param name="store">The store where the app is published.</param>
/// <param name="storeCulture">Culture of the query (returns location specific metadata + ratings).</param>
/// <param name="sorting">Sorting criteria.</param>
/// <returns></returns>
public async Task<AppReviews> 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<XElement> 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<XElement> 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;
}
}
}
@@ -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
{
/// <summary>
/// Searches for apps based on a term (app name, keywords, ..)
@@ -151,6 +150,65 @@ namespace OnePeek.Api
/// <summary>
/// Get a list of reviews (up to 20 per request) for the specified app.
/// </summary>
/// <param name="appId">The ID of the app. Can be found in the dev portal or the store URI.</param>
/// <param name="store">The store where the app is published.</param>
/// <param name="storeCulture">Culture of the query (returns location specific metadata + ratings).</param>
/// <param name="sorting">Sorting criteria.</param>
/// <returns></returns>
public async Task<AppReviews> 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<XElement> 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<XElement> 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;
}
/// <summary>
/// Creates an URI from an image urn (included in the AppImage POCO).
/// </summary>
+5 -6
View File
@@ -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<AppRating> ratings = await metaEndpoint.GetRatingsForAllCultures(ctx.id, StoreType.WindowsPhone8, new System.Threading.CancellationTokenSource().Token, null);
IEnumerable<AppRating> 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");
};
}
+4 -5
View File
@@ -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<AppRating> ratings = await metaEndpoint.GetRatingsForAllCultures(Request.Query["id"], StoreType.WindowsPhone8, new System.Threading.CancellationTokenSource().Token, null);
IEnumerable<AppRating> ratings = await api.GetRatingsForAllCultures(Request.Query["id"], StoreType.WindowsPhone8, new System.Threading.CancellationTokenSource().Token, null);
if (order == "rating")
{