From 0e66971654e2b3117ef31849317b10995410ad7a Mon Sep 17 00:00:00 2001 From: Euiseo Cha Date: Fri, 16 Sep 2022 15:12:27 +0900 Subject: [PATCH] feat(breaking change): add more features and rename functions --- README.md | 2 +- nso_bridge/nso.py | 66 ++++++++++++++++- tests/test_main.py | 180 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 244 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index cbdc5e6..b4f6464 100644 --- a/README.md +++ b/README.md @@ -45,7 +45,7 @@ nso_res = NintendoSwitchOnlineAPI( session_token=session_token, ) nso_res.sync_login() -print(json.dumps(nso_res.getSelf(), indent=4, ensure_ascii=False)) +print(json.dumps(nso_res.getCurrentUser(), indent=4, ensure_ascii=False)) ``` diff --git a/nso_bridge/nso.py b/nso_bridge/nso.py index 5667de9..58bb86b 100644 --- a/nso_bridge/nso.py +++ b/nso_bridge/nso.py @@ -30,6 +30,7 @@ def get_response(self): if api_resp.status_code != 200: raise Exception(f"{api_resp.json()}") rs = api_resp.json() + print(rs) f = rs["f"] uuid = rs["request_id"] timestamp = rs["timestamp"] @@ -120,9 +121,11 @@ def __init__( self.login = {"login": None, "time": 0} - def getSelf(self): - """Get information of My Nintendo Switch Account.""" - resp = requests.post(url=self.url + "/v3/User/ShowSelf", headers=self.headers) + def getAnnouncements(self): + """Get information of announcements.""" + resp = requests.post( + url=self.url + "/v3/Announcement/List", headers=self.headers + ) if resp.status_code != 200: raise Exception(f"Error: {resp.status_code}") return resp.json() @@ -134,6 +137,63 @@ def getFriends(self): raise Exception(f"Error: {resp.status_code}") return resp.json() + def getWebServices(self): + """Get information of web services registered to Nintendo Switch account.""" + resp = requests.post( + url=self.url + "/v1/Game/ListWebServices", headers=self.headers + ) + if resp.status_code != 200: + raise Exception(f"Error: {resp.status_code}") + return resp.json() + + def getActiveEvent(self): + """Get information of active events.""" + resp = requests.post( + url=self.url + "/v1/Event/GetActiveEvent", headers=self.headers + ) + if resp.status_code != 200: + raise Exception(f"Error: {resp.status_code}") + return resp.json() + + def getEvent(self): + """Get information of events.""" + resp = requests.post(url=self.url + "/v1/Event/Show", headers=self.headers) + if resp.status_code != 200: + raise Exception(f"Error: {resp.status_code}") + return resp.json() + + def getUser(self): + """Get information of user.""" + resp = requests.post(url=self.url + "/v1/User/Show", headers=self.headers) + if resp.status_code != 200: + raise Exception(f"Error: {resp.status_code}") + return resp.json() + + def getCurrentUser(self): + """Get information of My Nintendo Switch Account.""" + resp = requests.post(url=self.url + "/v3/User/ShowSelf", headers=self.headers) + if resp.status_code != 200: + raise Exception(f"Error: {resp.status_code}") + return resp.json() + + def getFriendCodeUrl(self): + """Get information of friend code URL.""" + resp = requests.post( + url=self.url + "/v3/Friend/CreateFriendCodeUrl", headers=self.headers + ) + if resp.status_code != 200: + raise Exception(f"Error: {resp.status_code}") + return resp.json() + + def getCurrentUserPermissions(self): + """Get information of current user permissions.""" + resp = requests.post( + url=self.url + "/v3/User/Permissions/ShowSelf", headers=self.headers + ) + if resp.status_code != 200: + raise Exception(f"Error: {resp.status_code}") + return resp.json() + def sync_login(self): wasc_access_token = keyring.get_password("nso-bridge", "login") wasc_time = keyring.get_password("nso-bridge", "wasc_time") diff --git a/tests/test_main.py b/tests/test_main.py index 066cf35..98cbca4 100644 --- a/tests/test_main.py +++ b/tests/test_main.py @@ -4,6 +4,15 @@ from nso_bridge.nso import NintendoSwitchOnlineAPI +def test_get_nso_app_version(): + try: + app = NintendoSwitchAccount() + app.get_nso_app_version() + assert True + except Exception: + assert False + + def test_nso_login(): try: app = NintendoSwitchAccount() @@ -20,3 +29,174 @@ def test_nso_login(): assert True except Exception: assert False + + +def test_getAnnouncements(): + try: + app = NintendoSwitchAccount() + + # override the nso_app_version + app.nso_app_version = app.get_nso_app_version() + session_token = keyring.get_password("nso-bridge", "session_token") + + nso_res = NintendoSwitchOnlineAPI( + nso_app_version=app.nso_app_version, + session_token=session_token, + ) + nso_res.sync_login() + nso_res.getAnnouncements() + assert True + except Exception: + assert False + + +def test_getFriends(): + try: + app = NintendoSwitchAccount() + + # override the nso_app_version + app.nso_app_version = app.get_nso_app_version() + session_token = keyring.get_password("nso-bridge", "session_token") + + nso_res = NintendoSwitchOnlineAPI( + nso_app_version=app.nso_app_version, + session_token=session_token, + ) + nso_res.sync_login() + nso_res.getFriends() + assert True + except Exception: + assert False + + +def test_getWebServices(): + try: + app = NintendoSwitchAccount() + + # override the nso_app_version + app.nso_app_version = app.get_nso_app_version() + session_token = keyring.get_password("nso-bridge", "session_token") + + nso_res = NintendoSwitchOnlineAPI( + nso_app_version=app.nso_app_version, + session_token=session_token, + ) + nso_res.sync_login() + nso_res.getWebServices() + assert True + except Exception: + assert False + + +def test_getActiveEvent(): + try: + app = NintendoSwitchAccount() + + # override the nso_app_version + app.nso_app_version = app.get_nso_app_version() + session_token = keyring.get_password("nso-bridge", "session_token") + + nso_res = NintendoSwitchOnlineAPI( + nso_app_version=app.nso_app_version, + session_token=session_token, + ) + nso_res.sync_login() + nso_res.getActiveEvent() + assert True + except Exception: + assert False + + +def test_getEvent(): + try: + app = NintendoSwitchAccount() + + # override the nso_app_version + app.nso_app_version = app.get_nso_app_version() + session_token = keyring.get_password("nso-bridge", "session_token") + + nso_res = NintendoSwitchOnlineAPI( + nso_app_version=app.nso_app_version, + session_token=session_token, + ) + nso_res.sync_login() + nso_res.getEvent() + assert True + except Exception: + assert False + + +def test_getUser(): + try: + app = NintendoSwitchAccount() + + # override the nso_app_version + app.nso_app_version = app.get_nso_app_version() + session_token = keyring.get_password("nso-bridge", "session_token") + + nso_res = NintendoSwitchOnlineAPI( + nso_app_version=app.nso_app_version, + session_token=session_token, + ) + nso_res.sync_login() + nso_res.getUser() + assert True + except Exception: + assert False + + +def test_getCurrentUser(): + try: + app = NintendoSwitchAccount() + + # override the nso_app_version + app.nso_app_version = app.get_nso_app_version() + session_token = keyring.get_password("nso-bridge", "session_token") + + nso_res = NintendoSwitchOnlineAPI( + nso_app_version=app.nso_app_version, + session_token=session_token, + ) + nso_res.sync_login() + nso_res.getCurrentUser() + assert True + except Exception: + assert False + + +def test_getFriendCodeUrl(): + try: + app = NintendoSwitchAccount() + + # override the nso_app_version + app.nso_app_version = app.get_nso_app_version() + session_token = keyring.get_password("nso-bridge", "session_token") + + nso_res = NintendoSwitchOnlineAPI( + nso_app_version=app.nso_app_version, + session_token=session_token, + ) + nso_res.sync_login() + nso_res.getFriendCodeUrl() + assert True + except Exception: + assert False + + +def test_getCurrentUserPermissions(): + try: + app = NintendoSwitchAccount() + + # override the nso_app_version + app.nso_app_version = app.get_nso_app_version() + session_token = keyring.get_password("nso-bridge", "session_token") + + nso_res = NintendoSwitchOnlineAPI( + nso_app_version=app.nso_app_version, + session_token=session_token, + ) + nso_res.sync_login() + nso_res.getCurrentUserPermissions() + assert True + except Exception: + assert False