Refactored HttpRequest and HttpRequestBuilder, moving most of the logic to the HttpRequestBuilder.

Added ContentSummary to be able to describe the ContentData in a human readable form. (Useful for JsonRpc and FormData).
This commit is contained in:
Taloth Saldono 2016-02-28 16:41:22 +01:00
parent 7818f0c59b
commit 2ffbbb0e71
41 changed files with 683 additions and 347 deletions

View file

@ -1,19 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using NzbDrone.Common.EnvironmentInfo;
using NzbDrone.Common.Extensions;
namespace NzbDrone.Common.Http
{
public class HttpRequest
{
private readonly Dictionary<string, string> _segments;
public HttpRequest(string url, HttpAccept httpAccept = null)
public HttpRequest(string uri, HttpAccept httpAccept = null)
{
UriBuilder = new UriBuilder(url);
UrlBuilder = new UriBuilder(uri);
Headers = new HttpHeader();
_segments = new Dictionary<string, string>();
AllowAutoRedirect = true;
Cookies = new Dictionary<string, string>();
@ -28,73 +28,41 @@ namespace NzbDrone.Common.Http
}
}
public UriBuilder UriBuilder { get; private set; }
public Uri Url
{
get
{
var uri = UriBuilder.Uri.ToString();
foreach (var segment in _segments)
{
uri = uri.Replace(segment.Key, segment.Value);
}
return new Uri(uri);
}
}
public UriBuilder UrlBuilder { get; private set; }
public Uri Url { get { return UrlBuilder.Uri; } }
public HttpMethod Method { get; set; }
public HttpHeader Headers { get; set; }
public string Body { get; set; }
public byte[] ContentData { get; set; }
public string ContentSummary { get; set; }
public NetworkCredential NetworkCredential { get; set; }
public bool SuppressHttpError { get; set; }
public bool AllowAutoRedirect { get; set; }
public Dictionary<string, string> Cookies { get; private set; }
public bool StoreResponseCookie { get; set; }
public TimeSpan RequestTimeout { get; set; }
public TimeSpan RateLimit { get; set; }
public override string ToString()
{
if (Body == null)
if (ContentSummary == null)
{
return string.Format("Req: [{0}] {1}", Method, Url);
}
return string.Format("Req: [{0}] {1} {2} {3}", Method, Url, Environment.NewLine, Body);
}
public void AddSegment(string segment, string value)
{
var key = "{" + segment + "}";
if (!UriBuilder.Uri.ToString().Contains(key))
else
{
throw new InvalidOperationException("Segment " + key +" is not defined in Uri");
return string.Format("Req: [{0}] {1}: {2}", Method, Url, ContentSummary);
}
_segments.Add(key, value);
}
public void AddQueryParam(string segment, string value)
public void SetContent(byte[] data)
{
UriBuilder.SetQueryParam(segment, value);
ContentData = data;
}
public void AddCookie(string key, string value)
public void SetContent(string data)
{
Cookies[key] = value;
}
public void AddCookie(string cookies)
{
foreach (var pair in cookies.Split(';'))
{
var split = pair.Split('=');
Cookies[split[0].Trim()] = split[1].Trim();
}
var encoding = HttpHeader.GetEncodingFromContentType(Headers.ContentType);
ContentData = encoding.GetBytes(data);
}
}
}