Skip to content

Commit

Permalink
[OneBot] Support Http and HttpPost
Browse files Browse the repository at this point in the history
  • Loading branch information
Linwenxuan04 committed Feb 23, 2024
1 parent 9549c6d commit 0d423cf
Show file tree
Hide file tree
Showing 17 changed files with 102 additions and 115 deletions.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
namespace Lagrange.OneBot.Core.Network.Options
{
public class ForwardWSServiceOptions : WSServiceOptions;
}
namespace Lagrange.OneBot.Core.Network.Options;

public class ForwardWSServiceOptions : WSServiceOptions;
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace Lagrange.OneBot.Core.Network.Options;

public sealed class ReverseHttpServiceOptions : HttpServiceOptions
public sealed class HttpPostServiceOptions : HttpServiceOptions
{
public string Suffix { get; set; } = "";

Expand Down
15 changes: 7 additions & 8 deletions Lagrange.OneBot/Core/Network/Options/HttpServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Lagrange.OneBot.Core.Network.Options
namespace Lagrange.OneBot.Core.Network.Options;

public class HttpServiceOptions
{
public abstract class HttpServiceOptions
{
public string Host { get; set; } = "";
public string Host { get; set; } = "";

public uint Port { get; set; }
public uint Port { get; set; }

public string? AccessToken { get; set; }
}
}
public string? AccessToken { get; set; }
}
13 changes: 6 additions & 7 deletions Lagrange.OneBot/Core/Network/Options/ReverseWSServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
namespace Lagrange.OneBot.Core.Network.Options
namespace Lagrange.OneBot.Core.Network.Options;

public sealed class ReverseWSServiceOptions : WSServiceOptions
{
public sealed class ReverseWSServiceOptions : WSServiceOptions
{
public string Suffix { get; set; } = "";
public string Suffix { get; set; } = "";

public uint ReconnectInterval { get; set; } = 5000;
}
}
public uint ReconnectInterval { get; set; } = 5000;
}
17 changes: 8 additions & 9 deletions Lagrange.OneBot/Core/Network/Options/WSServiceOptions.cs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
namespace Lagrange.OneBot.Core.Network.Options
namespace Lagrange.OneBot.Core.Network.Options;

public abstract class WSServiceOptions
{
public abstract class WSServiceOptions
{
public string Host { get; set; } = "";
public string Host { get; set; } = "";

public uint Port { get; set; }
public uint Port { get; set; }

public uint HeartBeatInterval { get; set; } = 5000; // by default 5000
public uint HeartBeatInterval { get; set; } = 5000; // by default 5000

public string? AccessToken { get; set; }
}
}
public string? AccessToken { get; set; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public class DefaultLagrangeWebServiceFactory(IServiceProvider services) : Lagra
{
"ReverseWebSocket" => Create<ReverseWSService>(config),
"ForwardWebSocket" => Create<ForwardWSService>(config),
"ReverseHttp" => Create<ReverseHttpService>(config),
"ForwardHttp" => Create<ForwardHttpService>(config),
"HttpPost" => Create<HttpPostService>(config),
"Http" => Create<HttpService>(config),
_ => null
};
}
Expand All @@ -27,11 +27,11 @@ public class DefaultLagrangeWebServiceFactory(IServiceProvider services) : Lagra
var fws = config.GetSection("ForwardWebSocket");
if (fws.Exists()) return Create<ForwardWSService>(fws);

var rh = config.GetSection("ReverseHttp");
if (rh.Exists()) return Create<ReverseHttpService>(rh);
var rh = config.GetSection("HttpPost");
if (rh.Exists()) return Create<HttpPostService>(rh);

var fh = config.GetSection("ForwardHttp");
if (fh.Exists()) return Create<ForwardHttpService>(fh);
var fh = config.GetSection("Http");
if (fh.Exists()) return Create<HttpService>(fh);

return null;
}
Expand Down
19 changes: 0 additions & 19 deletions Lagrange.OneBot/Core/Network/Service/ForwardHttpServiceFactory.cs

This file was deleted.

19 changes: 9 additions & 10 deletions Lagrange.OneBot/Core/Network/Service/ForwardWSServiceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Lagrange.OneBot.Core.Network.Service
namespace Lagrange.OneBot.Core.Network.Service;

public sealed class ForwardWSServiceFactory(IServiceProvider services) : LagrangeWebServiceFactory(services), ILagrangeWebServiceFactory<ForwardWSService>
{
public sealed class ForwardWSServiceFactory(IServiceProvider services) : LagrangeWebServiceFactory(services), ILagrangeWebServiceFactory<ForwardWSService>
public override ILagrangeWebService Create()
{
public override ILagrangeWebService Create()
{
var config = _config ?? throw new InvalidOperationException("Configuration must be provided");
var options = _services.GetRequiredService<IOptionsSnapshot<ForwardWSServiceOptions>>();
config.Bind(options.Value);
var config = _config ?? throw new InvalidOperationException("Configuration must be provided");
var options = _services.GetRequiredService<IOptionsSnapshot<ForwardWSServiceOptions>>();
config.Bind(options.Value);

return _services.GetRequiredService<ForwardWSService>();
}
return _services.GetRequiredService<ForwardWSService>();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@

namespace Lagrange.OneBot.Core.Network.Service;

public partial class ReverseHttpService(IOptionsSnapshot<ReverseHttpServiceOptions> options, ILogger<ReverseHttpService> logger, BotContext context)
public partial class HttpPostService(IOptionsSnapshot<HttpPostServiceOptions> options, ILogger<HttpPostService> logger, BotContext context)
: BackgroundService, ILagrangeWebService
{
private const string Tag = nameof(ReverseHttpService);
private const string Tag = nameof(HttpPostService);

public event EventHandler<MsgRecvEventArgs>? OnMessageReceived { add { } remove { } }

private readonly ReverseHttpServiceOptions _options = options.Value;
private readonly HttpPostServiceOptions _options = options.Value;

private readonly ILogger _logger = logger;

Expand Down
18 changes: 18 additions & 0 deletions Lagrange.OneBot/Core/Network/Service/HttpPostServiceFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Lagrange.OneBot.Core.Network.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Lagrange.OneBot.Core.Network.Service;

public sealed class HttpPostServiceFactory(IServiceProvider services) : LagrangeWebServiceFactory(services), ILagrangeWebServiceFactory<HttpPostService>
{
public override ILagrangeWebService Create()
{
var config = _config ?? throw new InvalidOperationException("Configuration must be provided");
var options = _services.GetRequiredService<IOptionsSnapshot<HttpPostServiceOptions>>();
config.Bind(options.Value);

return _services.GetRequiredService<HttpPostService>();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@

namespace Lagrange.OneBot.Core.Network.Service;

public sealed partial class ForwardHttpService : ILagrangeWebService
public sealed partial class HttpService : ILagrangeWebService
{
public event EventHandler<MsgRecvEventArgs>? OnMessageReceived;

private readonly ForwardHttpServiceOptions _options;
private readonly HttpServiceOptions _options;

private readonly ILogger _logger;

Expand All @@ -25,9 +25,7 @@ public sealed partial class ForwardHttpService : ILagrangeWebService

private readonly string _accessToken;

public ForwardHttpService(IOptionsSnapshot<ForwardHttpServiceOptions> options,
ILogger<ForwardHttpService> logger,
BotContext context)
public HttpService(IOptionsSnapshot<HttpServiceOptions> options, ILogger<HttpService> logger, BotContext context)
{
_options = options.Value;
_logger = logger;
Expand All @@ -53,9 +51,9 @@ public Task StartAsync(CancellationToken cancellationToken)
while (_listener.IsListening)
{
var context = _listener.GetContext();
Task.Run(() => HandleRequest(context.Request, context.Response));
Task.Run(() => HandleRequest(context.Request, context.Response), cancellationToken);
}
});
}, cancellationToken);

return Task.CompletedTask;
}
Expand Down
18 changes: 18 additions & 0 deletions Lagrange.OneBot/Core/Network/Service/HttpServiceFactory.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Lagrange.OneBot.Core.Network.Options;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Lagrange.OneBot.Core.Network.Service;

public sealed class HttpServiceFactory(IServiceProvider services) : LagrangeWebServiceFactory(services), ILagrangeWebServiceFactory<HttpService>
{
public override ILagrangeWebService Create()
{
var config = _config ?? throw new InvalidOperationException("Configuration must be provided");
var options = _services.GetRequiredService<IOptionsSnapshot<HttpServiceOptions>>();
config.Bind(options.Value);

return _services.GetRequiredService<HttpService>();
}
}
19 changes: 0 additions & 19 deletions Lagrange.OneBot/Core/Network/Service/ReverseHttpServiceFactory.cs

This file was deleted.

19 changes: 9 additions & 10 deletions Lagrange.OneBot/Core/Network/Service/ReverseWSServiceFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,16 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;

namespace Lagrange.OneBot.Core.Network.Service
namespace Lagrange.OneBot.Core.Network.Service;

public sealed class ReverseWSServiceFactory(IServiceProvider services) : LagrangeWebServiceFactory(services), ILagrangeWebServiceFactory<ReverseWSService>
{
public sealed class ReverseWSServiceFactory(IServiceProvider services) : LagrangeWebServiceFactory(services), ILagrangeWebServiceFactory<ReverseWSService>
public override ILagrangeWebService Create()
{
public override ILagrangeWebService Create()
{
var config = _config ?? throw new InvalidOperationException("Configuration must be provided");
var options = _services.GetRequiredService<IOptionsSnapshot<ReverseWSServiceOptions>>();
config.Bind(options.Value);
var config = _config ?? throw new InvalidOperationException("Configuration must be provided");
var options = _services.GetRequiredService<IOptionsSnapshot<ReverseWSServiceOptions>>();
config.Bind(options.Value);

return _services.GetRequiredService<ReverseWSService>();
}
return _services.GetRequiredService<ReverseWSService>();
}
}
}
8 changes: 4 additions & 4 deletions Lagrange.OneBot/LagrangeAppBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,10 @@ public LagrangeAppBuilder ConfigureOneBot()
Services.AddScoped<ForwardWSService>();
Services.AddScoped<ILagrangeWebServiceFactory<ReverseWSService>, ReverseWSServiceFactory>();
Services.AddScoped<ReverseWSService>();
Services.AddScoped<ILagrangeWebServiceFactory<ForwardHttpService>, ForwardHttpServiceFactory>();
Services.AddScoped<ForwardHttpService>();
Services.AddScoped<ILagrangeWebServiceFactory<ReverseHttpService>, ReverseHttpServiceFactory>();
Services.AddScoped<ReverseHttpService>();
Services.AddScoped<ILagrangeWebServiceFactory<HttpService>, HttpServiceFactory>();
Services.AddScoped<HttpService>();
Services.AddScoped<ILagrangeWebServiceFactory<HttpPostService>, HttpPostServiceFactory>();
Services.AddScoped<HttpPostService>();
Services.AddScoped<ILagrangeWebServiceFactory, DefaultLagrangeWebServiceFactory>();
Services.AddScoped(services =>
{
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -235,11 +235,11 @@ Please use Lagrange.Core responsibly and in accordance with the law.
<Summary>Communication</Summary>

| CommunicationType | Support |
| ------------------ | :-----: |
| [Http] | 🔴 |
| [Http-Post] | 🔴 |
| [ForwardWebSocket] | 🟢 |
| [ReverseWebSocket] | 🟢 |
|--------------------|:-------:|
| [Http] | 🟢 |
| [Http-Post] | 🟢 |
| [ForwardWebSocket] | 🟢 |
| [ReverseWebSocket] | 🟢 |

[Http]: https://github.com/botuniverse/onebot-11/blob/master/communication/http.md
[Http-Post]: https://github.com/botuniverse/onebot-11/blob/master/communication/http-post.md
Expand Down

0 comments on commit 0d423cf

Please sign in to comment.