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

fix receiving bug with try-catch, and add registeration api type classes. #27

Closed
wants to merge 3 commits into from
Closed
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
14 changes: 0 additions & 14 deletions TDLib.Api/Functions/CheckAuthenticationCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,6 @@ public class CheckAuthenticationCode : Function<Ok>
[JsonConverter(typeof(Converter))]
[JsonProperty("code")]
public string Code { get; set; }

/// <summary>
/// If the user is not yet registered, the first name of the user; 1-64 characters. You can also pass an empty string for unregistered user there to check verification code validness. In the latter case PHONE_NUMBER_UNOCCUPIED error will be returned for a valid code
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("first_name")]
public string FirstName { get; set; }

/// <summary>
/// If the user is not yet registered; the last name of the user; optional; 0-64 characters
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("last_name")]
public string LastName { get; set; }
}
}
}
43 changes: 43 additions & 0 deletions TDLib.Api/Functions/RegisterUser.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using System;
using Newtonsoft.Json;

namespace TdLib
{
/// <summary>
/// Autogenerated TDLib APIs
/// </summary>
public partial class TdApi
{
/// <summary>
/// Checks the authentication code. Works only when the current authorization state is authorizationStateWaitCode
/// </summary>
public class RegisterUser : Function<Ok>
{
/// <summary>
/// Data type for serialization
/// </summary>
[JsonProperty("@type")]
public override string DataType { get; set; } = "registerUser";

/// <summary>
/// Extra data attached to the message
/// </summary>
[JsonProperty("@extra")]
public override string Extra { get; set; }

/// <summary>
/// If the user is not yet registered, the first name of the user; 1-64 characters. You can also pass an empty string for unregistered user there to check verification code validness. In the latter case PHONE_NUMBER_UNOCCUPIED error will be returned for a valid code
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("first_name")]
public string FirstName { get; set; }

/// <summary>
/// If the user is not yet registered; the last name of the user; optional; 0-64 characters
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("last_name")]
public string LastName { get; set; }
}
}
}
15 changes: 4 additions & 11 deletions TDLib.Api/Functions/SetAuthenticationPhoneNumber.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,20 +31,13 @@ public class SetAuthenticationPhoneNumber : Function<Ok>
[JsonConverter(typeof(Converter))]
[JsonProperty("phone_number")]
public string PhoneNumber { get; set; }

/// <summary>
/// Pass true if the authentication code may be sent via flash call to the specified phone number
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("allow_flash_call")]
public bool AllowFlashCall { get; set; }


/// <summary>
/// Pass true if the phone number is used on the current device. Ignored if allow_flash_call is false
/// Settings for the authentication of the user's phone number.
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("is_current_phone_number")]
public bool IsCurrentPhoneNumber { get; set; }
[JsonProperty("settings")]
public PhoneNumberAuthenticationSettings settings { get; set; }
}
}
}
14 changes: 0 additions & 14 deletions TDLib.Api/Objects/AuthorizationStateWaitCode.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,6 @@ public class AuthorizationStateWaitCode : AuthorizationState
[JsonProperty("@extra")]
public override string Extra { get; set; }

/// <summary>
/// True, if the user is already registered
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("is_registered")]
public bool IsRegistered { get; set; }

/// <summary>
/// Telegram terms of service, which should be accepted before user can continue registration; may be null
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("terms_of_service")]
public TermsOfService TermsOfService { get; set; }

/// <summary>
/// Information about the authorization code that was sent
/// </summary>
Expand Down
42 changes: 42 additions & 0 deletions TDLib.Api/Objects/AuthorizationStateWaitRegistration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
using System;
using Newtonsoft.Json;

namespace TdLib
{
/// <summary>
/// Autogenerated TDLib APIs
/// </summary>
public partial class TdApi
{
/// <summary>
/// TDLib needs the user's authentication code to finalize authorization
/// </summary>
public partial class AuthorizationState : Object
{
/// <summary>
/// TDLib needs the user's authentication code to finalize authorization
/// </summary>
public class AuthorizationStateWaitRegistration : AuthorizationState
{
/// <summary>
/// Data type for serialization
/// </summary>
[JsonProperty("@type")]
public override string DataType { get; set; } = "authorizationStateWaitRegistration";

/// <summary>
/// Extra data attached to the message
/// </summary>
[JsonProperty("@extra")]
public override string Extra { get; set; }

/// <summary>
/// Information about the authorization code that was sent
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("terms_of_service")]
public TermsOfService TermsOfService { get; set; }
}
}
}
}
50 changes: 50 additions & 0 deletions TDLib.Api/Objects/PhoneNumberAuthenticationSettings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
using System;
using Newtonsoft.Json;

namespace TdLib
{
/// <summary>
/// Autogenerated TDLib APIs
/// </summary>
public partial class TdApi
{
/// <summary>
/// A personal document, containing some information about a user
/// </summary>
public class PhoneNumberAuthenticationSettings : Object
{
/// <summary>
/// Data type for serialization
/// </summary>
[JsonProperty("@type")]
public override string DataType { get; set; } = "phoneNumberAuthenticationSettings";

/// <summary>
/// Extra data attached to the message
/// </summary>
[JsonProperty("@extra")]
public override string Extra { get; set; }

/// <summary>
/// Pass true if the authentication code may be sent via flash call to the specified phone number
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("allow_flash_call")]
public bool AllowFlashCall { get; set; }

/// <summary>
/// Pass true if the phone number is used on the current device. Ignored if allow_flash_call is false
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("is_current_phone_number")]
public bool IsCurrentPhoneNumber { get; set; }

/// <summary>
/// For official applications only. True, if the app can use Android SMS Retriever API (requires Google Play Services >= 10.2) to automatically receive the authentication code from the SMS. See https://developers.google.com/identity/sms-retriever/ for more details.
/// </summary>
[JsonConverter(typeof(Converter))]
[JsonProperty("allow_sms_retriever_api")]
public bool AllowSmsRetrieverApi { get; set; }
}
}
}
24 changes: 16 additions & 8 deletions TDLib/Bindings/Receiver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,27 @@ private void ProcessEvents()
var ct = _cts.Token;
while (!ct.IsCancellationRequested)
{
var data = _tdJsonClient.Receive(0.1);

if (!string.IsNullOrEmpty(data))
string data = null;
try
{
var structure = JsonConvert.DeserializeObject<TdApi.Object>(data, _converter);

Received?.Invoke(this, structure);
data = _tdJsonClient.Receive(0.1);

if (structure is TdApi.Update.UpdateAuthorizationState update)
if (!string.IsNullOrEmpty(data))
{
AuthorizationStateChanged?.Invoke(this, update.AuthorizationState);
var structure = JsonConvert.DeserializeObject<TdApi.Object>(data, _converter);

Received?.Invoke(this, structure);

if (structure is TdApi.Update.UpdateAuthorizationState update)
{
AuthorizationStateChanged?.Invoke(this, update.AuthorizationState);
}
}
}
catch (Exception ex)
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think printing to Console from the library is a good solution. Maybe event would fit better in this case

Copy link
Author

Choose a reason for hiding this comment

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

Added.

{
throw new TdApiNotFoundException(data, ex);
}
}
}

Expand Down
16 changes: 16 additions & 0 deletions TDLib/TdApiNotFoundException.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

namespace TdLib
{
public class TdApiNotFoundException : Exception
{
public readonly string ApiName;

public TdApiNotFoundException(string apiName) : this(apiName, null)
{
this.ApiName = apiName;
}

public TdApiNotFoundException(string apiName, Exception innerException) : base($"{apiName} Not found. Maybe your TdDLib.Api is outdated.", innerException) { }
}
}