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

Using a proxy for tonClient #121

Open
wants to merge 3 commits into
base: dev
Choose a base branch
from

Conversation

Fenixs8973
Copy link

I suggest adding the ability to use a proxy for tonClient

@Fenixs8973 Fenixs8973 changed the base branch from main to dev July 26, 2024 22:56
Changing the data type of an Ip address variable;
Adding a value constraint for the port variable
Adding a description to the fields of the Proxy class
Copy link
Contributor

@7645re 7645re left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Из любопытства спрашиваю, зачем там partial классы и зачем логика сравнения прокси?

Comment on lines +33 to +37
ProxyType.HTTP => "http://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
ProxyType.HTTPS => "https://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
ProxyType.Socks4 => "socks4://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
ProxyType.Socks5 => "socks5://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
_ => throw new ArgumentOutOfRangeException()
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Как будто бы можно объединить повторяющийся код "protocol://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port.
Мб UriBuilder

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Partial, в данном случае, выглядит удобнее всего. В Models.cs у нас указываются только поля и их типы в разных классах. Реализация работы методов, на мой взгляд, там не к месту.

На счет логики сравнения прокси. А как по-другому указывать тип прокси? Как-то из enum доставать? Я просто не знаю)

Comment on lines +27 to +47
if (httpApiParameters.Proxy != null)
{
WebProxy webProxy = new WebProxy
{
Address = new Uri( httpApiParameters.Proxy.ProxyType switch
{
ProxyType.HTTP => "http://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
ProxyType.HTTPS => "https://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
ProxyType.Socks4 => "socks4://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
ProxyType.Socks5 => "socks5://" + httpApiParameters.Proxy.Ip + ":" + httpApiParameters.Proxy.Port,
_ => throw new ArgumentOutOfRangeException()
}),
Credentials = new NetworkCredential(
userName: httpApiParameters.Proxy.UserName,
password: httpApiParameters.Proxy.Password
)
};
HttpClientHandler httpClientHandler = new HttpClientHandler
{
Proxy = webProxy
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Можно вынести в приватный метод по типу:

private HttpClient CreateHttpClient(HttpApiParameters httpApiParameters)

и там можно будет повысить читаемость кода (понизим вложенность и облегчим чуть конструктор)

if (httpApiParameters.Proxy == null)
{
    return new HttpClient();
}

var proxyAddress = BuildProxyAddress(httpApiParameters.Proxy);
var webProxy = new WebProxy {};

var httpClientHandler = new HttpClientHandler
{
    Proxy = webProxy
};

return new HttpClient(httpClientHandler);

+мб можно вынести и переиспользовать для других типов хттп клиентов

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Кстати да, факты, так было бы удобней

Copy link

@naxoootcnjncgjnnxfhjjv naxoootcnjncgjnnxfhjjv left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    • [ ]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants