mirror of
https://github.com/zoriya/octokit.net.git
synced 2026-06-03 11:05:56 +00:00
Move the Builder into it's own file
This commit is contained in:
@@ -41,9 +41,11 @@
|
||||
<Compile Include="Helpers\Ensure.cs" />
|
||||
<Compile Include="GitHubClient.cs" />
|
||||
<Compile Include="AuthenticationType.cs" />
|
||||
<Compile Include="Http\Builder.cs" />
|
||||
<Compile Include="Http\Connection.cs">
|
||||
<SubType>Code</SubType>
|
||||
</Compile>
|
||||
<Compile Include="Http\IBuilder.cs" />
|
||||
<Compile Include="Http\RequestHandler.cs" />
|
||||
<Compile Include="Http\ResponseHandler.cs" />
|
||||
<Compile Include="IApiObjectMap.cs" />
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using Burr.Helpers;
|
||||
|
||||
namespace Burr.Http
|
||||
{
|
||||
/// <summary>
|
||||
/// Standard Implementation of a <see cref="IBuilder"/>.
|
||||
/// A <see cref="Builder"/>'s job is to build up the middleware for a connection.
|
||||
/// </summary>
|
||||
public class Builder : IBuilder
|
||||
{
|
||||
readonly List<Func<IApplication, IApplication>> handlers = new List<Func<IApplication, IApplication>>();
|
||||
bool frozen;
|
||||
|
||||
public List<Func<IApplication, IApplication>> Handlers
|
||||
{
|
||||
get { return handlers; }
|
||||
}
|
||||
|
||||
public void Use(Func<IApplication, IApplication> handler)
|
||||
{
|
||||
Ensure.ArgumentNotNull(handler, "handler");
|
||||
if (frozen) throw new NotSupportedException("The middleware stack has already been built. You cannot modify the handlers now");
|
||||
|
||||
handlers.Add(handler);
|
||||
}
|
||||
|
||||
public IApplication Run(IApplication adapter = null)
|
||||
{
|
||||
if (frozen) throw new NotSupportedException("The middleware stack has already been built. You can only call run once.");
|
||||
frozen = true;
|
||||
|
||||
// The builder sets up a chain of apps to be called just like Rack
|
||||
// The inner most app is always an adapter which must produce the actual response
|
||||
// The call tree for a basic middleware stack will look like this:
|
||||
//
|
||||
// request( response ( adapter(env) ) )
|
||||
|
||||
adapter = adapter ?? new HttpClientAdapter();
|
||||
|
||||
return Enumerable.Reverse(handlers).Aggregate(adapter, (app, handler) => handler(app));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,55 +7,6 @@ using Burr.Helpers;
|
||||
|
||||
namespace Burr.Http
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="IBuilder"/>'s job is to build up the "application" for a connection.
|
||||
/// </summary>
|
||||
public interface IBuilder
|
||||
{
|
||||
List<Func<IApplication, IApplication>> Handlers { get; }
|
||||
IApplication Run(IApplication adapter);
|
||||
void Use(Func<IApplication, IApplication> handler);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Standard Implementation of a <see cref="IBuilder"/>.
|
||||
/// A <see cref="Builder"/>'s job is to build up the middleware for a connection.
|
||||
/// </summary>
|
||||
public class Builder : IBuilder
|
||||
{
|
||||
readonly List<Func<IApplication, IApplication>> handlers = new List<Func<IApplication, IApplication>>();
|
||||
bool frozen;
|
||||
|
||||
public List<Func<IApplication, IApplication>> Handlers
|
||||
{
|
||||
get { return handlers; }
|
||||
}
|
||||
|
||||
public void Use(Func<IApplication, IApplication> handler)
|
||||
{
|
||||
Ensure.ArgumentNotNull(handler, "handler");
|
||||
if (frozen) throw new NotSupportedException("The middleware stack has already been built. You cannot modify the handlers now");
|
||||
|
||||
handlers.Add(handler);
|
||||
}
|
||||
|
||||
public IApplication Run(IApplication adapter = null)
|
||||
{
|
||||
if (frozen) throw new NotSupportedException("The middleware stack has already been built. You can only call run once.");
|
||||
frozen = true;
|
||||
|
||||
// The builder sets up a chain of apps to be called just like Rack
|
||||
// The inner most app is always an adapter which must produce the actual response
|
||||
// The call tree for a basic middleware stack will look like this:
|
||||
//
|
||||
// request( response ( adapter(env) ) )
|
||||
|
||||
adapter = adapter ?? new HttpClientAdapter();
|
||||
|
||||
return Enumerable.Reverse(handlers).Aggregate(adapter, (app, handler) => handler(app));
|
||||
}
|
||||
}
|
||||
|
||||
public interface IResponse<T>
|
||||
{
|
||||
string Body { get; set; }
|
||||
|
||||
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
|
||||
namespace Burr.Http
|
||||
{
|
||||
/// <summary>
|
||||
/// A <see cref="IBuilder"/>'s job is to build up the "application" for a connection.
|
||||
/// </summary>
|
||||
public interface IBuilder
|
||||
{
|
||||
List<Func<IApplication, IApplication>> Handlers { get; }
|
||||
IApplication Run(IApplication adapter);
|
||||
void Use(Func<IApplication, IApplication> handler);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user