summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Arnott <andrewarnott@gmail.com>2013-05-27 09:46:47 -0700
committerAndrew Arnott <andrewarnott@gmail.com>2013-05-27 09:46:47 -0700
commit7fda43b0eece0204231bfbda881ada7e8fbb035b (patch)
tree105a74788a5849b0fbf827df094aa2f9d876cd50
parent5a0a8ee4c55f323a6c1fbdb619cd89b7d28a94ba (diff)
downloadDotNetOpenAuth-7fda43b0eece0204231bfbda881ada7e8fbb035b.zip
DotNetOpenAuth-7fda43b0eece0204231bfbda881ada7e8fbb035b.tar.gz
DotNetOpenAuth-7fda43b0eece0204231bfbda881ada7e8fbb035b.tar.bz2
Made AppBlock GetGraph methods async
-rw-r--r--samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Facebook/FacebookClient.cs17
-rw-r--r--samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs16
-rw-r--r--samples/DotNetOpenAuth.ApplicationBlock/OAuth2/WindowsLive/WindowsLiveClient.cs19
-rw-r--r--samples/OAuthClient/Facebook.aspx.cs2
-rw-r--r--samples/OAuthClient/Google.aspx.cs6
-rw-r--r--samples/OAuthClient/WindowsLive.aspx.cs2
6 files changed, 31 insertions, 31 deletions
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Facebook/FacebookClient.cs b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Facebook/FacebookClient.cs
index b8717f7..6d7b484 100644
--- a/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Facebook/FacebookClient.cs
+++ b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Facebook/FacebookClient.cs
@@ -10,7 +10,10 @@ namespace DotNetOpenAuth.ApplicationBlock {
using System.IO;
using System.Linq;
using System.Net;
+ using System.Net.Http;
using System.Text;
+ using System.Threading;
+ using System.Threading.Tasks;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2;
@@ -29,17 +32,15 @@ namespace DotNetOpenAuth.ApplicationBlock {
: base(FacebookDescription) {
}
- public IOAuth2Graph GetGraph(IAuthorizationState authState, string[] fields = null) {
+ public async Task<IOAuth2Graph> GetGraphAsync(IAuthorizationState authState, string[] fields = null, CancellationToken cancellationToken = default(CancellationToken)) {
if ((authState != null) && (authState.AccessToken != null)) {
+ var httpClient = new HttpClient(this.CreateAuthorizingHandler(authState));
string fieldsStr = (fields == null) || (fields.Length == 0) ? FacebookGraph.Fields.Defaults : string.Join(",", fields);
- WebRequest request = WebRequest.Create("https://graph.Facebook.com/me?access_token=" + Uri.EscapeDataString(authState.AccessToken) + "&fields=" + fieldsStr);
- WebResponse response = request.GetResponse();
-
- if (response != null) {
- Stream responseStream = response.GetResponseStream();
-
- if (responseStream != null) {
+ using (
+ var response = await httpClient.GetAsync("https://graph.Facebook.com/me?fields=" + fieldsStr, cancellationToken)) {
+ response.EnsureSuccessStatusCode();
+ using (var responseStream = await response.Content.ReadAsStreamAsync()) {
return FacebookGraph.Deserialize(responseStream);
}
}
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs
index 1e1a486..679955a 100644
--- a/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs
+++ b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/Google/GoogleClient.cs
@@ -10,7 +10,10 @@ namespace DotNetOpenAuth.ApplicationBlock {
using System.IO;
using System.Linq;
using System.Net;
+ using System.Net.Http;
using System.Text;
+ using System.Threading;
+ using System.Threading.Tasks;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2;
@@ -32,15 +35,12 @@ namespace DotNetOpenAuth.ApplicationBlock {
: base(GoogleDescription) {
}
- public IOAuth2Graph GetGraph(IAuthorizationState authState, string[] fields = null) {
+ public async Task<IOAuth2Graph> GetGraphAsync(IAuthorizationState authState, string[] fields = null, CancellationToken cancellationToken = default(CancellationToken)) {
if ((authState != null) && (authState.AccessToken != null)) {
- WebRequest request = WebRequest.Create("https://www.googleapis.com/oauth2/v1/userinfo?access_token=" + Uri.EscapeDataString(authState.AccessToken));
- WebResponse response = request.GetResponse();
-
- if (response != null) {
- Stream responseStream = response.GetResponseStream();
-
- if (responseStream != null) {
+ var httpClient = new HttpClient(this.CreateAuthorizingHandler(authState));
+ using (var response = await httpClient.GetAsync("https://www.googleapis.com/oauth2/v1/userinfo", cancellationToken)) {
+ response.EnsureSuccessStatusCode();
+ using (var responseStream = await response.Content.ReadAsStreamAsync()) {
return GoogleGraph.Deserialize(responseStream);
}
}
diff --git a/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/WindowsLive/WindowsLiveClient.cs b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/WindowsLive/WindowsLiveClient.cs
index 7f7af18..749ab0d 100644
--- a/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/WindowsLive/WindowsLiveClient.cs
+++ b/samples/DotNetOpenAuth.ApplicationBlock/OAuth2/WindowsLive/WindowsLiveClient.cs
@@ -10,7 +10,10 @@ namespace DotNetOpenAuth.ApplicationBlock {
using System.IO;
using System.Linq;
using System.Net;
+ using System.Net.Http;
using System.Text;
+ using System.Threading;
+ using System.Threading.Tasks;
using System.Web;
using DotNetOpenAuth.Messaging;
using DotNetOpenAuth.OAuth2;
@@ -29,15 +32,12 @@ namespace DotNetOpenAuth.ApplicationBlock {
: base(WindowsLiveDescription) {
}
- public IOAuth2Graph GetGraph(IAuthorizationState authState, string[] fields = null) {
+ public async Task<IOAuth2Graph> GetGraphAsync(IAuthorizationState authState, string[] fields = null, CancellationToken cancellationToken = default(CancellationToken)) {
if ((authState != null) && (authState.AccessToken != null)) {
- WebRequest request = WebRequest.Create("https://apis.live.net/v5.0/me?access_token=" + Uri.EscapeDataString(authState.AccessToken));
- WebResponse response = request.GetResponse();
-
- if (response != null) {
- Stream responseStream = response.GetResponseStream();
-
- if (responseStream != null) {
+ var httpClient = new HttpClient(this.CreateAuthorizingHandler(authState));
+ using (var response = await httpClient.GetAsync("https://apis.live.net/v5.0/me", cancellationToken)) {
+ response.EnsureSuccessStatusCode();
+ using (var responseStream = await response.Content.ReadAsStreamAsync()) {
// string debugJsonStr = new StreamReader(responseStream).ReadToEnd();
WindowsLiveGraph windowsLiveGraph = WindowsLiveGraph.Deserialize(responseStream);
@@ -45,7 +45,8 @@ namespace DotNetOpenAuth.ApplicationBlock {
// &type=small 96x96
// &type=medium 96x96
// &type=large 448x448
- windowsLiveGraph.AvatarUrl = new Uri("https://apis.live.net/v5.0/me/picture?access_token=" + Uri.EscapeDataString(authState.AccessToken));
+ windowsLiveGraph.AvatarUrl =
+ new Uri("https://apis.live.net/v5.0/me/picture?access_token=" + Uri.EscapeDataString(authState.AccessToken));
return windowsLiveGraph;
}
diff --git a/samples/OAuthClient/Facebook.aspx.cs b/samples/OAuthClient/Facebook.aspx.cs
index 5afc945..101b7f6 100644
--- a/samples/OAuthClient/Facebook.aspx.cs
+++ b/samples/OAuthClient/Facebook.aspx.cs
@@ -32,7 +32,7 @@
// alternatively you can ask for more information
// facebookClient.RequestUserAuthorization(scope: new[] { FacebookClient.Scopes.Email, FacebookClient.Scopes.UserBirthday });
} else {
- IOAuth2Graph oauth2Graph = facebookClient.GetGraph(authorization);
+ IOAuth2Graph oauth2Graph = await facebookClient.GetGraphAsync(authorization, cancellationToken: ct);
//// IOAuth2Graph oauth2Graph = facebookClient.GetGraph(authorization, new[] { FacebookGraph.Fields.Defaults, FacebookGraph.Fields.Email, FacebookGraph.Fields.Picture, FacebookGraph.Fields.Birthday });
this.nameLabel.Text = HttpUtility.HtmlEncode(oauth2Graph.Name);
diff --git a/samples/OAuthClient/Google.aspx.cs b/samples/OAuthClient/Google.aspx.cs
index 7d0c77e..e0c4941 100644
--- a/samples/OAuthClient/Google.aspx.cs
+++ b/samples/OAuthClient/Google.aspx.cs
@@ -5,11 +5,9 @@
using System.Net;
using System.Web;
using System.Web.UI;
-
using DotNetOpenAuth.ApplicationBlock;
- using DotNetOpenAuth.OAuth2;
-
using DotNetOpenAuth.Messaging;
+ using DotNetOpenAuth.OAuth2;
public partial class Google : System.Web.UI.Page
{
@@ -33,7 +31,7 @@
// alternatively you can ask for more information
// googleClient.RequestUserAuthorization(scope: new[] { GoogleClient.Scopes.UserInfo.Profile, GoogleClient.Scopes.UserInfo.Email });
} else {
- IOAuth2Graph oauth2Graph = googleClient.GetGraph(authorization);
+ IOAuth2Graph oauth2Graph = await googleClient.GetGraphAsync(authorization, cancellationToken: ct);
this.nameLabel.Text = HttpUtility.HtmlEncode(oauth2Graph.Name);
}
diff --git a/samples/OAuthClient/WindowsLive.aspx.cs b/samples/OAuthClient/WindowsLive.aspx.cs
index 61dc443..e2adad9 100644
--- a/samples/OAuthClient/WindowsLive.aspx.cs
+++ b/samples/OAuthClient/WindowsLive.aspx.cs
@@ -43,7 +43,7 @@
// alternatively you can ask for more information
// windowsLiveClient.RequestUserAuthorization(scope: new[] { WindowsLiveClient.Scopes.SignIn, WindowsLiveClient.Scopes.Emails, WindowsLiveClient.Scopes.Birthday });
} else {
- IOAuth2Graph oauth2Graph = windowsLiveClient.GetGraph(authorization);
+ IOAuth2Graph oauth2Graph = await windowsLiveClient.GetGraphAsync(authorization, cancellationToken: ct);
this.nameLabel.Text = HttpUtility.HtmlEncode(oauth2Graph.Name);
}