removed flurl and made a request creator

This commit is contained in:
TomPallister
2016-10-07 12:54:50 +01:00
parent 74a7f5d270
commit 8688c1eb6f
11 changed files with 289 additions and 591 deletions

View File

@@ -1,17 +1,16 @@
namespace Ocelot.Library.Infrastructure.RequestBuilder
{
using System.IO;
using System.Net.Http;
using Microsoft.AspNetCore.Http;
using System.IO;
using Microsoft.AspNetCore.Http;
namespace Ocelot.Library.Infrastructure.RequestBuilder
{
public interface IRequestBuilder
{
HttpRequestMessage Build(string httpMethod,
Request Build(string httpMethod,
string downstreamUrl,
Stream content,
IHeaderDictionary headers,
IRequestCookieCollection cookies,
IQueryCollection queryString,
string queryString,
string contentType);
}
}

View File

@@ -0,0 +1,17 @@
using System.Net;
using System.Net.Http;
namespace Ocelot.Library.Infrastructure.RequestBuilder
{
public class Request
{
public Request(HttpRequestMessage httpRequestMessage, CookieContainer cookieContainer)
{
HttpRequestMessage = httpRequestMessage;
CookieContainer = cookieContainer;
}
public HttpRequestMessage HttpRequestMessage { get; private set; }
public CookieContainer CookieContainer { get; private set; }
}
}

View File

@@ -1,24 +1,27 @@
namespace Ocelot.Library.Infrastructure.RequestBuilder
{
using System;
using System.IO;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Http;
using System;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using Microsoft.AspNetCore.Http;
namespace Ocelot.Library.Infrastructure.RequestBuilder
{
public class RequestBuilder : IRequestBuilder
{
public HttpRequestMessage Build(string httpMethod, string downstreamUrl, Stream content, IHeaderDictionary headers,
IRequestCookieCollection cookies, IQueryCollection queryString, string contentType)
public Request Build(string httpMethod, string downstreamUrl, Stream content, IHeaderDictionary headers,
IRequestCookieCollection cookies, string queryString, string contentType)
{
var method = new HttpMethod(httpMethod);
var uri = new Uri(downstreamUrl + queryString);
var uri = new Uri(string.Format("{0}{1}", downstreamUrl, queryString));
var httpRequestMessage = new HttpRequestMessage(method, uri)
var httpRequestMessage = new HttpRequestMessage(method, uri);
if (content != null)
{
Content = new StreamContent(content),
};
httpRequestMessage.Content = new StreamContent(content);
}
if (!string.IsNullOrEmpty(contentType))
{
@@ -42,8 +45,19 @@
}
}
return httpRequestMessage;
var cookieContainer = new CookieContainer();
//todo get rid of if
if (cookies != null)
{
foreach (var cookie in cookies)
{
cookieContainer.Add(uri, new Cookie(cookie.Key, cookie.Value));
}
}
return new Request(httpRequestMessage, cookieContainer);
}
}
}