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

Load and Save an ApiContext from and to JSON Data #13

Merged
merged 8 commits into from
Aug 15, 2017
Merged

Load and Save an ApiContext from and to JSON Data #13

merged 8 commits into from
Aug 15, 2017

Conversation

PJUllrich
Copy link
Contributor

@PJUllrich PJUllrich commented Aug 15, 2017

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 in tests/example/.

Copy link
Contributor

@dnl-blkv dnl-blkv left a comment

Choose a reason for hiding this comment

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

A few comments.

@@ -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):
Copy link
Contributor

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)

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):
Copy link
Contributor

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.

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 \
Copy link
Contributor

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)

Copy link
Contributor

@OGKevin OGKevin Aug 15, 2017

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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.

Copy link
Contributor Author

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

Copy link
Contributor

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? :)

Copy link
Contributor

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 👍

Copy link
Contributor Author

@PJUllrich PJUllrich Aug 15, 2017

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)

Copy link
Contributor

@OGKevin OGKevin Aug 15, 2017

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:
Copy link
Contributor

@OGKevin OGKevin Aug 15, 2017

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.

Copy link
Contributor

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.

Copy link
Contributor Author

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_?

Copy link
Contributor

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()
Copy link
Contributor

Choose a reason for hiding this comment

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

Same as above.

Copy link
Contributor

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.

def __eq__(self, other):
return self.token == other.token \
and self.api_key == other.api_key \
and self.environment_type == other.environment_type
Copy link
Contributor

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

Copy link
Contributor

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

@OGKevin OGKevin added this to the v.0.10.0 milestone Aug 15, 2017
… ApiContext.restore()

Added beautiful braces to ApiContext.__eq__()

Updated the unittests for ApiContext accordingly
Creates an ApiContext instance from JSON data

:param data: str
:return: ApiContext
Copy link
Contributor

@OGKevin OGKevin Aug 15, 2017

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.

Copy link
Contributor

@OGKevin OGKevin left a comment

Choose a reason for hiding this comment

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

Two small comments 😁

"""
Serializes an ApiInstance to JSON data

:return: str
Copy link
Contributor

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: ?

Copy link
Contributor

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!

Copy link
Contributor Author

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

OGKevin
OGKevin previously approved these changes Aug 15, 2017
Copy link
Contributor

@OGKevin OGKevin left a 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

@PJUllrich
Copy link
Contributor Author

Quick question before you merge this PR:
ApiContext has now a to_json and from_json function, however still contains save() and restore(), which might now be a bit misleading.

I could rename save() to to_file() and restore() to from_file() to make it less ambiguous. What do you think?


:rtype: Union[None, str]
:rtype: None, str
Copy link
Contributor

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 :)

Copy link
Contributor

Choose a reason for hiding this comment

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

Good catch 😁

@PJUllrich
Copy link
Contributor Author

PJUllrich commented Aug 15, 2017

I think this PR can now be merged 😊

Copy link
Contributor

@dnl-blkv dnl-blkv left a 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.

@@ -254,6 +254,25 @@ def session_context(self):

return self._session_context

def to_json(self):
"""
Serializes an ApiInstance to JSON data
Copy link
Contributor

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.

@classmethod
def from_json(cls, data):
"""
Creates an ApiContext instance from JSON data
Copy link
Contributor

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.


:rtype: ApiContext
"""
return converter.json_to_class(ApiContext, data)
Copy link
Contributor

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


:rtype: str
"""
return converter.class_to_json(self)
Copy link
Contributor

@dnl-blkv dnl-blkv Aug 15, 2017

Choose a reason for hiding this comment

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

Newline after docstring please

@dnl-blkv dnl-blkv merged commit 35240cd into bunq:develop Aug 15, 2017
from tests.bunq_test import BunqSdkTestCase


class ApiContextTest(BunqSdkTestCase):
Copy link
Contributor

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

Copy link
Contributor

Choose a reason for hiding this comment

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

rip already merged 🙈

Copy link
Contributor

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!

@dnl-blkv
Copy link
Contributor

@andrederoos

OGKevin added a commit to bunq/tinker_python that referenced this pull request Aug 20, 2018
angelomelonas added a commit to bunq/tinker_python that referenced this pull request Jun 3, 2020
angelomelonas added a commit to bunq/tinker_python that referenced this pull request Jun 3, 2020
angelomelonas added a commit to bunq/tinker_python that referenced this pull request Jun 3, 2020
angelomelonas added a commit to bunq/tinker_python that referenced this pull request Jun 3, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants