-
Notifications
You must be signed in to change notification settings - Fork 24
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
Load and Save an ApiContext from and to JSON Data #13
Conversation
…o and from JSON data
…nctions in order to test whether modifying them in the last commit broke them or not.
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.
A few comments.
bunq/sdk/context.py
Outdated
@@ -254,33 +254,46 @@ def session_context(self): | |||
|
|||
return self._session_context | |||
|
|||
def save(self, path=None): | |||
def save(self, path=None, to_json=False): |
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.
Functions should do one thing. They should do it well. They should do it only.
-- Uncle Bob
Please make a separate public method (say, to_json
) and use it in save
.
(also, though not relevant with the change I suggest: Union
was introduced in Python 3.5, but we support Python 2 as well, so we do not want it)
bunq/sdk/context.py
Outdated
with open(path, self._FILE_MODE_WRITE) as file: | ||
file.write(converter.class_to_json(self)) | ||
|
||
@classmethod | ||
def restore(cls, path=None): | ||
def restore(cls, path=None, json_data=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.
Same, please create a public classmethod from_json
and use it in restore
.
bunq/sdk/context.py
Outdated
with open(path, cls._FILE_MODE_READ) as file: | ||
return converter.json_to_class(ApiContext, file.read()) | ||
|
||
def __eq__(self, other): | ||
return self.token == other.token \ |
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.
Please:
return (self.token == other.token and
self.api_key == other.api_key and
self.environment_type == other.environment_type)
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.
Hmm what about
if not x:
return False
if not y:
return False
return True
Looks cleaner IMO.
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.
I am not a big fan of this chaining thing when it comes to just comparison, but since we did it already before, it can be this, yes.
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.
I prefer option 1 with the and
statement.
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.
Also, about option 1: You don't seem to PEP8 your code then? At least my IDE gives me a warning when I do that: PEP8: Continuation line under-indented for visual indent
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.
@PJUllrich I might have the wrong indent there; probably adding one tab on the left for each of the indented lines will fix it? :)
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.
But let's go for option 1 then 👍
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.
This seems PEP8 compliant:
return (self.token == other.token and
self.api_key == other.api_key and
self.environment_type == other.environment_type)
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.
return (self.token == other.token and
self.api_key == other.api_key and
self.environment_type == other.environment_type)
Should do the trick 😁
|
||
self._API_CONTEXT.save(self._TMP_FILE_PATH_FULL) | ||
|
||
with open(self._TMP_FILE_PATH_FULL, self._FILE_MODE_READ) as file: |
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.
file
is a builtin in python please append a _
at the end of it or rename the variable.
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.
@OGKevin this was here before this PR.
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.
Just wanted to mention that :D
Shall I make it file_
?
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.
@dnl-blkv 😮 Yes @PJUllrich please change it to file_
self._API_CONTEXT.save(self._TMP_FILE_PATH_FULL) | ||
|
||
with open(self._TMP_FILE_PATH_FULL, self._FILE_MODE_READ) as file: | ||
context_retrieved = file.read() |
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.
Same as above.
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.
@OGKevin this was here before this PR.
bunq/sdk/context.py
Outdated
def __eq__(self, other): | ||
return self.token == other.token \ | ||
and self.api_key == other.api_key \ | ||
and self.environment_type == other.environment_type |
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.
I don't like the way this looks, maybe an if statement that exits with False
if the requirements aren't met and returns True
if it never exited?
What do you think @dnl-blkv
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.
We can chain if's, but I am ok with just using some healthy braces
… ApiContext.restore() Added beautiful braces to ApiContext.__eq__() Updated the unittests for ApiContext accordingly
bunq/sdk/context.py
Outdated
Creates an ApiContext instance from JSON data | ||
|
||
:param data: str | ||
:return: ApiContext |
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.
Shouldn't this be :rtype:
? And could you place a new line before :rtype:
&/or :return:
please.
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.
Two small comments 😁
bunq/sdk/context.py
Outdated
""" | ||
Serializes an ApiInstance to JSON data | ||
|
||
:return: str |
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.
Shouldn't this also be :rtype:
?
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.
@OGKevin correct, that should be!
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.
Changed that in the latest commit
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.
All tests passing @dnl-blkv
Quick question before you merge this PR: I could rename |
bunq/sdk/context.py
Outdated
|
||
:rtype: Union[None, str] | ||
:rtype: None, str |
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.
There is no str
anymore :)
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.
Good catch 😁
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.
Please make sure that in all the new code there are newlines after the docstrings (that's the standard we use throughout the SDK) :)
UPD: it is already present everywhere except the places I pointed out.
bunq/sdk/context.py
Outdated
@@ -254,6 +254,25 @@ def session_context(self): | |||
|
|||
return self._session_context | |||
|
|||
def to_json(self): | |||
""" | |||
Serializes an ApiInstance to JSON data |
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.
Serializes an ApiContext to JSON string.
bunq/sdk/context.py
Outdated
@classmethod | ||
def from_json(cls, data): | ||
""" | ||
Creates an ApiContext instance from JSON data |
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.
Creates an ApiContext instance from JSON string.
bunq/sdk/context.py
Outdated
|
||
:rtype: ApiContext | ||
""" | ||
return converter.json_to_class(ApiContext, data) |
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.
newline after the docstring please
bunq/sdk/context.py
Outdated
|
||
:rtype: str | ||
""" | ||
return converter.class_to_json(self) |
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.
Newline after docstring please
from tests.bunq_test import BunqSdkTestCase | ||
|
||
|
||
class ApiContextTest(BunqSdkTestCase): |
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.
For consistency, shouldn't this class be named TestApiContext
? @dnl-blkv
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.
rip already merged 🙈
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.
@OGKevin Please create a PR :P
Nice catch!
…update-sdk-version
Update the SDK version to 1.13.1. (bunq/sdk_python#13)
I added a small functionality to save and load an ApiContext to and from JSON Data.
Previously, the ApiContext was always saved to and loaded from a file, but this restricted me from saving the config of an ApiContext in a database. I also added unittests for the original ApiContext.save() and ApiContext.restore() functions and their modified versions.
I also added clarification for some of the variables that one need to set in the
conf.json
intests/example/
.