Core: avoid some code repetition

This commit is contained in:
Andres G. Aragoneses 2020-01-25 16:16:14 +08:00
parent 7230b9b509
commit dc8c20df63

View file

@ -201,7 +201,6 @@ namespace TLSharp.Core
public async Task<TLUser> MakeAuthWithPasswordAsync(TLPassword password, string password_str) public async Task<TLUser> MakeAuthWithPasswordAsync(TLPassword password, string password_str)
{ {
byte[] password_Bytes = Encoding.UTF8.GetBytes(password_str); byte[] password_Bytes = Encoding.UTF8.GetBytes(password_str);
IEnumerable<byte> rv = password.CurrentSalt.Concat(password_Bytes).Concat(password.CurrentSalt); IEnumerable<byte> rv = password.CurrentSalt.Concat(password_Bytes).Concat(password.CurrentSalt);
@ -227,6 +226,7 @@ namespace TLSharp.Core
return ((TLUser)request.Response.User); return ((TLUser)request.Response.User);
} }
public async Task<T> SendRequestAsync<T>(TLMethod methodToExecute) public async Task<T> SendRequestAsync<T>(TLMethod methodToExecute)
{ {
await RequestWithDcMigration(methodToExecute); await RequestWithDcMigration(methodToExecute);
@ -236,72 +236,59 @@ namespace TLSharp.Core
return (T)result; return (T)result;
} }
public async Task<TLUser> UpdateUsernameAsync(string username) private async Task<T> SendAuthenticatedRequestAsync<T> (TLMethod methodToExecute)
{ {
if (!IsUserAuthorized()) if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!"); throw new InvalidOperationException("Authorize user first!");
return await SendRequestAsync<T>(methodToExecute);
}
public async Task<TLUser> UpdateUsernameAsync(string username)
{
var req = new TLRequestUpdateUsername { Username = username }; var req = new TLRequestUpdateUsername { Username = username };
return await SendRequestAsync<TLUser>(req); return await SendAuthenticatedRequestAsync<TLUser>(req);
} }
public async Task<bool> CheckUsernameAsync(string username) public async Task<bool> CheckUsernameAsync(string username)
{ {
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
var req = new TLRequestCheckUsername { Username = username }; var req = new TLRequestCheckUsername { Username = username };
return await SendRequestAsync<bool>(req); return await SendAuthenticatedRequestAsync<bool>(req);
} }
public async Task<TLImportedContacts> ImportContactsAsync(IReadOnlyList<TLInputPhoneContact> contacts) public async Task<TLImportedContacts> ImportContactsAsync(IReadOnlyList<TLInputPhoneContact> contacts)
{ {
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
var req = new TLRequestImportContacts { Contacts = new TLVector<TLInputPhoneContact>(contacts)}; var req = new TLRequestImportContacts { Contacts = new TLVector<TLInputPhoneContact>(contacts)};
return await SendRequestAsync<TLImportedContacts>(req); return await SendAuthenticatedRequestAsync<TLImportedContacts>(req);
} }
public async Task<bool> DeleteContactsAsync(IReadOnlyList<TLAbsInputUser> users) public async Task<bool> DeleteContactsAsync(IReadOnlyList<TLAbsInputUser> users)
{ {
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
var req = new TLRequestDeleteContacts {Id = new TLVector<TLAbsInputUser>(users)}; var req = new TLRequestDeleteContacts {Id = new TLVector<TLAbsInputUser>(users)};
return await SendRequestAsync<bool>(req); return await SendAuthenticatedRequestAsync<bool>(req);
} }
public async Task<TLLink> DeleteContactAsync(TLAbsInputUser user) public async Task<TLLink> DeleteContactAsync(TLAbsInputUser user)
{ {
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
var req = new TLRequestDeleteContact {Id = user}; var req = new TLRequestDeleteContact {Id = user};
return await SendRequestAsync<TLLink>(req); return await SendAuthenticatedRequestAsync<TLLink>(req);
} }
public async Task<TLContacts> GetContactsAsync() public async Task<TLContacts> GetContactsAsync()
{ {
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
var req = new TLRequestGetContacts() { Hash = "" }; var req = new TLRequestGetContacts() { Hash = "" };
return await SendRequestAsync<TLContacts>(req); return await SendAuthenticatedRequestAsync<TLContacts>(req);
} }
public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message) public async Task<TLAbsUpdates> SendMessageAsync(TLAbsInputPeer peer, string message)
{ {
if (!IsUserAuthorized()) return await SendAuthenticatedRequestAsync<TLAbsUpdates>(
throw new InvalidOperationException("Authorize user first!");
return await SendRequestAsync<TLAbsUpdates>(
new TLRequestSendMessage() new TLRequestSendMessage()
{ {
Peer = peer, Peer = peer,
@ -322,9 +309,6 @@ namespace TLSharp.Core
public async Task<TLAbsDialogs> GetUserDialogsAsync(int offsetDate = 0, int offsetId = 0, TLAbsInputPeer offsetPeer = null, int limit = 100) public async Task<TLAbsDialogs> GetUserDialogsAsync(int offsetDate = 0, int offsetId = 0, TLAbsInputPeer offsetPeer = null, int limit = 100)
{ {
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
if (offsetPeer == null) if (offsetPeer == null)
offsetPeer = new TLInputPeerSelf(); offsetPeer = new TLInputPeerSelf();
@ -335,7 +319,7 @@ namespace TLSharp.Core
OffsetPeer = offsetPeer, OffsetPeer = offsetPeer,
Limit = limit Limit = limit
}; };
return await SendRequestAsync<TLAbsDialogs>(req); return await SendAuthenticatedRequestAsync<TLAbsDialogs>(req);
} }
public async Task<TLAbsUpdates> SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption) public async Task<TLAbsUpdates> SendUploadedPhoto(TLAbsInputPeer peer, TLAbsInputFile file, string caption)
@ -388,9 +372,6 @@ namespace TLSharp.Core
public async Task<TLAbsMessages> GetHistoryAsync(TLAbsInputPeer peer, int offsetId = 0, int offsetDate = 0, int addOffset = 0, int limit = 100, int maxId = 0, int minId = 0) public async Task<TLAbsMessages> GetHistoryAsync(TLAbsInputPeer peer, int offsetId = 0, int offsetDate = 0, int addOffset = 0, int limit = 100, int maxId = 0, int minId = 0)
{ {
if (!IsUserAuthorized())
throw new InvalidOperationException("Authorize user first!");
var req = new TLRequestGetHistory() var req = new TLRequestGetHistory()
{ {
Peer = peer, Peer = peer,
@ -401,7 +382,7 @@ namespace TLSharp.Core
MaxId = maxId, MaxId = maxId,
MinId = minId MinId = minId
}; };
return await SendRequestAsync<TLAbsMessages>(req); return await SendAuthenticatedRequestAsync<TLAbsMessages>(req);
} }
/// <summary> /// <summary>