-
Notifications
You must be signed in to change notification settings - Fork 0
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
Refactor account CLI commands and implementation #26
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,18 +1,11 @@ | ||||||
""" | ||||||
Provide forms for command line interface's account commands. | ||||||
""" | ||||||
import re | ||||||
from marshmallow import Schema | ||||||
|
||||||
from marshmallow import ( | ||||||
Schema, | ||||||
ValidationError, | ||||||
fields, | ||||||
validates, | ||||||
) | ||||||
|
||||||
from cli.constants import ( | ||||||
ADDRESS_REGEXP, | ||||||
DOMAIN_NAME_REGEXP, | ||||||
from cli.generic.forms.fields import ( | ||||||
AccountAddressField, | ||||||
NodeURLField, | ||||||
) | ||||||
|
||||||
|
||||||
|
@@ -21,29 +14,5 @@ class GetAccountBalanceForm(Schema): | |||||
Get balance of the account form. | ||||||
""" | ||||||
|
||||||
address = fields.String(required=True) | ||||||
node_url = fields.String(allow_none=True, required=False) | ||||||
|
||||||
@validates('address') | ||||||
def validate_address(self, address): | ||||||
""" | ||||||
Validate account address. | ||||||
""" | ||||||
if re.match(pattern=ADDRESS_REGEXP, string=address) is None: | ||||||
raise ValidationError(f'The following address `{address}` is invalid.') | ||||||
|
||||||
@validates('node_url') | ||||||
def validate_node_url(self, node_url): | ||||||
""" | ||||||
Validate node URL. | ||||||
|
||||||
If node URL is localhost, it means client didn't passed any URL, so nothing to validate. | ||||||
""" | ||||||
if node_url == 'localhost': | ||||||
return | ||||||
|
||||||
if 'http' in node_url or 'https' in node_url: | ||||||
raise ValidationError(f'Pass the following node URL `{node_url}` without protocol (http, https, etc.).') | ||||||
|
||||||
if re.match(pattern=DOMAIN_NAME_REGEXP, string=node_url) is None: | ||||||
raise ValidationError(f'The following node URL `{node_url}` is invalid.') | ||||||
address = AccountAddressField(required=True) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an extra check.
Suggested change
Because of click. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @Alladin9393, imagine we will migrate this code from CLI to Web. Do we need to pay additional attention to how CLI handle arguments? No, we don't. We just need to cut the CLI. I mean forms do not know about the CLI. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @dmytrostriletskyi, ok. |
||||||
node_url = NodeURLField(allow_none=True, required=False) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
""" | ||
Provide help messages for command line interface's account commands. | ||
""" | ||
GET_ACCOUNT_BALANCE_ADDRESS_ARGUMENT_HELP_MESSAGE = 'Account address to get a balance by.' | ||
ADDRESS_ARGUMENT_HELP_MESSAGE = 'Account address to get a balance by.' |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
Provide implementation of the custom fields. | ||
""" | ||
import re | ||
|
||
from marshmallow import ( | ||
ValidationError, | ||
fields, | ||
) | ||
|
||
from cli.constants import ( | ||
ADDRESS_REGEXP, | ||
DOMAIN_NAME_REGEXP, | ||
) | ||
|
||
|
||
class AccountAddressField(fields.Field): | ||
""" | ||
Implements validation of the account address. | ||
|
||
References: | ||
- https://marshmallow.readthedocs.io/en/3.0/custom_fields.html | ||
""" | ||
|
||
def _deserialize(self, value, attr, obj, **kwargs): | ||
""" | ||
Validate data (account address) that was passed to field. | ||
""" | ||
address = value | ||
|
||
if re.match(pattern=ADDRESS_REGEXP, string=address) is None: | ||
raise ValidationError(f'The following address `{address}` is invalid.') | ||
|
||
return address | ||
|
||
|
||
class NodeURLField(fields.Field): | ||
""" | ||
Implements validation of the node URL. | ||
|
||
If node URL is localhost, it means client didn't passed any URL, so nothing to validate. | ||
|
||
References: | ||
- https://marshmallow.readthedocs.io/en/3.0/custom_fields.html | ||
""" | ||
|
||
def _deserialize(self, value, attr, obj, **kwargs): | ||
""" | ||
Validate data (node URL) that was passed to field. | ||
""" | ||
node_url = value | ||
|
||
if node_url == 'localhost': | ||
return node_url | ||
|
||
if 'http' in node_url or 'https' in node_url: | ||
raise ValidationError(f'Pass the following node URL `{node_url}` without protocol (http, https, etc.).') | ||
|
||
if re.match(pattern=DOMAIN_NAME_REGEXP, string=node_url) is None: | ||
raise ValidationError(f'The following node URL `{node_url}` is invalid.') | ||
|
||
return node_url |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why it's needed here, if
Account(service=remme).get_balance(address=address)
always returnerror=None
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Alladin9393, I think, In the future, we will have general exception cathing in every service. Could we leave it here although there are no errors for now?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@dmytrostriletskyi, ok.