-
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
Changes from 6 commits
67dfb41
d4994de
f732248
eb969c0
60a4579
592b44c
60440e0
7e62a69
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 |
---|---|---|
|
@@ -254,6 +254,25 @@ def session_context(self): | |
|
||
return self._session_context | ||
|
||
def to_json(self): | ||
""" | ||
Serializes an ApiInstance to JSON data | ||
|
||
:rtype: str | ||
""" | ||
return converter.class_to_json(self) | ||
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. Newline after docstring please |
||
|
||
@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 commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
|
||
:type data: str | ||
|
||
:rtype: ApiContext | ||
""" | ||
return converter.json_to_class(ApiContext, data) | ||
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. newline after the docstring please |
||
|
||
def save(self, path=None): | ||
""" | ||
:type path: str | ||
|
@@ -264,8 +283,8 @@ def save(self, path=None): | |
if path is None: | ||
path = self._PATH_API_CONTEXT_DEFAULT | ||
|
||
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): | ||
|
@@ -278,8 +297,13 @@ def restore(cls, path=None): | |
if path is None: | ||
path = cls._PATH_API_CONTEXT_DEFAULT | ||
|
||
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) | ||
|
||
|
||
class InstallationContext(object): | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
import os | ||
|
||
from bunq.sdk.context import ApiContext | ||
from bunq.sdk.json import converter | ||
from tests.bunq_test import BunqSdkTestCase | ||
|
||
|
||
class ApiContextTest(BunqSdkTestCase): | ||
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. For consistency, shouldn't this class be named 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. rip already merged 🙈 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. @OGKevin Please create a PR :P Nice catch! |
||
""" | ||
Tests: | ||
ApiContext | ||
""" | ||
|
||
_TMP_FILE_PATH = '/context-save-test.conf' | ||
|
||
@classmethod | ||
def setUpClass(cls): | ||
cls._FILE_MODE_READ = ApiContext._FILE_MODE_READ | ||
cls._API_CONTEXT = cls._get_api_context() | ||
cls._TMP_FILE_PATH_FULL = (cls._get_directory_test_root() + | ||
cls._TMP_FILE_PATH) | ||
|
||
def test_api_context_save(self): | ||
""" | ||
Converts an ApiContext to JSON data, saves the same ApiContext to a | ||
temporary file, and compares whether the JSON data is equal to the | ||
data in the file. | ||
|
||
Removes the temporary file before assertion. | ||
""" | ||
|
||
context_json = converter.class_to_json(self._API_CONTEXT) | ||
|
||
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() | ||
|
||
os.remove(self._TMP_FILE_PATH_FULL) | ||
|
||
self.assertEqual(context_retrieved, context_json) | ||
|
||
def test_api_context_restore(self): | ||
""" | ||
Saves an ApiContext to a temporary file, restores an ApiContext from | ||
that file, and compares whether the api_keys, tokens, and environment | ||
types are equal in the ApiContext and the restored ApiContext. | ||
|
||
Removes the temporary file before assertion. | ||
""" | ||
|
||
self._API_CONTEXT.save(self._TMP_FILE_PATH_FULL) | ||
api_context_restored = ApiContext.restore(self._TMP_FILE_PATH_FULL) | ||
|
||
os.remove(self._TMP_FILE_PATH_FULL) | ||
|
||
self.assertEqual(api_context_restored, self._API_CONTEXT) | ||
|
||
def test_api_context_save_json(self): | ||
""" | ||
Converts an ApiContext to JSON data, saves the ApiContext using the | ||
ApiContext.save() function with the to_JSON flag set to True, and | ||
compares whether the JSON data equals the returned JSON data from the | ||
ApiContext.save() function. | ||
""" | ||
|
||
context_json = converter.class_to_json(self._API_CONTEXT) | ||
context_saved = self._API_CONTEXT.to_json() | ||
|
||
self.assertEqual(context_saved, context_json) | ||
|
||
def test_api_context_restore_json(self): | ||
""" | ||
Saves an ApiContext with the ApiContext.save() function with the | ||
to_JSON flag set to True, restores an ApiContext from the JSON data | ||
returned from the ApiContext.save() function, and checks that the | ||
api_key, token, and environment type variables of the restored | ||
ApiContext are equal to the respective variables in the original | ||
ApiContext. | ||
""" | ||
|
||
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) |
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.