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
46 changes: 28 additions & 18 deletions bunq/sdk/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -254,45 +254,55 @@ def session_context(self):

return self._session_context

def save(self, path=None, to_json=False):
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.


: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

"""
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


@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.


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

"""
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


def save(self, path=None):
"""
:type path: str
:type to_json: bool

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

"""

if path is None:
path = self._PATH_API_CONTEXT_DEFAULT

if to_json:
return converter.class_to_json(self)

with open(path, self._FILE_MODE_WRITE) as file:
file.write(converter.class_to_json(self))
with open(path, self._FILE_MODE_WRITE) as file_:
file_.write(self.to_json())

@classmethod
def restore(cls, path=None, json_data=None):
def restore(cls, path=None):
"""
:type path: str
:type json_data: str

:rtype: ApiContext
"""

if path is None:
path = cls._PATH_API_CONTEXT_DEFAULT

if json_data is not None:
return converter.json_to_class(ApiContext, json_data)

with open(path, cls._FILE_MODE_READ) as file:
return converter.json_to_class(ApiContext, file.read())
with open(path, cls._FILE_MODE_READ) as file_:
return cls.from_json(file_.read())

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


class InstallationContext(object):
Expand Down
10 changes: 5 additions & 5 deletions tests/model/generated/test_api_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ def test_api_context_save(self):

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()
with open(self._TMP_FILE_PATH_FULL, self._FILE_MODE_READ) as file_:
context_retrieved = file_.read()

os.remove(self._TMP_FILE_PATH_FULL)

Expand Down Expand Up @@ -65,7 +65,7 @@ def test_api_context_save_json(self):
"""

context_json = converter.class_to_json(self._API_CONTEXT)
context_saved = self._API_CONTEXT.save(to_json=True)
context_saved = self._API_CONTEXT.to_json()

self.assertEqual(context_saved, context_json)

Expand All @@ -79,7 +79,7 @@ def test_api_context_restore_json(self):
ApiContext.
"""

context_json = self._API_CONTEXT.save(to_json=True)
api_context_restored = self._API_CONTEXT.restore(json_data=context_json)
context_json = self._API_CONTEXT.to_json()
api_context_restored = self._API_CONTEXT.from_json(context_json)

self.assertEqual(api_context_restored, self._API_CONTEXT)