Modern HTTP Client (#685)
parent
593a649045
commit
b7b5a6e7e1
@ -0,0 +1,12 @@
|
||||
using System.Net;
|
||||
|
||||
namespace NzbDrone.Common.Http
|
||||
{
|
||||
public class BasicNetworkCredential : NetworkCredential
|
||||
{
|
||||
public BasicNetworkCredential(string user, string pass)
|
||||
: base(user, pass)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,11 @@
|
||||
using System.Net.Http;
|
||||
using System.Net.Security;
|
||||
using System.Security.Cryptography.X509Certificates;
|
||||
|
||||
namespace NzbDrone.Common.Http.Dispatchers
|
||||
{
|
||||
public interface ICertificateValidationService
|
||||
{
|
||||
bool ShouldByPassValidationError(object sender, X509Certificate certificate, X509Chain chain, SslPolicyErrors sslPolicyErrors);
|
||||
}
|
||||
}
|
@ -0,0 +1,103 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Xml.Linq;
|
||||
using NLog;
|
||||
using NzbDrone.Common.Instrumentation;
|
||||
|
||||
namespace NzbDrone.Common.Http
|
||||
{
|
||||
public class XmlRpcRequestBuilder : HttpRequestBuilder
|
||||
{
|
||||
public static string XmlRpcContentType = "text/xml";
|
||||
|
||||
private static readonly Logger Logger = NzbDroneLogger.GetLogger(typeof(XmlRpcRequestBuilder));
|
||||
|
||||
public string XmlMethod { get; private set; }
|
||||
public List<object> XmlParameters { get; private set; }
|
||||
|
||||
public XmlRpcRequestBuilder(string baseUrl)
|
||||
: base(baseUrl)
|
||||
{
|
||||
Method = HttpMethod.Post;
|
||||
XmlParameters = new List<object>();
|
||||
}
|
||||
|
||||
public XmlRpcRequestBuilder(bool useHttps, string host, int port, string urlBase = null)
|
||||
: this(BuildBaseUrl(useHttps, host, port, urlBase))
|
||||
{
|
||||
}
|
||||
|
||||
public override HttpRequestBuilder Clone()
|
||||
{
|
||||
var clone = base.Clone() as XmlRpcRequestBuilder;
|
||||
clone.XmlParameters = new List<object>(XmlParameters);
|
||||
return clone;
|
||||
}
|
||||
|
||||
public XmlRpcRequestBuilder Call(string method, params object[] parameters)
|
||||
{
|
||||
var clone = Clone() as XmlRpcRequestBuilder;
|
||||
clone.XmlMethod = method;
|
||||
clone.XmlParameters = parameters.ToList();
|
||||
return clone;
|
||||
}
|
||||
|
||||
protected override void Apply(HttpRequest request)
|
||||
{
|
||||
base.Apply(request);
|
||||
|
||||
request.Headers.ContentType = XmlRpcContentType;
|
||||
|
||||
var methodCallElements = new List<XElement> { new XElement("methodName", XmlMethod) };
|
||||
|
||||
if (XmlParameters.Any())
|
||||
{
|
||||
var argElements = XmlParameters.Select(x => new XElement("param", ConvertParameter(x))).ToList();
|
||||
var paramsElement = new XElement("params", argElements);
|
||||
methodCallElements.Add(paramsElement);
|
||||
}
|
||||
|
||||
var message = new XDocument(
|
||||
new XDeclaration("1.0", "utf-8", "yes"),
|
||||
new XElement("methodCall", methodCallElements));
|
||||
|
||||
var body = message.ToString();
|
||||
|
||||
Logger.Debug($"Executing remote method: {XmlMethod}");
|
||||
|
||||
Logger.Trace($"methodCall {XmlMethod} body:\n{body}");
|
||||
|
||||
request.SetContent(body);
|
||||
}
|
||||
|
||||
private static XElement ConvertParameter(object value)
|
||||
{
|
||||
XElement data;
|
||||
|
||||
if (value is string s)
|
||||
{
|
||||
data = new XElement("string", s);
|
||||
}
|
||||
else if (value is List<string> l)
|
||||
{
|
||||
data = new XElement("array", new XElement("data", l.Select(x => new XElement("value", new XElement("string", x)))));
|
||||
}
|
||||
else if (value is int i)
|
||||
{
|
||||
data = new XElement("int", i);
|
||||
}
|
||||
else if (value is byte[] bytes)
|
||||
{
|
||||
data = new XElement("base64", Convert.ToBase64String(bytes));
|
||||
}
|
||||
else
|
||||
{
|
||||
throw new InvalidOperationException($"Unhandled argument type {value.GetType().Name}");
|
||||
}
|
||||
|
||||
return new XElement("value", data);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,111 +1,161 @@
|
||||
using CookComputing.XmlRpc;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Xml.Linq;
|
||||
using System.Xml.XPath;
|
||||
using NzbDrone.Core.Download.Extensions;
|
||||
|
||||
namespace NzbDrone.Core.Download.Clients.Aria2
|
||||
{
|
||||
public class Aria2Version
|
||||
public class Aria2Fault
|
||||
{
|
||||
[XmlRpcMember("version")]
|
||||
public string Version;
|
||||
|
||||
[XmlRpcMember("enabledFeatures")]
|
||||
public string[] EnabledFeatures;
|
||||
public Aria2Fault(XElement element)
|
||||
{
|
||||
foreach (var e in element.XPathSelectElements("./value/struct/member"))
|
||||
{
|
||||
var name = e.ElementAsString("name");
|
||||
if (name == "faultCode")
|
||||
{
|
||||
FaultCode = e.Element("value").ElementAsInt("int");
|
||||
}
|
||||
else if (name == "faultString")
|
||||
{
|
||||
FaultString = e.Element("value").GetStringValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int FaultCode { get; set; }
|
||||
public string FaultString { get; set; }
|
||||
}
|
||||
|
||||
public class Aria2Uri
|
||||
public class Aria2Version
|
||||
{
|
||||
[XmlRpcMember("status")]
|
||||
public string Status;
|
||||
|
||||
[XmlRpcMember("uri")]
|
||||
public string Uri;
|
||||
public Aria2Version(XElement element)
|
||||
{
|
||||
foreach (var e in element.XPathSelectElements("./struct/member"))
|
||||
{
|
||||
if (e.ElementAsString("name") == "version")
|
||||
{
|
||||
Version = e.Element("value").GetStringValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Version { get; set; }
|
||||
}
|
||||
|
||||
public class Aria2File
|
||||
{
|
||||
[XmlRpcMember("index")]
|
||||
public string Index;
|
||||
|
||||
[XmlRpcMember("length")]
|
||||
public string Length;
|
||||
public Aria2File(XElement element)
|
||||
{
|
||||
foreach (var e in element.XPathSelectElements("./struct/member"))
|
||||
{
|
||||
var name = e.ElementAsString("name");
|
||||
|
||||
if (name == "path")
|
||||
{
|
||||
Path = e.Element("value").GetStringValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Path { get; set; }
|
||||
}
|
||||
|
||||
[XmlRpcMember("completedLength")]
|
||||
public string CompletedLength;
|
||||
public class Aria2Dict
|
||||
{
|
||||
public Aria2Dict(XElement element)
|
||||
{
|
||||
Dict = new Dictionary<string, string>();
|
||||
|
||||
[XmlRpcMember("path")]
|
||||
public string Path;
|
||||
foreach (var e in element.XPathSelectElements("./struct/member"))
|
||||
{
|
||||
Dict.Add(e.ElementAsString("name"), e.Element("value").GetStringValue());
|
||||
}
|
||||
}
|
||||
|
||||
[XmlRpcMember("selected")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string Selected;
|
||||
public Dictionary<string, string> Dict { get; set; }
|
||||
}
|
||||
|
||||
[XmlRpcMember("uris")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public Aria2Uri[] Uris;
|
||||
public class Aria2Bittorrent
|
||||
{
|
||||
public Aria2Bittorrent(XElement element)
|
||||
{
|
||||
foreach (var e in element.Descendants("member"))
|
||||
{
|
||||
if (e.ElementAsString("name") == "name")
|
||||
{
|
||||
Name = e.Element("value").GetStringValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public string Name;
|
||||
}
|
||||
|
||||
public class Aria2Status
|
||||
{
|
||||
[XmlRpcMember("bittorrent")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public XmlRpcStruct Bittorrent;
|
||||
|
||||
[XmlRpcMember("bitfield")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string Bitfield;
|
||||
|
||||
[XmlRpcMember("infoHash")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string InfoHash;
|
||||
|
||||
[XmlRpcMember("completedLength")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string CompletedLength;
|
||||
|
||||
[XmlRpcMember("connections")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string Connections;
|
||||
|
||||
[XmlRpcMember("dir")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string Dir;
|
||||
|
||||
[XmlRpcMember("downloadSpeed")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string DownloadSpeed;
|
||||
|
||||
[XmlRpcMember("files")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public Aria2File[] Files;
|
||||
|
||||
[XmlRpcMember("gid")]
|
||||
public string Gid;
|
||||
|
||||
[XmlRpcMember("numPieces")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string NumPieces;
|
||||
|
||||
[XmlRpcMember("pieceLength")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string PieceLength;
|
||||
|
||||
[XmlRpcMember("status")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string Status;
|
||||
|
||||
[XmlRpcMember("totalLength")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string TotalLength;
|
||||
|
||||
[XmlRpcMember("uploadLength")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string UploadLength;
|
||||
|
||||
[XmlRpcMember("uploadSpeed")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string UploadSpeed;
|
||||
|
||||
[XmlRpcMember("errorMessage")]
|
||||
[XmlRpcMissingMapping(MappingAction.Ignore)]
|
||||
public string ErrorMessage;
|
||||
public Aria2Status(XElement element)
|
||||
{
|
||||
foreach (var e in element.XPathSelectElements("./struct/member"))
|
||||
{
|
||||
var name = e.ElementAsString("name");
|
||||
|
||||
if (name == "bittorrent")
|
||||
{
|
||||
Bittorrent = new Aria2Bittorrent(e.Element("value"));
|
||||
}
|
||||
else if (name == "infoHash")
|
||||
{
|
||||
InfoHash = e.Element("value").GetStringValue();
|
||||
}
|
||||
else if (name == "completedLength")
|
||||
{
|
||||
CompletedLength = e.Element("value").GetStringValue();
|
||||
}
|
||||
else if (name == "downloadSpeed")
|
||||
{
|
||||
DownloadSpeed = e.Element("value").GetStringValue();
|
||||
}
|
||||
else if (name == "files")
|
||||
{
|
||||
Files = e.XPathSelectElement("./value/array/data")
|
||||
.Elements()
|
||||
.Select(x => new Aria2File(x))
|
||||
.ToArray();
|
||||
}
|
||||
else if (name == "gid")
|
||||
{
|
||||
Gid = e.Element("value").GetStringValue();
|
||||
}
|
||||
else if (name == "status")
|
||||
{
|
||||
Status = e.Element("value").GetStringValue();
|
||||
}
|
||||
else if (name == "totalLength")
|
||||
{
|
||||
TotalLength = e.Element("value").GetStringValue();
|
||||
}
|
||||
else if (name == "uploadLength")
|
||||
{
|
||||
UploadLength = e.Element("value").GetStringValue();
|
||||
}
|
||||
else if (name == "errorMessage")
|
||||
{
|
||||
ErrorMessage = e.Element("value").GetStringValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public Aria2Bittorrent Bittorrent { get; set; }
|
||||
public string InfoHash { get; set; }
|
||||
public string CompletedLength { get; set; }
|
||||
public string DownloadSpeed { get; set; }
|
||||
public Aria2File[] Files { get; set; }
|
||||
public string Gid { get; set; }
|
||||
public string Status { get; set; }
|
||||
public string TotalLength { get; set; }
|
||||
public string UploadLength { get; set; }
|
||||
public string ErrorMessage { get; set; }
|
||||
}
|
||||
}
|
||||
|
@ -0,0 +1,28 @@
|
||||
using System.Xml.Linq;
|
||||
using System.Xml.XPath;
|
||||
using NzbDrone.Core.Download.Extensions;
|
||||
|
||||
namespace NzbDrone.Core.Download.Clients.RTorrent
|
||||
{
|
||||
public class RTorrentFault
|
||||
{
|
||||
public RTorrentFault(XElement element)
|
||||
{
|
||||
foreach (var e in element.XPathSelectElements("./value/struct/member"))
|
||||
{
|
||||
var name = e.ElementAsString("name");
|
||||
if (name == "faultCode")
|
||||
{
|
||||
FaultCode = e.Element("value").GetIntValue();
|
||||
}
|
||||
else if (name == "faultString")
|
||||
{
|
||||
FaultString = e.Element("value").GetStringValue();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public int FaultCode { get; set; }
|
||||
public string FaultString { get; set; }
|
||||
}
|
||||
}
|
@ -0,0 +1,54 @@
|
||||
using System.Xml.Linq;
|
||||
using System.Xml.XPath;
|
||||
|
||||
namespace NzbDrone.Core.Download.Extensions
|
||||
{
|
||||
internal static class XmlExtensions
|
||||
{
|
||||
public static string GetStringValue(this XElement element)
|
||||
{
|
||||
return element.ElementAsString("string");
|
||||
}
|
||||
|
||||
public static long GetLongValue(this XElement element)
|
||||
{
|
||||
return element.ElementAsLong("i8");
|
||||
}
|
||||
|
||||
public static int GetIntValue(this XElement element)
|
||||
{
|
||||
return element.ElementAsInt("i4");
|
||||
}
|
||||
|
||||
public static string ElementAsString(this XElement element, XName name, bool trim = false)
|
||||
{
|
||||
var el = element.Element(name);
|
||||
|
||||
return string.IsNullOrWhiteSpace(el?.Value)
|
||||
? null
|
||||
: (trim ? el.Value.Trim() : el.Value);
|
||||
}
|
||||
|
||||
public static long ElementAsLong(this XElement element, XName name)
|
||||
{
|
||||
var el = element.Element(name);
|
||||
return long.TryParse(el?.Value, out long value) ? value : default;
|
||||
}
|
||||
|
||||
public static int ElementAsInt(this XElement element, XName name)
|
||||
{
|
||||
var el = element.Element(name);
|
||||
return int.TryParse(el?.Value, out int value) ? value : default(int);
|
||||
}
|
||||
|
||||
public static int GetIntResponse(this XDocument document)
|
||||
{
|
||||
return document.XPathSelectElement("./methodResponse/params/param/value").GetIntValue();
|
||||
}
|
||||
|
||||
public static string GetStringResponse(this XDocument document)
|
||||
{
|
||||
return document.XPathSelectElement("./methodResponse/params/param/value").GetStringValue();
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,110 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Specialized;
|
||||
using System.Linq;
|
||||
using System.Net.Http;
|
||||
using System.Text;
|
||||
using System.Web;
|
||||
using NzbDrone.Common.Extensions;
|
||||
using NzbDrone.Common.Http;
|
||||
using NzbDrone.Common.OAuth;
|
||||
|
||||
namespace NzbDrone.Core.Notifications.Twitter
|
||||
{
|
||||
public interface ITwitterProxy
|
||||
{
|
||||
NameValueCollection GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier);
|
||||
string GetOAuthRedirect(string consumerKey, string consumerSecret, string callbackUrl);
|
||||
void UpdateStatus(string message, TwitterSettings settings);
|
||||
void DirectMessage(string message, TwitterSettings settings);
|
||||
}
|
||||
|
||||
public class TwitterProxy : ITwitterProxy
|
||||
{
|
||||
private readonly IHttpClient _httpClient;
|
||||
|
||||
public TwitterProxy(IHttpClient httpClient)
|
||||
{
|
||||
_httpClient = httpClient;
|
||||
}
|
||||
|
||||
public string GetOAuthRedirect(string consumerKey, string consumerSecret, string callbackUrl)
|
||||
{
|
||||
// Creating a new instance with a helper method
|
||||
var oAuthRequest = OAuthRequest.ForRequestToken(consumerKey, consumerSecret, callbackUrl);
|
||||
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/request_token";
|
||||
var qscoll = HttpUtility.ParseQueryString(ExecuteRequest(GetRequest(oAuthRequest, new Dictionary<string, string>())).Content);
|
||||
|
||||
return string.Format("https://api.twitter.com/oauth/authorize?oauth_token={0}", qscoll["oauth_token"]);
|
||||
}
|
||||
|
||||
public NameValueCollection GetOAuthToken(string consumerKey, string consumerSecret, string oauthToken, string oauthVerifier)
|
||||
{
|
||||
// Creating a new instance with a helper method
|
||||
var oAuthRequest = OAuthRequest.ForAccessToken(consumerKey, consumerSecret, oauthToken, "", oauthVerifier);
|
||||
oAuthRequest.RequestUrl = "https://api.twitter.com/oauth/access_token";
|
||||
|
||||
return HttpUtility.ParseQueryString(ExecuteRequest(GetRequest(oAuthRequest, new Dictionary<string, string>())).Content);
|
||||
}
|
||||
|
||||
public void UpdateStatus(string message, TwitterSettings settings)
|
||||
{
|
||||
var oAuthRequest = OAuthRequest.ForProtectedResource("POST", settings.ConsumerKey, settings.ConsumerSecret, settings.AccessToken, settings.AccessTokenSecret);
|
||||
|
||||
oAuthRequest.RequestUrl = "https://api.twitter.com/1.1/statuses/update.json";
|
||||
|
||||
var customParams = new Dictionary<string, string>
|
||||
{
|
||||
{ "status", message.EncodeRFC3986() }
|
||||
};
|
||||
|
||||
var request = GetRequest(oAuthRequest, customParams);
|
||||
|
||||
request.Headers.ContentType = "application/x-www-form-urlencoded";
|
||||
request.SetContent(Encoding.ASCII.GetBytes(GetCustomParametersString(customParams)));
|
||||
|
||||
ExecuteRequest(request);
|
||||
}
|
||||
|
||||
public void DirectMessage(string message, TwitterSettings settings)
|
||||
{
|
||||
var oAuthRequest = OAuthRequest.ForProtectedResource("POST", settings.ConsumerKey, settings.ConsumerSecret, settings.AccessToken, settings.AccessTokenSecret);
|
||||
|
||||
oAuthRequest.RequestUrl = "https://api.twitter.com/1.1/direct_messages/new.json";
|
||||
|
||||
var customParams = new Dictionary<string, string>
|
||||
{
|
||||
{ "text", message.EncodeRFC3986() },
|
||||
{ "screenname", settings.Mention.EncodeRFC3986() }
|
||||
};
|
||||
|
||||
var request = GetRequest(oAuthRequest, customParams);
|
||||
|
||||
request.Headers.ContentType = "application/x-www-form-urlencoded";
|
||||
request.SetContent(Encoding.ASCII.GetBytes(GetCustomParametersString(customParams)));
|
||||
|
||||
ExecuteRequest(request);
|
||||
}
|
||||
|
||||
private string GetCustomParametersString(Dictionary<string, string> customParams)
|
||||
{
|
||||
return customParams.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
|
||||
}
|
||||
|
||||
private HttpRequest GetRequest(OAuthRequest oAuthRequest, Dictionary<string, string> customParams)
|
||||
{
|
||||
var auth = oAuthRequest.GetAuthorizationHeader(customParams);
|
||||
var request = new HttpRequest(oAuthRequest.RequestUrl);
|
||||
|
||||
request.Headers.Add("Authorization", auth);
|
||||
|
||||
request.Method = oAuthRequest.Method == "POST" ? HttpMethod.Post : HttpMethod.Get;
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
private HttpResponse ExecuteRequest(HttpRequest request)
|
||||
{
|
||||
return _httpClient.Execute(request);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,235 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Security.Cryptography;
|
||||
using System.Text;
|
||||
using System.Text.RegularExpressions;
|
||||
|
||||
namespace TinyTwitter
|
||||
{
|
||||
public class OAuthInfo
|
||||
{
|
||||
public string ConsumerKey { get; set; }
|
||||
public string ConsumerSecret { get; set; }
|
||||
public string AccessToken { get; set; }
|
||||
public string AccessSecret { get; set; }
|
||||
}
|
||||
|
||||
public class Tweet
|
||||
{
|
||||
public long Id { get; set; }
|
||||
public DateTime CreatedAt { get; set; }
|
||||
public string UserName { get; set; }
|
||||
public string ScreenName { get; set; }
|
||||
public string Text { get; set; }
|
||||
}
|
||||
|
||||
public class TinyTwitter
|
||||
{
|
||||
private readonly OAuthInfo _oauth;
|
||||
|
||||
public TinyTwitter(OAuthInfo oauth)
|
||||
{
|
||||
_oauth = oauth;
|
||||
}
|
||||
|
||||
public void UpdateStatus(string message)
|
||||
{
|
||||
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/statuses/update.json")
|
||||
.AddParameter("status", message)
|
||||
.Execute();
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
* As of June 26th 2015 Direct Messaging is not part of TinyTwitter.
|
||||
* I have added it to Sonarr's copy to make our implementation easier
|
||||
* and added this banner so it's not blindly updated.
|
||||
*
|
||||
**/
|
||||
public void DirectMessage(string message, string screenName)
|
||||
{
|
||||
new RequestBuilder(_oauth, "POST", "https://api.twitter.com/1.1/direct_messages/new.json")
|
||||
.AddParameter("text", message)
|
||||
.AddParameter("screen_name", screenName)
|
||||
.Execute();
|
||||
}
|
||||
|
||||
public class RequestBuilder
|
||||
{
|
||||
private const string VERSION = "1.0";
|
||||
private const string SIGNATURE_METHOD = "HMAC-SHA1";
|
||||
|
||||
private readonly OAuthInfo _oauth;
|
||||
private readonly string _method;
|
||||
private readonly IDictionary<string, string> _customParameters;
|
||||
private readonly string _url;
|
||||
|
||||
public RequestBuilder(OAuthInfo oauth, string method, string url)
|
||||
{
|
||||
_oauth = oauth;
|
||||
_method = method;
|
||||
_url = url;
|
||||
_customParameters = new Dictionary<string, string>();
|
||||
}
|
||||
|
||||
public RequestBuilder AddParameter(string name, string value)
|
||||
{
|
||||
_customParameters.Add(name, value.EncodeRFC3986());
|
||||
return this;
|
||||
}
|
||||
|
||||
public string Execute()
|
||||
{
|
||||
var timespan = GetTimestamp();
|
||||
var nonce = CreateNonce();
|
||||
|
||||
var parameters = new Dictionary<string, string>(_customParameters);
|
||||
AddOAuthParameters(parameters, timespan, nonce);
|
||||
|
||||
var signature = GenerateSignature(parameters);
|
||||
var headerValue = GenerateAuthorizationHeaderValue(parameters, signature);
|
||||
|
||||
var request = (HttpWebRequest)WebRequest.Create(GetRequestUrl());
|
||||
request.Method = _method;
|
||||
request.ContentType = "application/x-www-form-urlencoded";
|
||||
|
||||
request.Headers.Add("Authorization", headerValue);
|
||||
|
||||
WriteRequestBody(request);
|
||||
|
||||
// It looks like a bug in HttpWebRequest. It throws random TimeoutExceptions
|
||||
// after some requests. Abort the request seems to work. More info:
|
||||
// http://stackoverflow.com/questions/2252762/getrequeststream-throws-timeout-exception-randomly
|
||||
var response = request.GetResponse();
|
||||
|
||||
string content;
|
||||
|
||||
using (var stream = response.GetResponseStream())
|
||||
{
|
||||
using (var reader = new StreamReader(stream))
|
||||
{
|
||||
content = reader.ReadToEnd();
|
||||
}
|
||||
}
|
||||
|
||||
request.Abort();
|
||||
|
||||
return content;
|
||||
}
|
||||
|
||||
private void WriteRequestBody(HttpWebRequest request)
|
||||
{
|
||||
if (_method == "GET")
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
var requestBody = Encoding.ASCII.GetBytes(GetCustomParametersString());
|
||||
using (var stream = request.GetRequestStream())
|
||||
{
|
||||
stream.Write(requestBody, 0, requestBody.Length);
|
||||
}
|
||||
}
|
||||
|
||||
private string GetRequestUrl()
|
||||
{
|
||||
if (_method != "GET" || _customParameters.Count == 0)
|
||||
{
|
||||
return _url;
|
||||
}
|
||||
|
||||
return string.Format("{0}?{1}", _url, GetCustomParametersString());
|
||||
}
|
||||
|
||||
private string GetCustomParametersString()
|
||||
{
|
||||
return _customParameters.Select(x => string.Format("{0}={1}", x.Key, x.Value)).Join("&");
|
||||
}
|
||||
|
||||
private string GenerateAuthorizationHeaderValue(IEnumerable<KeyValuePair<string, string>> parameters, string signature)
|
||||
{
|
||||
return new StringBuilder("OAuth ")
|
||||
.Append(parameters.Concat(new KeyValuePair<string, string>("oauth_signature", signature))
|
||||
.Where(x => x.Key.StartsWith("oauth_"))
|
||||
.Select(x => string.Format("{0}=\"{1}\"", x.Key, x.Value.EncodeRFC3986()))
|
||||
.Join(","))
|
||||
.ToString();
|
||||
}
|
||||
|
||||
private string GenerateSignature(IEnumerable<KeyValuePair<string, string>> parameters)
|
||||
{
|
||||
var dataToSign = new StringBuilder()
|
||||
.Append(_method).Append('&')
|
||||
.Append(_url.EncodeRFC3986()).Append('&')
|
||||
.Append(parameters
|
||||
.OrderBy(x => x.Key)
|
||||
.Select(x => string.Format("{0}={1}", x.Key, x.Value))
|
||||
.Join("&")
|
||||
.EncodeRFC3986());
|
||||
|
||||
var signatureKey = string.Format("{0}&{1}", _oauth.ConsumerSecret.EncodeRFC3986(), _oauth.AccessSecret.EncodeRFC3986());
|
||||
var sha1 = new HMACSHA1(Encoding.ASCII.GetBytes(signatureKey));
|
||||
|
||||
var signatureBytes = sha1.ComputeHash(Encoding.ASCII.GetBytes(dataToSign.ToString()));
|
||||
return Convert.ToBase64String(signatureBytes);
|
||||
}
|
||||
|
||||
private void AddOAuthParameters(IDictionary<string, string> parameters, string timestamp, string nonce)
|
||||
{
|
||||
parameters.Add("oauth_version", VERSION);
|
||||
parameters.Add("oauth_consumer_key", _oauth.ConsumerKey);
|
||||
parameters.Add("oauth_nonce", nonce);
|
||||
parameters.Add("oauth_signature_method", SIGNATURE_METHOD);
|
||||
parameters.Add("oauth_timestamp", timestamp);
|
||||
parameters.Add("oauth_token", _oauth.AccessToken);
|
||||
}
|
||||
|
||||
private static string GetTimestamp()
|
||||
{
|
||||
return ((int)(DateTime.UtcNow - new DateTime(1970, 1, 1)).TotalSeconds).ToString();
|
||||
}
|
||||
|
||||
private static string CreateNonce()
|
||||
{
|
||||
return new Random().Next(0x0000000, 0x7fffffff).ToString("X8");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static class TinyTwitterHelperExtensions
|
||||
{
|
||||
public static string Join<T>(this IEnumerable<T> items, string separator)
|
||||
{
|
||||
return string.Join(separator, items.ToArray());
|
||||
}
|
||||
|
||||
public static IEnumerable<T> Concat<T>(this IEnumerable<T> items, T value)
|
||||
{
|
||||
return items.Concat(new[] { value });
|
||||
}
|
||||
|
||||
public static string EncodeRFC3986(this string value)
|
||||
{
|
||||
// From Twitterizer http://www.twitterizer.net/
|
||||
if (string.IsNullOrEmpty(value))
|
||||
{
|
||||
return string.Empty;
|
||||
}
|
||||
|
||||
var encoded = Uri.EscapeDataString(value);
|
||||
|
||||
return Regex
|
||||
.Replace(encoded, "(%[0-9a-f][0-9a-f])", c => c.Value.ToUpper())
|
||||
.Replace("(", "%28")
|
||||
.Replace(")", "%29")
|
||||
.Replace("$", "%24")
|
||||
.Replace("!", "%21")
|
||||
.Replace("*", "%2A")
|
||||
.Replace("'", "%27")
|
||||
.Replace("%7E", "~");
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in new issue