diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index 5fadabe..700546c 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -49,12 +49,8 @@ namespace TLSharp.Core _transport = new TcpTransport(Session.DataCenter.Address, Session.DataCenter.Port, _handler); } - public async Task ConnectAsync(bool reconnect = false) - { - await ConnectAsync(CancellationToken.None, reconnect).ConfigureAwait(false); - } - - public async Task ConnectAsync(CancellationToken token, bool reconnect = false) + + public async Task ConnectAsync(bool reconnect = false, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); @@ -105,7 +101,7 @@ namespace TLSharp.Core _transport = new TcpTransport(dc.IpAddress, dc.Port, _handler); Session.DataCenter = dataCenter; - await ConnectAsync(token, true).ConfigureAwait(false); + await ConnectAsync(true, token).ConfigureAwait(false); if (Session.TLUser != null) { @@ -149,12 +145,7 @@ namespace TLSharp.Core return Session.TLUser != null; } - public async Task IsPhoneRegisteredAsync(string phoneNumber) - { - return await IsPhoneRegisteredAsync(phoneNumber, CancellationToken.None).ConfigureAwait(false); - } - - public async Task IsPhoneRegisteredAsync(string phoneNumber, CancellationToken token) + public async Task IsPhoneRegisteredAsync(string phoneNumber, CancellationToken token = default(CancellationToken)) { if (String.IsNullOrWhiteSpace(phoneNumber)) throw new ArgumentNullException(nameof(phoneNumber)); @@ -166,12 +157,7 @@ namespace TLSharp.Core return authCheckPhoneRequest.Response.PhoneRegistered; } - public async Task SendCodeRequestAsync(string phoneNumber) - { - return await SendCodeRequestAsync(phoneNumber, CancellationToken.None).ConfigureAwait(false); - } - - public async Task SendCodeRequestAsync(string phoneNumber, CancellationToken token) + public async Task SendCodeRequestAsync(string phoneNumber, CancellationToken token = default(CancellationToken)) { if (String.IsNullOrWhiteSpace(phoneNumber)) throw new ArgumentNullException(nameof(phoneNumber)); @@ -183,12 +169,7 @@ namespace TLSharp.Core return request.Response.PhoneCodeHash; } - public async Task MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code) - { - return await MakeAuthAsync(phoneNumber, phoneCodeHash, code, CancellationToken.None).ConfigureAwait(false); - } - - public async Task MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code, CancellationToken token) + public async Task MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code, CancellationToken token = default(CancellationToken)) { if (String.IsNullOrWhiteSpace(phoneNumber)) throw new ArgumentNullException(nameof(phoneNumber)); @@ -208,12 +189,7 @@ namespace TLSharp.Core return ((TLUser)request.Response.User); } - public async Task GetPasswordSetting() - { - return await GetPasswordSetting(CancellationToken.None).ConfigureAwait(false); - } - - public async Task GetPasswordSetting(CancellationToken token) + public async Task GetPasswordSetting(CancellationToken token = default(CancellationToken)) { var request = new TLRequestGetPassword(); @@ -222,12 +198,7 @@ namespace TLSharp.Core return ((TLPassword)request.Response); } - public async Task MakeAuthWithPasswordAsync(TLPassword password, string password_str) - { - return await MakeAuthWithPasswordAsync(password, password_str, CancellationToken.None).ConfigureAwait(false); - } - - public async Task MakeAuthWithPasswordAsync(TLPassword password, string password_str, CancellationToken token) + public async Task MakeAuthWithPasswordAsync(TLPassword password, string password_str, CancellationToken token = default(CancellationToken)) { token.ThrowIfCancellationRequested(); @@ -246,12 +217,7 @@ namespace TLSharp.Core return ((TLUser)request.Response.User); } - public async Task SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName) - { - return await SignUpAsync(phoneNumber, phoneCodeHash, code, firstName, lastName, CancellationToken.None).ConfigureAwait(false); - } - - public async Task SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName, CancellationToken token) + public async Task SignUpAsync(string phoneNumber, string phoneCodeHash, string code, string firstName, string lastName, CancellationToken token = default(CancellationToken)) { var request = new TLRequestSignUp() { PhoneNumber = phoneNumber, PhoneCode = code, PhoneCodeHash = phoneCodeHash, FirstName = firstName, LastName = lastName }; @@ -262,12 +228,7 @@ namespace TLSharp.Core return ((TLUser)request.Response.User); } - public async Task SendRequestAsync(TLMethod methodToExecute) - { - return await SendRequestAsync(methodToExecute, CancellationToken.None).ConfigureAwait(false); - } - - public async Task SendRequestAsync(TLMethod methodToExecute, CancellationToken token) + public async Task SendRequestAsync(TLMethod methodToExecute, CancellationToken token = default(CancellationToken)) { await RequestWithDcMigration(methodToExecute, token).ConfigureAwait(false); @@ -276,12 +237,7 @@ namespace TLSharp.Core return (T)result; } - public async Task GetContactsAsync() - { - return await GetContactsAsync(CancellationToken.None).ConfigureAwait(false); - } - - public async Task GetContactsAsync(CancellationToken token) + public async Task GetContactsAsync(CancellationToken token = default(CancellationToken)) { if (!IsUserAuthorized()) throw new InvalidOperationException("Authorize user first!"); @@ -291,12 +247,7 @@ namespace TLSharp.Core return await SendRequestAsync(req, token).ConfigureAwait(false); } - public async Task SendMessageAsync(TLAbsInputPeer peer, string message) - { - return await SendMessageAsync(peer, message, CancellationToken.None).ConfigureAwait(false); - } - - public async Task SendMessageAsync(TLAbsInputPeer peer, string message, CancellationToken token) + public async Task SendMessageAsync(TLAbsInputPeer peer, string message, CancellationToken token = default(CancellationToken)) { if (!IsUserAuthorized()) throw new InvalidOperationException("Authorize user first!"); @@ -310,12 +261,7 @@ namespace TLSharp.Core }, token).ConfigureAwait(false); } - public async Task SendTypingAsync(TLAbsInputPeer peer) - { - return await SendTypingAsync(peer, CancellationToken.None).ConfigureAwait(false); - } - - public async Task SendTypingAsync(TLAbsInputPeer peer, CancellationToken token) + public async Task SendTypingAsync(TLAbsInputPeer peer, CancellationToken token = default(CancellationToken)) { var req = new TLRequestSetTyping() { @@ -325,12 +271,8 @@ namespace TLSharp.Core return await SendRequestAsync(req, token).ConfigureAwait(false); } - public async Task GetUserDialogsAsync(int offsetDate = 0, int offsetId = 0, TLAbsInputPeer offsetPeer = null, int limit = 100) - { - return await GetUserDialogsAsync(CancellationToken.None, offsetDate, offsetId, offsetPeer, limit).ConfigureAwait(false); - } - - public async Task GetUserDialogsAsync(CancellationToken token, int offsetDate = 0, int offsetId = 0, TLAbsInputPeer offsetPeer = null, int limit = 100) + public async Task GetUserDialogsAsync(int offsetDate = 0, int offsetId = 0, TLAbsInputPeer offsetPeer = null, int limit = 100, + CancellationToken token = default(CancellationToken)) { if (!IsUserAuthorized()) throw new InvalidOperationException("Authorize user first!"); @@ -348,12 +290,7 @@ namespace TLSharp.Core return await SendRequestAsync(req, token).ConfigureAwait(false); } - public async Task SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption) - { - return await SendUploadedPhoto(peer, file, caption, CancellationToken.None).ConfigureAwait(false); - } - - public async Task SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption, CancellationToken token) + public async Task SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption, CancellationToken token = default(CancellationToken)) { return await SendRequestAsync(new TLRequestSendMedia() { @@ -365,12 +302,8 @@ namespace TLSharp.Core }, token).ConfigureAwait(false); } - public async Task SendUploadedDocument(TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector attributes) - { - return await SendUploadedDocument(peer, file, caption, mimeType, attributes, CancellationToken.None).ConfigureAwait(false); - } - - public async Task SendUploadedDocument(TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector attributes, CancellationToken token) + public async Task SendUploadedDocument(TLAbsInputPeer peer, TLAbsInputFile file, string caption, string mimeType, TLVector attributes, + CancellationToken token = default(CancellationToken)) { return await SendRequestAsync(new TLRequestSendMedia() { @@ -388,15 +321,9 @@ namespace TLSharp.Core }, token).ConfigureAwait(false); } - public async Task GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0) + public async Task GetFile(TLAbsInputFileLocation location, int filePartSize, int offset = 0, CancellationToken token = default(CancellationToken)) { - return await GetFile(location, filePartSize, CancellationToken.None, offset).ConfigureAwait(false); - } - - public async Task GetFile(TLAbsInputFileLocation location, int filePartSize, CancellationToken token, int offset = 0) - { - TLFile result = null; - result = await SendRequestAsync(new TLRequestGetFile() + var result = await SendRequestAsync(new TLRequestGetFile() { Location = location, Limit = filePartSize, @@ -405,12 +332,13 @@ namespace TLSharp.Core return result; } - public async Task SendPingAsync(CancellationToken token) + public async Task SendPingAsync(CancellationToken token = default(CancellationToken)) { await _sender.SendPingAsync(token).ConfigureAwait(false); } - public async Task GetHistoryAsync(TLAbsInputPeer peer, CancellationToken token, int offsetId = 0, int offsetDate = 0, int addOffset = 0, int limit = 100, int maxId = 0, int minId = 0) + public async Task GetHistoryAsync(TLAbsInputPeer peer, int offsetId = 0, int offsetDate = 0, int addOffset = 0, int limit = 100, int maxId = 0, + int minId = 0, CancellationToken token = default(CancellationToken)) { if (!IsUserAuthorized()) throw new InvalidOperationException("Authorize user first!"); @@ -433,13 +361,9 @@ namespace TLSharp.Core /// /// User or chat name /// Max result count + /// /// - public async Task SearchUserAsync(string q, int limit = 10) - { - return await SearchUserAsync(q, CancellationToken.None, limit).ConfigureAwait(false); - } - - public async Task SearchUserAsync(string q, CancellationToken token, int limit = 10) + public async Task SearchUserAsync(string q, int limit = 10, CancellationToken token = default(CancellationToken)) { var r = new TeleSharp.TL.Contacts.TLRequestSearch { diff --git a/TLSharp.Core/Utils/UploadHelper.cs b/TLSharp.Core/Utils/UploadHelper.cs index dba0fac..a5b0fe6 100644 --- a/TLSharp.Core/Utils/UploadHelper.cs +++ b/TLSharp.Core/Utils/UploadHelper.cs @@ -29,12 +29,7 @@ namespace TLSharp.Core.Utils return md5_checksum; } - public static async Task UploadFile(this TelegramClient client, string name, StreamReader reader) - { - return await UploadFile(client, name, reader, CancellationToken.None).ConfigureAwait(false); - } - - public static async Task UploadFile(this TelegramClient client, string name, StreamReader reader, CancellationToken token) + public static async Task UploadFile(this TelegramClient client, string name, StreamReader reader, CancellationToken token = default(CancellationToken)) { const long tenMb = 10 * 1024 * 1024; return await UploadFile(name, reader, client, reader.BaseStream.Length >= tenMb, token).ConfigureAwait(false);