-
Notifications
You must be signed in to change notification settings - Fork 25
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 1 commit
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,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 | ||
|
||
:return: str | ||
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. Shouldn't this also be 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 correct, that should be! 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. Changed that in the latest commit |
||
""" | ||
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.
|
||
|
||
:param data: str | ||
:return: ApiContext | ||
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. Shouldn't this be |
||
""" | ||
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 | ||
:type to_json: bool | ||
|
||
:rtype: Union[None, str] | ||
:rtype: None, str | ||
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. There is no 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. 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): | ||
|
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.