add Reader Tests; add PocketArticle response object
This commit is contained in:
@@ -8,17 +8,39 @@ namespace PocketSharp.Tests
|
||||
{
|
||||
public class ReadTests : TestsBase
|
||||
{
|
||||
public ReadTests() : base() { }
|
||||
private PocketReader reader;
|
||||
|
||||
|
||||
public ReadTests() : base()
|
||||
{
|
||||
reader = new PocketReader();
|
||||
}
|
||||
|
||||
|
||||
[Fact]
|
||||
public async Task TemporaryReadTest()
|
||||
public async Task ReadArticleTest()
|
||||
{
|
||||
PocketReader reader = new PocketReader();
|
||||
PocketArticle result = await reader.Read(new PocketItem()
|
||||
{
|
||||
ID = 99,
|
||||
Uri = new Uri("http://frontendplay.com/story/4/http-caching-demystified-part-2-implementation")
|
||||
});
|
||||
|
||||
string result = await reader.Read(new Uri("http://frontendplay.com/story/4/http-caching-demystified-part-2-implementation"));
|
||||
Assert.True(result.Content.Length > 15000);
|
||||
}
|
||||
|
||||
Assert.True(result.Length > 15000);
|
||||
|
||||
[Fact]
|
||||
public async Task ReadArticleWithInvalidUriTest()
|
||||
{
|
||||
await ThrowsAsync<PocketException>(async () =>
|
||||
{
|
||||
await reader.Read(new PocketItem()
|
||||
{
|
||||
ID = 99,
|
||||
Uri = new Uri("http://frontendplayyyyy.com")
|
||||
});
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
|
||||
namespace PocketSharp.Models
|
||||
{
|
||||
/// <summary>
|
||||
/// Readable article
|
||||
/// </summary>
|
||||
public class PocketArticle
|
||||
{
|
||||
/// <summary>
|
||||
/// Gets or sets the pocket item ID.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The pocket item ID.
|
||||
/// </value>
|
||||
public int PocketItemID { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the content.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The content.
|
||||
/// </value>
|
||||
public string Content { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the title.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The title.
|
||||
/// </value>
|
||||
public string Title { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the next page URL.
|
||||
/// </summary>
|
||||
/// <value>
|
||||
/// The next page URL.
|
||||
/// </value>
|
||||
public string NextPageUrl { get; set; }
|
||||
}
|
||||
}
|
||||
+34
-45
@@ -15,7 +15,7 @@ namespace PocketSharp
|
||||
/// <summary>
|
||||
/// REST client used for HTML retrieval
|
||||
/// </summary>
|
||||
protected readonly HttpClient _restClient;
|
||||
protected readonly HttpClient _httpClient;
|
||||
|
||||
|
||||
/// <summary>
|
||||
@@ -23,13 +23,22 @@ namespace PocketSharp
|
||||
/// </summary>
|
||||
public PocketReader()
|
||||
{
|
||||
// initialize REST client
|
||||
_restClient = new HttpClient(new HttpClientHandler()
|
||||
// initialize HTTP client
|
||||
_httpClient = new HttpClient(new HttpClientHandler()
|
||||
{
|
||||
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip
|
||||
AutomaticDecompression = DecompressionMethods.Deflate | DecompressionMethods.GZip,
|
||||
AllowAutoRedirect = true,
|
||||
MaxAutomaticRedirections = 10
|
||||
});
|
||||
|
||||
_restClient.DefaultRequestHeaders.Add("Accept", "text/html");
|
||||
// add accept types
|
||||
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8");
|
||||
|
||||
// add accepted encodings
|
||||
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("Accept-Encoding", "gzip,deflate");
|
||||
|
||||
// add user agent (default for Opera with PocketSharp identifier appended)
|
||||
_httpClient.DefaultRequestHeaders.TryAddWithoutValidation("User-Agent", "Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.4 Safari/537.36 OPR/18.0.1284.2 PocketSharp/2.0");
|
||||
}
|
||||
|
||||
|
||||
@@ -39,19 +48,8 @@ namespace PocketSharp
|
||||
/// </summary>
|
||||
/// <param name="item">The pocket item.</param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> Read(PocketItem item)
|
||||
{
|
||||
return await Read(item.Uri);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reads content from the given URI.
|
||||
/// </summary>
|
||||
/// <param name="uri">The URI.</param>
|
||||
/// <returns></returns>
|
||||
public async Task<string> Read(Uri uri)
|
||||
/// <exception cref="PocketException"></exception>
|
||||
public async Task<PocketArticle> Read(PocketItem item)
|
||||
{
|
||||
// initialize transcoder
|
||||
NReadabilityTranscoder transcoder = new NReadabilityTranscoder(
|
||||
@@ -64,12 +62,12 @@ namespace PocketSharp
|
||||
);
|
||||
|
||||
// get HTML string from URI
|
||||
string htmlResponse = await Request(uri);
|
||||
string htmlResponse = await Request(item.Uri);
|
||||
|
||||
// set properties for processing
|
||||
TranscodingInput transcodingInput = new TranscodingInput(htmlResponse)
|
||||
{
|
||||
Url = uri.ToString(),
|
||||
Url = item.Uri.ToString(),
|
||||
DomSerializationParams = new DomSerializationParams()
|
||||
{
|
||||
BodyOnly = true,
|
||||
@@ -84,7 +82,13 @@ namespace PocketSharp
|
||||
// process/transcode HTML
|
||||
TranscodingResult transcodingResult = transcoder.Transcode(transcodingInput);
|
||||
|
||||
return transcodingResult.ExtractedContent;
|
||||
return new PocketArticle()
|
||||
{
|
||||
Content = transcodingResult.ExtractedContent,
|
||||
Title = transcodingResult.ExtractedTitle,
|
||||
NextPageUrl = transcodingResult.NextPageUrl,
|
||||
PocketItemID = item.ID
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@@ -92,42 +96,27 @@ namespace PocketSharp
|
||||
/// <summary>
|
||||
/// Fetches a resource
|
||||
/// </summary>
|
||||
/// <param name="uri">The URI.</param>
|
||||
/// <returns></returns>
|
||||
protected async Task<string> Request(Uri uri)
|
||||
{
|
||||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
|
||||
|
||||
// make async request
|
||||
HttpResponseMessage response = await _restClient.SendAsync(request);
|
||||
HttpResponseMessage response = await _httpClient.SendAsync(request);
|
||||
|
||||
// validate HTTP response
|
||||
ValidateResponse(response);
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
{
|
||||
string exceptionString = String.Format("Request error: {0} ({1})", response.ReasonPhrase, (int)response.StatusCode);
|
||||
|
||||
throw new PocketException(exceptionString);
|
||||
}
|
||||
|
||||
// read response
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
|
||||
return responseString;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Validates the response.
|
||||
/// </summary>
|
||||
/// <param name="response">The response.</param>
|
||||
/// <returns></returns>
|
||||
/// <exception cref="PocketException">
|
||||
/// Error retrieving response
|
||||
/// </exception>
|
||||
protected void ValidateResponse(HttpResponseMessage response)
|
||||
{
|
||||
// no error found
|
||||
if (response.StatusCode == HttpStatusCode.OK)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
string exceptionString = String.Format("Request error: {0} ({1})", response.ReasonPhrase, (int)response.StatusCode);
|
||||
|
||||
throw new PocketException(exceptionString);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -41,6 +41,7 @@
|
||||
<ItemGroup>
|
||||
<Compile Include="Components\Statistics.cs" />
|
||||
<Compile Include="Models\Parameters\RegisterParameters.cs" />
|
||||
<Compile Include="Models\PocketArticle.cs" />
|
||||
<Compile Include="Models\PocketStatistics.cs" />
|
||||
<Compile Include="PocketException.cs" />
|
||||
<Compile Include="Components\Add.cs" />
|
||||
|
||||
Reference in New Issue
Block a user