merge httpclient+restparserclient; add RequestAsString method
This commit is contained in:
+20
-2
@@ -1,6 +1,6 @@
|
|||||||
Microsoft Visual Studio Solution File, Format Version 12.00
|
Microsoft Visual Studio Solution File, Format Version 12.00
|
||||||
# Visual Studio 15
|
# Visual Studio 15
|
||||||
VisualStudioVersion = 15.0.27428.2027
|
VisualStudioVersion = 15.0.27130.2027
|
||||||
MinimumVisualStudioVersion = 10.0.40219.1
|
MinimumVisualStudioVersion = 10.0.40219.1
|
||||||
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PocketSharp.Examples", "PocketSharp.Examples", "{AFD4E3F1-B23C-4924-BA5B-D917FBADB8FA}"
|
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "PocketSharp.Examples", "PocketSharp.Examples", "{AFD4E3F1-B23C-4924-BA5B-D917FBADB8FA}"
|
||||||
EndProject
|
EndProject
|
||||||
@@ -15,7 +15,9 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PocketSharp.UWP", "PocketSh
|
|||||||
EndProject
|
EndProject
|
||||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PocketSharp", "PocketSharp\PocketSharp.csproj", "{0E22926C-7244-4A17-923C-84B46F5149C0}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PocketSharp", "PocketSharp\PocketSharp.csproj", "{0E22926C-7244-4A17-923C-84B46F5149C0}"
|
||||||
EndProject
|
EndProject
|
||||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PocketSharp.Tests", "PocketSharp.Tests\PocketSharp.Tests.csproj", "{3BAA93AD-3B5C-47C4-816B-F29A1E6D1C2D}"
|
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "PocketSharp.Tests", "PocketSharp.Tests\PocketSharp.Tests.csproj", "{3BAA93AD-3B5C-47C4-816B-F29A1E6D1C2D}"
|
||||||
|
EndProject
|
||||||
|
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PocketSharp.Explore", "PocketSharp.Explore\PocketSharp.Explore.csproj", "{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}"
|
||||||
EndProject
|
EndProject
|
||||||
Global
|
Global
|
||||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||||
@@ -83,6 +85,22 @@ Global
|
|||||||
{3BAA93AD-3B5C-47C4-816B-F29A1E6D1C2D}.Release|x64.Build.0 = Release|Any CPU
|
{3BAA93AD-3B5C-47C4-816B-F29A1E6D1C2D}.Release|x64.Build.0 = Release|Any CPU
|
||||||
{3BAA93AD-3B5C-47C4-816B-F29A1E6D1C2D}.Release|x86.ActiveCfg = Release|Any CPU
|
{3BAA93AD-3B5C-47C4-816B-F29A1E6D1C2D}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
{3BAA93AD-3B5C-47C4-816B-F29A1E6D1C2D}.Release|x86.Build.0 = Release|Any CPU
|
{3BAA93AD-3B5C-47C4-816B-F29A1E6D1C2D}.Release|x86.Build.0 = Release|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Debug|ARM.ActiveCfg = Debug|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Debug|ARM.Build.0 = Debug|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Debug|x64.ActiveCfg = Debug|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Debug|x64.Build.0 = Debug|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Debug|x86.ActiveCfg = Debug|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Debug|x86.Build.0 = Debug|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Release|ARM.ActiveCfg = Release|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Release|ARM.Build.0 = Release|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Release|x64.ActiveCfg = Release|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Release|x64.Build.0 = Release|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Release|x86.ActiveCfg = Release|Any CPU
|
||||||
|
{9A72F701-72B5-4400-AE9B-2814FE5FCBB1}.Release|x86.Build.0 = Release|Any CPU
|
||||||
EndGlobalSection
|
EndGlobalSection
|
||||||
GlobalSection(SolutionProperties) = preSolution
|
GlobalSection(SolutionProperties) = preSolution
|
||||||
HideSolutionNode = FALSE
|
HideSolutionNode = FALSE
|
||||||
|
|||||||
+84
-49
@@ -20,12 +20,7 @@ namespace PocketSharp
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// REST client used for the API communication
|
/// REST client used for the API communication
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected readonly HttpClient _restClient;
|
protected readonly HttpClient httpClient;
|
||||||
|
|
||||||
/// <summary>
|
|
||||||
/// REST client used for the API communication with the Text Parser API
|
|
||||||
/// </summary>
|
|
||||||
protected readonly HttpClient _restParserClient;
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Caches HTTP headers from last response
|
/// Caches HTTP headers from last response
|
||||||
@@ -40,7 +35,12 @@ namespace PocketSharp
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// The base URL for the Pocket API
|
/// The base URL for the Pocket API
|
||||||
/// </summary>
|
/// </summary>
|
||||||
protected static Uri baseUri = new Uri("https://getpocket.com/v3/");
|
protected static string baseUri = "https://getpocket.com/v3/";
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// The base URL for Pocket reader requests
|
||||||
|
/// </summary>
|
||||||
|
protected static string parserUri = null;
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// The authentification URL
|
/// The authentification URL
|
||||||
@@ -141,6 +141,7 @@ namespace PocketSharp
|
|||||||
this.isMobileClient = isMobileClient;
|
this.isMobileClient = isMobileClient;
|
||||||
this.useInsideWebAuthenticationBroker = useInsideWebAuthenticationBroker;
|
this.useInsideWebAuthenticationBroker = useInsideWebAuthenticationBroker;
|
||||||
this.cacheHTTPResponseData = cacheHTTPResponseData;
|
this.cacheHTTPResponseData = cacheHTTPResponseData;
|
||||||
|
PocketClient.parserUri = parserUri.OriginalString;
|
||||||
|
|
||||||
// assign access code if submitted
|
// assign access code if submitted
|
||||||
if (accessCode != null)
|
if (accessCode != null)
|
||||||
@@ -154,36 +155,19 @@ namespace PocketSharp
|
|||||||
CallbackUri = Uri.EscapeUriString(callbackUri.ToString());
|
CallbackUri = Uri.EscapeUriString(callbackUri.ToString());
|
||||||
}
|
}
|
||||||
|
|
||||||
// assign text parser if parserUri submitted
|
|
||||||
if (parserUri != null)
|
|
||||||
{
|
|
||||||
_restParserClient = new HttpClient(handler ?? new HttpClientHandler());
|
|
||||||
_restParserClient.BaseAddress = parserUri;
|
|
||||||
_restParserClient.DefaultRequestHeaders.Add("Accept", "*/*");
|
|
||||||
_restParserClient.DefaultRequestHeaders.Add("X-Accept", "application/json");
|
|
||||||
|
|
||||||
if (timeout.HasValue)
|
|
||||||
{
|
|
||||||
_restParserClient.Timeout = TimeSpan.FromSeconds(timeout.Value);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// initialize REST client
|
// initialize REST client
|
||||||
_restClient = new HttpClient(handler ?? new HttpClientHandler());
|
httpClient = new HttpClient(handler ?? new HttpClientHandler());
|
||||||
|
|
||||||
if (timeout.HasValue)
|
if (timeout.HasValue)
|
||||||
{
|
{
|
||||||
_restClient.Timeout = TimeSpan.FromSeconds(timeout.Value);
|
httpClient.Timeout = TimeSpan.FromSeconds(timeout.Value);
|
||||||
}
|
}
|
||||||
|
|
||||||
// set base uri
|
|
||||||
_restClient.BaseAddress = baseUri;
|
|
||||||
|
|
||||||
// Pocket needs this specific Accept header :-S
|
// Pocket needs this specific Accept header :-S
|
||||||
_restClient.DefaultRequestHeaders.Add("Accept", "*/*");
|
httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
|
||||||
|
|
||||||
// defines the response format (according to the Pocket docs)
|
// defines the response format (according to the Pocket docs)
|
||||||
_restClient.DefaultRequestHeaders.Add("X-Accept", "application/json");
|
httpClient.DefaultRequestHeaders.Add("X-Accept", "application/json");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -209,17 +193,15 @@ namespace PocketSharp
|
|||||||
throw new PocketException("SDK error: No access token available. Use authentication first.");
|
throw new PocketException("SDK error: No access token available. Use authentication first.");
|
||||||
}
|
}
|
||||||
|
|
||||||
// rewrite base if it is a request to the Parser API
|
string httpBase = isReaderRequest ? parserUri : baseUri;
|
||||||
if (isReaderRequest)
|
|
||||||
|
if (isReaderRequest && String.IsNullOrEmpty(parserUri))
|
||||||
{
|
{
|
||||||
if (_restParserClient == null)
|
throw new PocketException("Please pass a valid parserUri in the PocketClient ctor.");
|
||||||
{
|
|
||||||
throw new PocketException("Please pass the parserUri in the PocketClient ctor.");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// every single Pocket API endpoint requires HTTP POST data
|
// every single Pocket API endpoint requires HTTP POST data
|
||||||
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, method);
|
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, httpBase + method);
|
||||||
HttpResponseMessage response = null;
|
HttpResponseMessage response = null;
|
||||||
string responseString = null;
|
string responseString = null;
|
||||||
|
|
||||||
@@ -246,14 +228,7 @@ namespace PocketSharp
|
|||||||
// make async request
|
// make async request
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
if (isReaderRequest)
|
response = await httpClient.SendAsync(request, cancellationToken);
|
||||||
{
|
|
||||||
response = await _restParserClient.SendAsync(request, cancellationToken);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
response = await _restClient.SendAsync(request, cancellationToken);
|
|
||||||
}
|
|
||||||
|
|
||||||
// validate HTTP response
|
// validate HTTP response
|
||||||
ValidateResponse(response);
|
ValidateResponse(response);
|
||||||
@@ -298,6 +273,71 @@ namespace PocketSharp
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Fetches a string from a resource
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="uri">URI</param>
|
||||||
|
/// <param name="cancellationToken">The cancellation token.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
/// <exception cref="PocketException"></exception>
|
||||||
|
protected async Task<string> RequestAsString(string uri, CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
|
// every single Pocket API endpoint requires HTTP POST data
|
||||||
|
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, uri);
|
||||||
|
HttpResponseMessage response = null;
|
||||||
|
string responseString = null;
|
||||||
|
|
||||||
|
// call pre request action
|
||||||
|
PreRequest?.Invoke(uri);
|
||||||
|
|
||||||
|
// make async request
|
||||||
|
try
|
||||||
|
{
|
||||||
|
response = await httpClient.GetAsync(uri, cancellationToken);
|
||||||
|
|
||||||
|
// validate HTTP response
|
||||||
|
ValidateResponse(response);
|
||||||
|
|
||||||
|
// cache headers
|
||||||
|
if (cacheHTTPResponseData)
|
||||||
|
{
|
||||||
|
lastHeaders = response.Headers;
|
||||||
|
}
|
||||||
|
|
||||||
|
// read response
|
||||||
|
responseString = await response.Content.ReadAsStringAsync();
|
||||||
|
}
|
||||||
|
catch (HttpRequestException exc)
|
||||||
|
{
|
||||||
|
throw new PocketException(exc.Message, exc);
|
||||||
|
}
|
||||||
|
catch (PocketException exc)
|
||||||
|
{
|
||||||
|
throw exc;
|
||||||
|
}
|
||||||
|
finally
|
||||||
|
{
|
||||||
|
request.Dispose();
|
||||||
|
|
||||||
|
if (response != null)
|
||||||
|
{
|
||||||
|
response.Dispose();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// call after request action
|
||||||
|
AfterRequest?.Invoke(responseString);
|
||||||
|
|
||||||
|
// cache response
|
||||||
|
if (cacheHTTPResponseData)
|
||||||
|
{
|
||||||
|
lastResponseData = responseString;
|
||||||
|
}
|
||||||
|
|
||||||
|
return responseString;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts JSON to Pocket objects
|
/// Converts JSON to Pocket objects
|
||||||
/// </summary>
|
/// </summary>
|
||||||
@@ -451,13 +491,8 @@ namespace PocketSharp
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Dispose()
|
public void Dispose()
|
||||||
{
|
{
|
||||||
_restClient.Dispose();
|
httpClient.Dispose();
|
||||||
lastHeaders = null;
|
lastHeaders = null;
|
||||||
|
|
||||||
if (_restParserClient != null)
|
|
||||||
{
|
|
||||||
_restParserClient.Dispose();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user