Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Proxyability #22

Merged
merged 2 commits into from
Sep 25, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion NetTelegramBotApi/NetTelegramBotApi.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=7.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<HintPath>..\..\..\packages\Newtonsoft.Json.7.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="System" />
Expand Down
28 changes: 26 additions & 2 deletions NetTelegramBotApi/TelegramBot.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using System.Net;
using System.Net.Http;
using System.Threading.Tasks;
using NetTelegramBotApi.Requests;
Expand All @@ -10,6 +11,8 @@ namespace NetTelegramBotApi
{
public class TelegramBot
{
private readonly string _proxyUrl;

public static readonly JsonSerializerSettings JsonSettings = new JsonSerializerSettings
{
ContractResolver = new Util.JsonLowerCaseUnderscoreContractResolver()
Expand All @@ -23,19 +26,26 @@ static TelegramBot()
JsonSettings.Converters.Add(new UnixDateTimeConverter());
}

public TelegramBot(string accessToken)
/// <summary>
/// Initialisiert eine neue Instanz des <see cref="TelegramBot"/>
/// </summary>
/// <param name="accessToken"></param>
/// <param name="proxyUrl">optional: URL like </param>
public TelegramBot(string accessToken, string proxyUrl = null)
{
_proxyUrl = proxyUrl;
if (string.IsNullOrWhiteSpace(accessToken))
{
throw new ArgumentNullException("accessToken");
}


this.baseAddress = new Uri("https://api.telegram.org/bot" + accessToken + "/");
}

public async Task<T> MakeRequestAsync<T>(RequestBase<T> request)
{
using (var client = new HttpClient())
using (var client = new HttpClient(getHttpDefaultHandler()))
{
client.BaseAddress = baseAddress;
using (var httpMessage = new HttpRequestMessage(HttpMethod.Get, request.MethodName))
Expand Down Expand Up @@ -64,6 +74,20 @@ public async Task<T> MakeRequestAsync<T>(RequestBase<T> request)
}
}

private HttpClientHandler getHttpDefaultHandler()
{
if (_proxyUrl != null)
{
return new HttpClientHandler
{
Proxy = new WebProxy(_proxyUrl, true),
UseProxy = true
};
}

return null;
}

/// <summary>
/// Use this method to deserialize <see cref="Update">Update</see> object, sent to <see cref="SetWebhook">your webhook</see> by Telegram server.
/// </summary>
Expand Down