Changing BotBase behaviour to fluent api

- removing unecessary constructors from BotBase
- removing generics from BotBase
- removing generics from SessionBase
- adding StartFormFactory interface
- adding DefaultStartFormFactory
- adding multiple methods to BotBaseBuilder
This commit is contained in:
FlorianDahn
2021-10-17 17:25:17 +02:00
parent 7004ee5963
commit a22ede0f4f
9 changed files with 271 additions and 82 deletions
+105
View File
@@ -0,0 +1,105 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Telegram.Bot;
using TelegramBotBase.Base;
using TelegramBotBase.Builder.Interfaces;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Builder
{
public class BotBaseBuilder : IAPIKeySelectionStage, IStartFormSelectionPage, IBuildingStage, INetworkingSelectionStage
{
String apiKey = null;
IStartFormFactory factory = null;
MessageClient client = null;
public static IAPIKeySelectionStage Create()
{
return new BotBaseBuilder();
}
public IStartFormSelectionPage WithAPIKey(string apiKey)
{
this.apiKey = apiKey;
return this;
}
public INetworkingSelectionStage WithStartForm(Type startFormClass)
{
this.factory = new Factories.DefaultStartFormFactory(startFormClass);
return this;
}
public INetworkingSelectionStage WithStartForm<T>()
where T : FormBase, new()
{
this.factory = new Factories.DefaultStartFormFactory(typeof(T));
return this;
}
public INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory)
{
this.factory = factory;
return this;
}
public IBuildingStage WithProxy(string proxyAddress)
{
var url = new Uri(proxyAddress);
client = new MessageClient(apiKey, url);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage NoProxy()
{
client = new MessageClient(apiKey);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage WithBotClient(TelegramBotClient tgclient)
{
client = new MessageClient(apiKey, tgclient);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage WithHostAndPort(string proxyHost, int proxyPort)
{
client = new MessageClient(apiKey, proxyHost, proxyPort);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public IBuildingStage WithHttpClient(HttpClient tgclient)
{
client = new MessageClient(apiKey, tgclient);
client.TelegramClient.Timeout = new TimeSpan(0, 1, 0);
return this;
}
public BotBase Build()
{
var bb = new BotBase();
bb.APIKey = apiKey;
bb.StartFormFactory = factory;
bb.Client = client;
bb.Sessions.Client = bb.Client;
return bb;
}
}
}
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Builder.Interfaces
{
public interface IAPIKeySelectionStage
{
/// <summary>
/// Sets the API Key which will be used by the telegram bot client.
/// </summary>
/// <param name="apiKey"></param>
/// <returns></returns>
IStartFormSelectionPage WithAPIKey(String apiKey);
}
}
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;
namespace TelegramBotBase.Builder.Interfaces
{
public interface IBuildingStage
{
BotBase Build();
}
}
@@ -0,0 +1,49 @@
using System;
using System.Collections.Generic;
using System.Net.Http;
using System.Text;
using Telegram.Bot;
namespace TelegramBotBase.Builder.Interfaces
{
public interface INetworkingSelectionStage
{
/// <summary>
/// Chooses a proxy as network configuration.
/// </summary>
/// <param name="proxyAddress"></param>
/// <returns></returns>
IBuildingStage WithProxy(String proxyAddress);
/// <summary>
/// Do not choose a proxy as network configuration.
/// </summary>
/// <returns></returns>
IBuildingStage NoProxy();
/// <summary>
/// Chooses a custom instance of TelegramBotClient.
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
IBuildingStage WithBotClient(TelegramBotClient client);
/// <summary>
/// Sets the custom proxy host and port.
/// </summary>
/// <param name="proxyHost"></param>
/// <param name="Port"></param>
/// <returns></returns>
IBuildingStage WithHostAndPort(String proxyHost, int Port);
/// <summary>
/// Uses a custom http client.
/// </summary>
/// <param name="client"></param>
/// <returns></returns>
IBuildingStage WithHttpClient(HttpClient client);
}
}
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Text;
using TelegramBotBase.Form;
using TelegramBotBase.Interfaces;
namespace TelegramBotBase.Builder.Interfaces
{
public interface IStartFormSelectionPage
{
/// <summary>
/// Chooses a start form type which will be used for new sessions.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
INetworkingSelectionStage WithStartForm(Type startFormClass);
/// <summary>
/// Chooses a generic start form which will be used for new sessions.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
INetworkingSelectionStage WithStartForm<T>() where T : FormBase, new();
/// <summary>
/// Chooses a StartFormFactory which will be use for new sessions.
/// </summary>
/// <param name="factory"></param>
/// <returns></returns>
INetworkingSelectionStage WithStartFormFactory(IStartFormFactory factory);
}
}