move utilites; catch HttpRequestException; Read Article from URI
This commit is contained in:
@@ -40,6 +40,6 @@ namespace PocketSharp.Models
|
||||
/// <value>
|
||||
/// The next page URL.
|
||||
/// </value>
|
||||
public string NextPageUrl { get; set; }
|
||||
public Uri NextPage { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -112,6 +112,7 @@ namespace PocketSharp
|
||||
|
||||
// every single Pocket API endpoint requires HTTP POST data
|
||||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, method);
|
||||
HttpResponseMessage response = null;
|
||||
|
||||
if (parameters == null)
|
||||
{
|
||||
@@ -131,7 +132,14 @@ namespace PocketSharp
|
||||
request.Content = new FormUrlEncodedContent(parameters);
|
||||
|
||||
// make async request
|
||||
HttpResponseMessage response = await _restClient.SendAsync(request);
|
||||
try
|
||||
{
|
||||
response = await _restClient.SendAsync(request);
|
||||
}
|
||||
catch (HttpRequestException exc)
|
||||
{
|
||||
throw new PocketException(exc.Message, exc);
|
||||
}
|
||||
|
||||
// validate HTTP response
|
||||
ValidateResponse(response);
|
||||
|
||||
@@ -42,6 +42,25 @@ namespace PocketSharp
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reads article content from the given URI.
|
||||
/// This method does not use the official Article View API, which is private.
|
||||
/// The PocketReader is based on a custom PCL port of NReadability and SgmlReader.
|
||||
/// </summary>
|
||||
/// <param name="uri">An URI.</param>
|
||||
/// <returns>A Pocket article with extracted content and title.</returns>
|
||||
/// <exception cref="PocketRequestException"></exception>
|
||||
public async Task<PocketArticle> Read(Uri uri)
|
||||
{
|
||||
return await Read(new PocketItem()
|
||||
{
|
||||
ID = 0,
|
||||
Uri = uri
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Reads article content from the given PocketItem.
|
||||
/// This method does not use the official Article View API, which is private.
|
||||
@@ -49,7 +68,7 @@ namespace PocketSharp
|
||||
/// </summary>
|
||||
/// <param name="item">The pocket item.</param>
|
||||
/// <returns>A Pocket article with extracted content and title.</returns>
|
||||
/// <exception cref="PocketException"></exception>
|
||||
/// <exception cref="PocketRequestException"></exception>
|
||||
public async Task<PocketArticle> Read(PocketItem item)
|
||||
{
|
||||
// initialize transcoder
|
||||
@@ -87,7 +106,7 @@ namespace PocketSharp
|
||||
{
|
||||
Content = transcodingResult.ExtractedContent,
|
||||
Title = transcodingResult.ExtractedTitle,
|
||||
NextPageUrl = transcodingResult.NextPageUrl,
|
||||
NextPage = transcodingResult.NextPageUrl != null ? new Uri(transcodingResult.NextPageUrl, UriKind.Absolute) : null,
|
||||
PocketItemID = item.ID
|
||||
};
|
||||
}
|
||||
@@ -102,9 +121,17 @@ namespace PocketSharp
|
||||
protected async Task<string> Request(Uri uri)
|
||||
{
|
||||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
|
||||
HttpResponseMessage response = null;
|
||||
|
||||
// make async request
|
||||
HttpResponseMessage response = await _httpClient.SendAsync(request);
|
||||
try
|
||||
{
|
||||
response = await _httpClient.SendAsync(request);
|
||||
}
|
||||
catch (HttpRequestException exc)
|
||||
{
|
||||
throw new PocketException(exc.Message, exc);
|
||||
}
|
||||
|
||||
// validate HTTP response
|
||||
if (response.StatusCode != HttpStatusCode.OK)
|
||||
@@ -115,9 +142,7 @@ namespace PocketSharp
|
||||
}
|
||||
|
||||
// read response
|
||||
var responseString = await response.Content.ReadAsStringAsync();
|
||||
|
||||
return responseString;
|
||||
return await response.Content.ReadAsStringAsync();
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -43,13 +43,13 @@
|
||||
<Compile Include="Models\Parameters\RegisterParameters.cs" />
|
||||
<Compile Include="Models\PocketArticle.cs" />
|
||||
<Compile Include="Models\PocketStatistics.cs" />
|
||||
<Compile Include="PocketException.cs" />
|
||||
<Compile Include="Utilities\PocketException.cs" />
|
||||
<Compile Include="Components\Add.cs" />
|
||||
<Compile Include="Components\Account.cs" />
|
||||
<Compile Include="Components\Modify.cs" />
|
||||
<Compile Include="Components\ModifyTags.cs" />
|
||||
<Compile Include="Components\Get.cs" />
|
||||
<Compile Include="JsonExtensions.cs" />
|
||||
<Compile Include="Utilities\JsonExtensions.cs" />
|
||||
<Compile Include="Models\Parameters\ActionParameter.cs" />
|
||||
<Compile Include="Models\Parameters\AddParameters.cs" />
|
||||
<Compile Include="Models\Parameters\ModifyParameters.cs" />
|
||||
@@ -69,7 +69,7 @@
|
||||
<Compile Include="PocketClient.cs" />
|
||||
<Compile Include="PocketReader.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
<Compile Include="Utilities.cs" />
|
||||
<Compile Include="Utilities\Utilities.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Reference Include="Microsoft.Threading.Tasks">
|
||||
|
||||
Reference in New Issue
Block a user