Created
July 14, 2012 10:50
-
-
Save fantasticswallow/3110539 to your computer and use it in GitHub Desktop.
ReactiveOAuthのラッパー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Net; | |
using Microsoft.Phone.Reactive; | |
using Codeplex.OAuth; | |
namespace TestPhoneApp1 | |
{ | |
public class TwitterConnectionCS | |
{ | |
public delegate void resultTextCollection(string resTxt); | |
public delegate void resultResponseCollection(WebResponse res); | |
public delegate void exceptionHandler(Exception ex); | |
/// <summary> | |
/// GETリクエストを行います | |
/// </summary> | |
/// <param name="url">通信先のurl</param> | |
/// <param name="param">ヘッダに追加する引数</param> | |
/// <param name="argOperate">レスポンスを処理するデリゲート</param> | |
/// <param name="exHandler">例外を処理するデリゲート</param> | |
public void GetRequest(string url, ParameterCollection param, resultResponseCollection argOperate,exceptionHandler exHandler) | |
{ | |
if (TwitterInfoCS.IsNotAuthed) | |
{ | |
exHandler(new Exception("Twitterに認証されていません、認証してからもう一度やり直してください")); | |
} | |
else | |
{ | |
var oauthClient = new OAuthClient(TwitterInfoCS.ConsumerKey, TwitterInfoCS.ConsumerSecret, TwitterInfoCS.OAuthToken, TwitterInfoCS.OAuthTokenSecret) | |
{ | |
Url = url, | |
Parameters = param, | |
ApplyBeforeRequest = req => { req.UserAgent = TwitterInfoCS.TwitterUserAgent; }, | |
}; | |
oauthClient.GetResponse().ObserveOnDispatcher() | |
.Subscribe(s => argOperate(s), x => exHandler(x)); | |
} | |
} | |
private IDisposable streamOAuthClient; | |
/// <summary> | |
/// StreamingAPIへの通信を行います | |
/// </summary> | |
/// <param name="url">通信先のurl</param> | |
/// <param name="param">ヘッダに追加する引数</param> | |
/// <param name="argOperate">レスポンスを処理するデリゲート</param> | |
/// <param name="exHandler">例外を処理するデリゲート</param> | |
public void GetRequestStream(string url, ParameterCollection param, resultTextCollection argOperate, exceptionHandler exHandler) | |
{ | |
if (TwitterInfoCS.IsNotAuthed) | |
{ | |
exHandler(new Exception("Twitterに認証されていません、認証してからもう一度やり直してください")); | |
} | |
else | |
{ | |
streamOAuthClient = new OAuthClient(TwitterInfoCS.ConsumerKey, TwitterInfoCS.ConsumerSecret, TwitterInfoCS.OAuthToken, TwitterInfoCS.OAuthTokenSecret) | |
{ | |
Url = url, | |
Parameters = param, | |
ApplyBeforeRequest = req => { req.UserAgent = TwitterInfoCS.TwitterUserAgent; }, | |
}.GetResponseLines().ObserveOnDispatcher() | |
.Subscribe(s => argOperate(s), x => exHandler(x)); | |
} | |
} | |
/// <summary> | |
/// Streaming通信を終了します | |
/// </summary> | |
public void StreamClientDispose() | |
{ | |
streamOAuthClient.Dispose(); | |
} | |
/// <summary> | |
/// POSTリクエストを行います | |
/// </summary> | |
/// <param name="url">通信先のurl</param> | |
/// <param name="param">ヘッダに追加する引数</param> | |
/// <param name="argOperate">レスポンスを処理するデリゲート</param> | |
/// <param name="exHandler">例外を処理するデリゲート</param> | |
public void PostRequest(string url, ParameterCollection param, resultResponseCollection argOperate, exceptionHandler exHandler) | |
{ | |
if (TwitterInfoCS.IsNotAuthed) | |
{ | |
exHandler(new Exception("Twitterに認証されていません、認証してからもう一度やり直してください")); | |
} | |
else | |
{ | |
new OAuthClient(TwitterInfoCS.ConsumerKey, TwitterInfoCS.ConsumerSecret, TwitterInfoCS.OAuthToken, TwitterInfoCS.OAuthTokenSecret) | |
{ | |
Url = url, | |
MethodType = MethodType.Post, | |
Parameters = param, | |
ApplyBeforeRequest = req => { req.UserAgent = TwitterInfoCS.TwitterUserAgent; }, | |
}.GetResponse() | |
.Subscribe(s => argOperate(s), x => exHandler(x)); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment