CSC/ECE 517 Spring 2020/P2000.Refactor pyrh oauth capabilities to use marshmallow: Difference between revisions

From Expertiza_Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
Line 2: Line 2:
== Introduction ==
== Introduction ==


This project contributed to an open-source Python project called [https://github.com/robinhood-unofficial/pyrh pyrh].
This project contributed to an open-source Python project called [https://github.com/robinhood-unofficial/pyrh pyrh]. This is an unofficial python API for the Robinhood trading platform. It allows users to programmatically interact with their specific user accounts.


=== pyrh ===
== Pre-Development Documentation ==
 
[https://github.com/robinhood-unofficial/pyrh pyrh] is an unofficial python API for the Robinhood trading platform. It allows users to programmatically interact with their specific user accounts.


=== Project Purpose ===
=== Project Purpose ===
Line 12: Line 10:
The goal of this project is to refactor the initial OAuth class that was created to leverage API best practices handling serialization/deserialization. An existing project called [https://marshmallow.readthedocs.io/en/stable/ marshmallow] handles the use case of the custom code that was initially written to manage serialization and deserialization. It would be prudent to adopt that package. Additionally, the internal code that passed around JSON dictionaries likely should use python object models which can more easily be manipulated allowing the business logic to be moved to the models. Additionally, this data model-based framework will make it easy to implement further refactors as more data models are introduced for other parts of the API besides auth.
The goal of this project is to refactor the initial OAuth class that was created to leverage API best practices handling serialization/deserialization. An existing project called [https://marshmallow.readthedocs.io/en/stable/ marshmallow] handles the use case of the custom code that was initially written to manage serialization and deserialization. It would be prudent to adopt that package. Additionally, the internal code that passed around JSON dictionaries likely should use python object models which can more easily be manipulated allowing the business logic to be moved to the models. Additionally, this data model-based framework will make it easy to implement further refactors as more data models are introduced for other parts of the API besides auth.


== Project Design ==
=== Project Design ===
 
(Please note that this is a custom project and does not have an Expertiza project requirements)
 
In order to more properly follow object-oriented design practices, the large SessionManager class will be aided by data manager classes that encapsulate both the data and business logic associated with that data to cut on bloat. The *Schema classes will aid in the serialization/deserialization of those data model classes. The use of the existing python package "Marshmallow" will aid in that data serialization/validation process.


New classes:
New classes:
Line 28: Line 30:


[[File:oauth.jpeg]]
[[File:oauth.jpeg]]
OAuth inheritance diagram
OAuth inheritance diagram


[[File:sessionmanager.jpeg]]
[[File:sessionmanager.jpeg]]
SessionManager inheritance diagram
SessionManager inheritance diagram


== Code Modifications ==
=== Testing Plan ===
 
TBD


== Testing Plan ==
The testing plan is quite simple in this case and revolves around these key ideas:


* Test that the high-level login/logout functionality works
* Test that none of the existing authentication functionality is broken
* Test the low-level OAuth functionality
* Test that the new data models can be serialized/deserialized
* Test the get/post functionality with API
* Test that the SessionManager class can be serialized to the custom data cache location
* Test caching user credential abilities
* Update tests to use [https://github.com/spulec/freezegun FreezeGun] where DateTime is required
* Use mock data to set up tests (as live testing cannot be done with the API, test accounts are not provided)


Tests are seen in the unit testing file. [https://github.com/robinhood-unofficial/pyrh/blob/78c16005860ed3801c67b12fbe5664b8be8c646b/tests/test_sessionmanager.py]
== Post-Development Documentation ==


These tests can be run by going through the documentation which is listed below.
=== Code Modifications ===


== Setting up the project to run the unit tests ==
TODO


The developer guidelines can be found at readthedocs. [https://pyrh.readthedocs.io/en/latest/developers.html] Please specifically look for the section on how to run pytest. (unit testing)
=== Setting up the project to run the unit tests ===


Additionally, the automatic class reference documentation generated can be found in the documentation as well. [https://pyrh.readthedocs.io/en/latest/stubs/pyrh.SessionManager.html] A guide on how to test the project can be found in the project Jupiter notebook [https://github.com/robinhood-unofficial/pyrh/blob/master/notebooks/example.ipynb]
TODO


== Team Members ==
== Team Members ==


* Adithya Balaji
* Adithya Balaji

Revision as of 01:55, 9 April 2020

Introduction

This project contributed to an open-source Python project called pyrh. This is an unofficial python API for the Robinhood trading platform. It allows users to programmatically interact with their specific user accounts.

Pre-Development Documentation

Project Purpose

The goal of this project is to refactor the initial OAuth class that was created to leverage API best practices handling serialization/deserialization. An existing project called marshmallow handles the use case of the custom code that was initially written to manage serialization and deserialization. It would be prudent to adopt that package. Additionally, the internal code that passed around JSON dictionaries likely should use python object models which can more easily be manipulated allowing the business logic to be moved to the models. Additionally, this data model-based framework will make it easy to implement further refactors as more data models are introduced for other parts of the API besides auth.

Project Design

(Please note that this is a custom project and does not have an Expertiza project requirements)

In order to more properly follow object-oriented design practices, the large SessionManager class will be aided by data manager classes that encapsulate both the data and business logic associated with that data to cut on bloat. The *Schema classes will aid in the serialization/deserialization of those data model classes. The use of the existing python package "Marshmallow" will aid in that data serialization/validation process.

New classes:

  • BaseModel: a model that any new model will inherit from, provides default data representation functionality and inherit from Python's SimpleNamespace
  • BaseSchema: a Schema class that provides default functionality for serializing the object attributes that matter for a particular object
  • Challenge: A model that represents the data returned by an OAuth Challenge from that designated endpoint
  • ChallengeSchema: the schema and validations associated with the Challenge model
  • OAuth: A model that represents the data returned by the OAuth token generation endpoint
  • OAuthSchema: the schema and validations associated with the OAuth model
  • SessionManager (existing): a class that implements functionality to interact with the Robinhood authorization endpoint API
  • SessionManagerSchema: the schema associated with the SessionManager model

The inheritance diagrams are listed below:

OAuth inheritance diagram

SessionManager inheritance diagram

Testing Plan

The testing plan is quite simple in this case and revolves around these key ideas:

  • Test that none of the existing authentication functionality is broken
  • Test that the new data models can be serialized/deserialized
  • Test that the SessionManager class can be serialized to the custom data cache location
  • Update tests to use FreezeGun where DateTime is required

Post-Development Documentation

Code Modifications

TODO

Setting up the project to run the unit tests

TODO

Team Members

  • Adithya Balaji