From d8d840dd73de19e54a8f60ab6be74e3c5c13e0eb Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 10:30:42 -0700 Subject: [PATCH 01/29] made quotation marks consistent for strings. --- api_intro.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api_intro.py b/api_intro.py index aac1ce8..f3b1dbc 100644 --- a/api_intro.py +++ b/api_intro.py @@ -23,7 +23,7 @@ def get_access_token(): def get_person(access_token): onid = input("Enter Person's ONID: ") - params = {'onid': onid} + params = {"onid": onid} headers = {"Content-Type": "application/json", "Authorization": "Bearer " + access_token} request = requests.get(personsUrl, params=params, headers=headers) From 3ff0e00dd6e3f1b940ee492957838411b542fd01 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 10:39:33 -0700 Subject: [PATCH 02/29] fixed styling to be consistent with flake8 and osu style guides. --- api_intro.py | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/api_intro.py b/api_intro.py index f3b1dbc..ec17318 100644 --- a/api_intro.py +++ b/api_intro.py @@ -5,18 +5,21 @@ personsUrl = "https://api.oregonstate.edu/v1/persons" authUrl = "https://api.oregonstate.edu/oauth2/token" + # Request access_token from osu api # Read in consumer key and consumer secret from user def get_access_token(): key = input("Enter Consumer Key: ") secret = input("Enter Consumer Secret: ") - data = {"client_id": key, "client_secret": secret, "grant_type": "client_credentials"} + data = {"client_id": key, "client_secret": secret, + "grant_type": "client_credentials"} request = requests.post(authUrl, data=data) response = request.json() return response["access_token"] + # Make get request for information about a person at osu # Read in ONID from user # requires access_token retrieved in get_access_token() @@ -24,7 +27,8 @@ def get_person(access_token): onid = input("Enter Person's ONID: ") params = {"onid": onid} - headers = {"Content-Type": "application/json", "Authorization": "Bearer " + access_token} + headers = {"Content-Type": "application/json", + "Authorization": "Bearer " + access_token} request = requests.get(personsUrl, params=params, headers=headers) response = request.json() @@ -34,6 +38,6 @@ def get_person(access_token): if __name__ == "__main__": access_token = get_access_token() personData = get_person(access_token) - + for person in personData: - print("Person's Name: " + person["attributes"]["firstName"]) \ No newline at end of file + print("Person's Name: " + person["attributes"]["firstName"]) From fec830b0da44a2fbb59659eafe08a4bcde42d752 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 10:48:24 -0700 Subject: [PATCH 03/29] created readme --- README.md | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..38240fd --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# Alex API Intro ![python](https://img.shields.io/badge/python-3.7-blue.svg) + +Introduction to using OSU's APIs + From b6c47afbfb131be513eba507ef9c66d353873178 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 10:55:21 -0700 Subject: [PATCH 04/29] added requirements.txt --- requirements.txt | 1 + 1 file changed, 1 insertion(+) create mode 100644 requirements.txt diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..e20605c --- /dev/null +++ b/requirements.txt @@ -0,0 +1 @@ +requests==2.22.0 \ No newline at end of file From 8df0c30f7bc277dbb0237c0eea7b2dad5986259b Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 11:00:00 -0700 Subject: [PATCH 05/29] added Usage steps to the readme --- README.md | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/README.md b/README.md index 38240fd..8bdf2d5 100644 --- a/README.md +++ b/README.md @@ -2,3 +2,19 @@ Introduction to using OSU's APIs +## Configuration + + + +## Usage + +1. Install dependencies via pipL + + ```shell + $ pip install -r requirements.txt + ``` +2. Run the script: + + ```shell + $ python api_intro.py --config path/to/configuration.json + ``` \ No newline at end of file From bc2125c7b621641ded4a4100c4a5d5741ebadc10 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 11:01:39 -0700 Subject: [PATCH 06/29] fixed typo on usage step 1. L -> : --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 8bdf2d5..f6df75c 100644 --- a/README.md +++ b/README.md @@ -8,7 +8,7 @@ Introduction to using OSU's APIs ## Usage -1. Install dependencies via pipL +1. Install dependencies via pip: ```shell $ pip install -r requirements.txt From 310ee212125048bcbcadb15bf6e87faed5c9f8ca Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 11:11:11 -0700 Subject: [PATCH 07/29] changed from sting concatenation to use f string --- api_intro.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api_intro.py b/api_intro.py index ec17318..738f021 100644 --- a/api_intro.py +++ b/api_intro.py @@ -28,7 +28,7 @@ def get_person(access_token): params = {"onid": onid} headers = {"Content-Type": "application/json", - "Authorization": "Bearer " + access_token} + "Authorization": f"Bearer {access_token}"} request = requests.get(personsUrl, params=params, headers=headers) response = request.json() From 5ab69f3ad807e13d0355d4b45f180177555aec0c Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 11:18:40 -0700 Subject: [PATCH 08/29] created example configuration file --- configuration-example.json | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 configuration-example.json diff --git a/configuration-example.json b/configuration-example.json new file mode 100644 index 0000000..87194cd --- /dev/null +++ b/configuration-example.json @@ -0,0 +1,10 @@ +{ + "api": { + "locations_url": "https://api.oregonstate.edu/v1/services" + }, + "oauth2": { + "auth_api_url": "https://api.oregonstate.edu/oauth2/token", + "client_id": "your_client_id", + "client_secret": "your_client_secret" + } +} \ No newline at end of file From 4a2ea6a4099b1a197663bc741e43415e4e75b834 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 11:32:16 -0700 Subject: [PATCH 09/29] removed api urls from main script. They exist in configuration-example.json now. --- api_intro.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/api_intro.py b/api_intro.py index 738f021..2d3a027 100644 --- a/api_intro.py +++ b/api_intro.py @@ -2,8 +2,6 @@ # authorize and send requests using OSU's API import requests -personsUrl = "https://api.oregonstate.edu/v1/persons" -authUrl = "https://api.oregonstate.edu/oauth2/token" # Request access_token from osu api From 472a0e41ad2f29169b31c20a6342d9bc7ef4494c Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 11:33:17 -0700 Subject: [PATCH 10/29] changed double quotes to single quotes --- api_intro.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/api_intro.py b/api_intro.py index 2d3a027..d8748e9 100644 --- a/api_intro.py +++ b/api_intro.py @@ -7,35 +7,36 @@ # Request access_token from osu api # Read in consumer key and consumer secret from user def get_access_token(): - key = input("Enter Consumer Key: ") - secret = input("Enter Consumer Secret: ") + key = input('Enter Consumer Key: ') + secret = input('Enter Consumer Secret: ') - data = {"client_id": key, "client_secret": secret, - "grant_type": "client_credentials"} + data = {'client_id': key, 'client_secret': secret, + 'grant_type': 'client_credentials'} request = requests.post(authUrl, data=data) response = request.json() - return response["access_token"] + return response['access_token'] # Make get request for information about a person at osu # Read in ONID from user # requires access_token retrieved in get_access_token() def get_person(access_token): - onid = input("Enter Person's ONID: ") + onid = input('Enter Person\'s ONID: ') - params = {"onid": onid} - headers = {"Content-Type": "application/json", - "Authorization": f"Bearer {access_token}"} + params = {'onid': onid} + headers = {'Content-Type': 'application/json', + 'Authorization': f'Bearer {access_token}'} request = requests.get(personsUrl, params=params, headers=headers) response = request.json() - return response["data"] + return response['data'] -if __name__ == "__main__": +if __name__ == '__main__': + access_token = get_access_token() personData = get_person(access_token) for person in personData: - print("Person's Name: " + person["attributes"]["firstName"]) + print('Person\'s Name: ' + person['attributes']['firstName']) From ee88f4ac65fc61a7cc99c5d550642b6e32fecd80 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 11:44:45 -0700 Subject: [PATCH 11/29] added persons api url to configuration --- configuration-example.json | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/configuration-example.json b/configuration-example.json index 87194cd..68d1a48 100644 --- a/configuration-example.json +++ b/configuration-example.json @@ -1,6 +1,7 @@ { "api": { - "locations_url": "https://api.oregonstate.edu/v1/services" + "locations_url": "https://api.oregonstate.edu/v1/services", + "persons_url": "https://api.oregonstate.edu/v1/persons" }, "oauth2": { "auth_api_url": "https://api.oregonstate.edu/oauth2/token", From 172ba786310b6353cc0df250a589d8f9d21fd054 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 11:50:32 -0700 Subject: [PATCH 12/29] changed to read oauth2 and api urls from configuration file. --- api_intro.py | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/api_intro.py b/api_intro.py index d8748e9..709265b 100644 --- a/api_intro.py +++ b/api_intro.py @@ -1,16 +1,15 @@ # api_intro.py # authorize and send requests using OSU's API -import requests +import sys +import json +import requests # Request access_token from osu api # Read in consumer key and consumer secret from user -def get_access_token(): - key = input('Enter Consumer Key: ') - secret = input('Enter Consumer Secret: ') - - data = {'client_id': key, 'client_secret': secret, +def get_access_token(authUrl, id, secret): + data = {'client_id': id, 'client_secret': secret, 'grant_type': 'client_credentials'} request = requests.post(authUrl, data=data) response = request.json() @@ -21,22 +20,39 @@ def get_access_token(): # Make get request for information about a person at osu # Read in ONID from user # requires access_token retrieved in get_access_token() -def get_person(access_token): +def get_person(access_token, apiUrl): + onid = input('Enter Person\'s ONID: ') + + params = {'onid': onid} + headers = {'Content-Type': 'application/json', + 'Authorization': f'Bearer {access_token}'} + + request = requests.get(apiUrl, params=params, headers=headers) + response = request.json() + return response['data'] + + +def get_locations(access_token, apiUrl): onid = input('Enter Person\'s ONID: ') params = {'onid': onid} headers = {'Content-Type': 'application/json', 'Authorization': f'Bearer {access_token}'} - request = requests.get(personsUrl, params=params, headers=headers) + request = requests.get(apiUrl, params=params, headers=headers) response = request.json() return response['data'] if __name__ == '__main__': + configPath = sys.argv[1] + with open(configPath, 'r') as configFile: + config = json.load(configFile) + personsUrl = config['api']['persons_url'] + locationsUrl = config['api']['locations_url'] + authUrl = config['oauth2']['auth_api_url'] + clientId = config['oauth2']['client_id'] + clientSecret = config['oauth2']['client_secret'] - access_token = get_access_token() - personData = get_person(access_token) + access_token = get_access_token(authUrl, clientId, clientSecret) - for person in personData: - print('Person\'s Name: ' + person['attributes']['firstName']) From 5054daa1b3542856dbc54f9e6a9aad5a6d86883f Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 11:53:01 -0700 Subject: [PATCH 13/29] changed locations to directory. --- api_intro.py | 5 ++--- configuration-example.json | 2 +- 2 files changed, 3 insertions(+), 4 deletions(-) diff --git a/api_intro.py b/api_intro.py index 709265b..700d922 100644 --- a/api_intro.py +++ b/api_intro.py @@ -32,7 +32,7 @@ def get_person(access_token, apiUrl): return response['data'] -def get_locations(access_token, apiUrl): +def get_directory(access_token, apiUrl): onid = input('Enter Person\'s ONID: ') params = {'onid': onid} @@ -49,10 +49,9 @@ def get_locations(access_token, apiUrl): with open(configPath, 'r') as configFile: config = json.load(configFile) personsUrl = config['api']['persons_url'] - locationsUrl = config['api']['locations_url'] + directoryUrl = config['api']['locations_url'] authUrl = config['oauth2']['auth_api_url'] clientId = config['oauth2']['client_id'] clientSecret = config['oauth2']['client_secret'] access_token = get_access_token(authUrl, clientId, clientSecret) - diff --git a/configuration-example.json b/configuration-example.json index 68d1a48..b4aa35b 100644 --- a/configuration-example.json +++ b/configuration-example.json @@ -1,6 +1,6 @@ { "api": { - "locations_url": "https://api.oregonstate.edu/v1/services", + "directory_url": "https://api.oregonstate.edu/v1/directory", "persons_url": "https://api.oregonstate.edu/v1/persons" }, "oauth2": { From d6fc3afe07f9fe96d6235636b4bcbde17bb10513 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 12:05:21 -0700 Subject: [PATCH 14/29] had trouble getting directory working. switched back to persons --- api_intro.py | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/api_intro.py b/api_intro.py index 700d922..84e6286 100644 --- a/api_intro.py +++ b/api_intro.py @@ -33,14 +33,15 @@ def get_person(access_token, apiUrl): def get_directory(access_token, apiUrl): - onid = input('Enter Person\'s ONID: ') + osuId = input('Enter Directory Search Query: ') - params = {'onid': onid} + # params = {'q': onid} headers = {'Content-Type': 'application/json', 'Authorization': f'Bearer {access_token}'} - request = requests.get(apiUrl, params=params, headers=headers) + request = requests.get(f'{apiUrl}/{osuId}', headers=headers) response = request.json() + print(response) return response['data'] @@ -49,9 +50,13 @@ def get_directory(access_token, apiUrl): with open(configPath, 'r') as configFile: config = json.load(configFile) personsUrl = config['api']['persons_url'] - directoryUrl = config['api']['locations_url'] + directoryUrl = config['api']['directory_url'] authUrl = config['oauth2']['auth_api_url'] clientId = config['oauth2']['client_id'] clientSecret = config['oauth2']['client_secret'] access_token = get_access_token(authUrl, clientId, clientSecret) + personsData = get_person(access_token, personsUrl) + + for person in personsData: + print(f'Name: {person["attributes"]["firstName"]}') From bc8f61989e5967f0634b45b08547507113d995b2 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 12:08:22 -0700 Subject: [PATCH 15/29] wrote configuration section on readme --- README.md | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index f6df75c..2c43178 100644 --- a/README.md +++ b/README.md @@ -4,7 +4,16 @@ Introduction to using OSU's APIs ## Configuration - +1. Register an application on [OSU Developer Portal](https://developer.oregonstate.edu/) +2. Get `client_id` and `client_secret` from your app, then copy[configuration-example.json](./configuration-example.json) as `configuration.json` and fill in the oauth2 section: + + ```json + "oauth2": { + "auth_api_url": "https://api.oregonstate.edu/oauth2/token", + "client_id": "your_client_id", + "client_secret": "your_client_secret" + } + ``` ## Usage From 61570687749d5ac9c0b418c0cd623e0715cae0cc Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 12:22:37 -0700 Subject: [PATCH 16/29] added error handling --- api_intro.py | 37 +++++++++++++++++++++++++++---------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/api_intro.py b/api_intro.py index 84e6286..fc32c6e 100644 --- a/api_intro.py +++ b/api_intro.py @@ -14,22 +14,39 @@ def get_access_token(authUrl, id, secret): request = requests.post(authUrl, data=data) response = request.json() - return response['access_token'] + try: + response['access_token'] + return response['access_token'] + except KeyError: + if response["ErrorCode"] == "invalid_client": + print("Client Id or Client Secret invalid. " + + "Please check your configuration.json " + + "file and try again.") + else: + print("Unknown error occurred.") + exit() # Make get request for information about a person at osu # Read in ONID from user # requires access_token retrieved in get_access_token() def get_person(access_token, apiUrl): - onid = input('Enter Person\'s ONID: ') - - params = {'onid': onid} - headers = {'Content-Type': 'application/json', - 'Authorization': f'Bearer {access_token}'} - - request = requests.get(apiUrl, params=params, headers=headers) - response = request.json() - return response['data'] + responseData = [] + while len(responseData) < 1: + onid = input('Enter Person\'s ONID: ') + + params = {'onid': onid} + headers = {'Content-Type': 'application/json', + 'Authorization': f'Bearer {access_token}'} + + request = requests.get(apiUrl, params=params, headers=headers) + response = request.json() + responseData = response['data'] + if len(responseData) < 1: + print(f'No data found for \"{onid}\". ' + + 'Please try a different search query.') + + return responseData def get_directory(access_token, apiUrl): From c7b687f1b167432f5383847f0ef5116c24f73564 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 12:23:38 -0700 Subject: [PATCH 17/29] fixed typo in readme --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2c43178..170f8f6 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,7 @@ Introduction to using OSU's APIs ## Configuration 1. Register an application on [OSU Developer Portal](https://developer.oregonstate.edu/) -2. Get `client_id` and `client_secret` from your app, then copy[configuration-example.json](./configuration-example.json) as `configuration.json` and fill in the oauth2 section: +2. Get `client_id` and `client_secret` from your app, then copy [configuration-example.json](./configuration-example.json) as `configuration.json` and fill in the oauth2 section: ```json "oauth2": { From b14b3071836c71f188ef7c3835137f6968552615 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 12:26:05 -0700 Subject: [PATCH 18/29] updated to python3 in readme --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 170f8f6..8cabe19 100644 --- a/README.md +++ b/README.md @@ -20,10 +20,10 @@ Introduction to using OSU's APIs 1. Install dependencies via pip: ```shell - $ pip install -r requirements.txt + $ pip3 install -r requirements.txt ``` 2. Run the script: ```shell - $ python api_intro.py --config path/to/configuration.json + $ python3 api_intro.py path/to/configuration.json ``` \ No newline at end of file From 3816016bbef917056f795ea20806c2b9949f87cf Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 13:39:21 -0700 Subject: [PATCH 19/29] Made directory request work now that the app has access --- api_intro.py | 25 +++++++++++++++---------- 1 file changed, 15 insertions(+), 10 deletions(-) diff --git a/api_intro.py b/api_intro.py index fc32c6e..cb4c9e8 100644 --- a/api_intro.py +++ b/api_intro.py @@ -50,15 +50,20 @@ def get_person(access_token, apiUrl): def get_directory(access_token, apiUrl): - osuId = input('Enter Directory Search Query: ') + responseData = [] + while len(responseData) < 1: + query = input('Enter Directory Search Query: ') - # params = {'q': onid} - headers = {'Content-Type': 'application/json', - 'Authorization': f'Bearer {access_token}'} + params = {'q': query} + headers = {'Content-Type': 'application/json', + 'Authorization': f'Bearer {access_token}'} - request = requests.get(f'{apiUrl}/{osuId}', headers=headers) - response = request.json() - print(response) + request = requests.get(apiUrl, params=params, headers=headers) + response = request.json() + responseData = response['data'] + if len(responseData) < 1: + print(f'No data found for \"{query}\". ' + + 'Please try a different search query.') return response['data'] @@ -73,7 +78,7 @@ def get_directory(access_token, apiUrl): clientSecret = config['oauth2']['client_secret'] access_token = get_access_token(authUrl, clientId, clientSecret) - personsData = get_person(access_token, personsUrl) + directoryData = get_directory(access_token, directoryUrl) - for person in personsData: - print(f'Name: {person["attributes"]["firstName"]}') + for directory in directoryData: + print(directory['attributes']['firstName']) From 6e4a8ef2c9772fb02beb77d5635b08add3ec2aba Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 13:39:37 -0700 Subject: [PATCH 20/29] added comments to get_directory method --- api_intro.py | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/api_intro.py b/api_intro.py index cb4c9e8..8dc4396 100644 --- a/api_intro.py +++ b/api_intro.py @@ -49,6 +49,11 @@ def get_person(access_token, apiUrl): return responseData +# Requests OSU directory information using api +# requires an access token and api url to be passed in +# asks for search query from user +# returns data if the search query finds some. +# If no data is found the user is asked again to enter a query def get_directory(access_token, apiUrl): responseData = [] while len(responseData) < 1: From 80e8476e30dce09a1254e97dd3fbb9f4c0d97632 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 13:50:04 -0700 Subject: [PATCH 21/29] fixed json import placement since it is part of python system files --- api_intro.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/api_intro.py b/api_intro.py index 8dc4396..839224c 100644 --- a/api_intro.py +++ b/api_intro.py @@ -1,8 +1,8 @@ # api_intro.py # authorize and send requests using OSU's API +import json import sys -import json import requests From 6adbe9d671d13490307a6caab2bad34debd89848 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 13:50:36 -0700 Subject: [PATCH 22/29] started checking for not 200 error codes instead of a particular error message --- api_intro.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/api_intro.py b/api_intro.py index 839224c..1df7fc0 100644 --- a/api_intro.py +++ b/api_intro.py @@ -18,12 +18,12 @@ def get_access_token(authUrl, id, secret): response['access_token'] return response['access_token'] except KeyError: - if response["ErrorCode"] == "invalid_client": - print("Client Id or Client Secret invalid. " - + "Please check your configuration.json " - + "file and try again.") + if request.status_code != 200: + print('Client Id or Client Secret invalid. ' + f'Please check your configuration.json ' + f'file and try again.') else: - print("Unknown error occurred.") + print('Unknown error occurred.') exit() From d4a8123649ccc1684337bff0139d71206ce625a7 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 13:50:51 -0700 Subject: [PATCH 23/29] used f strings instead of concatination --- api_intro.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api_intro.py b/api_intro.py index 1df7fc0..5bac271 100644 --- a/api_intro.py +++ b/api_intro.py @@ -44,7 +44,7 @@ def get_person(access_token, apiUrl): responseData = response['data'] if len(responseData) < 1: print(f'No data found for \"{onid}\". ' - + 'Please try a different search query.') + f'Please try a different search query.') return responseData @@ -68,7 +68,7 @@ def get_directory(access_token, apiUrl): responseData = response['data'] if len(responseData) < 1: print(f'No data found for \"{query}\". ' - + 'Please try a different search query.') + f'Please try a different search query.') return response['data'] From b72a850d6b62a809c5ab0ef6e1c61f9261829554 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 13:54:28 -0700 Subject: [PATCH 24/29] renamed all variables from camel case to snake case --- api_intro.py | 42 +++++++++++++++++++++--------------------- 1 file changed, 21 insertions(+), 21 deletions(-) diff --git a/api_intro.py b/api_intro.py index 5bac271..7de4efc 100644 --- a/api_intro.py +++ b/api_intro.py @@ -30,23 +30,23 @@ def get_access_token(authUrl, id, secret): # Make get request for information about a person at osu # Read in ONID from user # requires access_token retrieved in get_access_token() -def get_person(access_token, apiUrl): - responseData = [] - while len(responseData) < 1: +def get_person(access_token, api_url): + response_data = [] + while len(response_data) < 1: onid = input('Enter Person\'s ONID: ') params = {'onid': onid} headers = {'Content-Type': 'application/json', 'Authorization': f'Bearer {access_token}'} - request = requests.get(apiUrl, params=params, headers=headers) + request = requests.get(api_url, params=params, headers=headers) response = request.json() - responseData = response['data'] - if len(responseData) < 1: + response_data = response['data'] + if len(response_data) < 1: print(f'No data found for \"{onid}\". ' f'Please try a different search query.') - return responseData + return response_data # Requests OSU directory information using api @@ -55,8 +55,8 @@ def get_person(access_token, apiUrl): # returns data if the search query finds some. # If no data is found the user is asked again to enter a query def get_directory(access_token, apiUrl): - responseData = [] - while len(responseData) < 1: + response_data = [] + while len(response_data) < 1: query = input('Enter Directory Search Query: ') params = {'q': query} @@ -65,25 +65,25 @@ def get_directory(access_token, apiUrl): request = requests.get(apiUrl, params=params, headers=headers) response = request.json() - responseData = response['data'] - if len(responseData) < 1: + response_data = response['data'] + if len(response_data) < 1: print(f'No data found for \"{query}\". ' f'Please try a different search query.') return response['data'] if __name__ == '__main__': - configPath = sys.argv[1] - with open(configPath, 'r') as configFile: + config_path = sys.argv[1] + with open(config_path, 'r') as configFile: config = json.load(configFile) - personsUrl = config['api']['persons_url'] - directoryUrl = config['api']['directory_url'] - authUrl = config['oauth2']['auth_api_url'] - clientId = config['oauth2']['client_id'] - clientSecret = config['oauth2']['client_secret'] + persons_url = config['api']['persons_url'] + directory_url = config['api']['directory_url'] + auth_url = config['oauth2']['auth_api_url'] + client_id = config['oauth2']['client_id'] + client_secret = config['oauth2']['client_secret'] - access_token = get_access_token(authUrl, clientId, clientSecret) - directoryData = get_directory(access_token, directoryUrl) + access_token = get_access_token(auth_url, client_id, client_secret) + directory_data = get_directory(access_token, directory_url) - for directory in directoryData: + for directory in directory_data: print(directory['attributes']['firstName']) From 84142a12563ef2abc56b49cf9fc0372d337a137f Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 14:07:24 -0700 Subject: [PATCH 25/29] changed method comment blocks into docstrings --- api_intro.py | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) diff --git a/api_intro.py b/api_intro.py index 7de4efc..17d0f2a 100644 --- a/api_intro.py +++ b/api_intro.py @@ -6,9 +6,10 @@ import requests -# Request access_token from osu api -# Read in consumer key and consumer secret from user def get_access_token(authUrl, id, secret): + """Request access_token from osu api + Read in consumer key and consumer secret from user""" + data = {'client_id': id, 'client_secret': secret, 'grant_type': 'client_credentials'} request = requests.post(authUrl, data=data) @@ -27,12 +28,13 @@ def get_access_token(authUrl, id, secret): exit() -# Make get request for information about a person at osu -# Read in ONID from user -# requires access_token retrieved in get_access_token() def get_person(access_token, api_url): response_data = [] while len(response_data) < 1: + """Make get request for information about a person at osu + Read in ONID from user + requires access_token retrieved in get_access_token()""" + onid = input('Enter Person\'s ONID: ') params = {'onid': onid} @@ -49,14 +51,15 @@ def get_person(access_token, api_url): return response_data -# Requests OSU directory information using api -# requires an access token and api url to be passed in -# asks for search query from user -# returns data if the search query finds some. -# If no data is found the user is asked again to enter a query def get_directory(access_token, apiUrl): response_data = [] while len(response_data) < 1: + """Requests OSU directory information using api + requires an access token and api url to be passed in + asks for search query from user + returns data if the search query finds some. + If no data is found the user is asked again to enter a query""" + query = input('Enter Directory Search Query: ') params = {'q': query} From 0da7e830ac844a28dad578589435ce73f9b4edc6 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 14:07:42 -0700 Subject: [PATCH 26/29] fixed formatting of input loops --- api_intro.py | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/api_intro.py b/api_intro.py index 17d0f2a..13026d6 100644 --- a/api_intro.py +++ b/api_intro.py @@ -29,12 +29,11 @@ def get_access_token(authUrl, id, secret): def get_person(access_token, api_url): - response_data = [] - while len(response_data) < 1: """Make get request for information about a person at osu Read in ONID from user requires access_token retrieved in get_access_token()""" + while True: onid = input('Enter Person\'s ONID: ') params = {'onid': onid} @@ -44,22 +43,21 @@ def get_person(access_token, api_url): request = requests.get(api_url, params=params, headers=headers) response = request.json() response_data = response['data'] - if len(response_data) < 1: + if response_data: + return response_data + else: print(f'No data found for \"{onid}\". ' f'Please try a different search query.') - return response_data - def get_directory(access_token, apiUrl): - response_data = [] - while len(response_data) < 1: """Requests OSU directory information using api requires an access token and api url to be passed in asks for search query from user returns data if the search query finds some. If no data is found the user is asked again to enter a query""" + while True: query = input('Enter Directory Search Query: ') params = {'q': query} @@ -69,10 +67,11 @@ def get_directory(access_token, apiUrl): request = requests.get(apiUrl, params=params, headers=headers) response = request.json() response_data = response['data'] - if len(response_data) < 1: + if response_data: + return response['data'] + else: print(f'No data found for \"{query}\". ' f'Please try a different search query.') - return response['data'] if __name__ == '__main__': From fd0107db2f1f7150123b08a5e8e41b79d3119971 Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 14:12:38 -0700 Subject: [PATCH 27/29] made final string for configuration json objects 'api' and 'oauth2' --- api_intro.py | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/api_intro.py b/api_intro.py index 13026d6..b314f43 100644 --- a/api_intro.py +++ b/api_intro.py @@ -75,14 +75,17 @@ def get_directory(access_token, apiUrl): if __name__ == '__main__': + CONFIG_API = 'api' + CONFIG_OAUTH = 'oauth2' + config_path = sys.argv[1] with open(config_path, 'r') as configFile: config = json.load(configFile) - persons_url = config['api']['persons_url'] - directory_url = config['api']['directory_url'] - auth_url = config['oauth2']['auth_api_url'] - client_id = config['oauth2']['client_id'] - client_secret = config['oauth2']['client_secret'] + persons_url = config[CONFIG_API]['persons_url'] + directory_url = config[CONFIG_API]['directory_url'] + auth_url = config[CONFIG_OAUTH]['auth_api_url'] + client_id = config[CONFIG_OAUTH]['client_id'] + client_secret = config[CONFIG_OAUTH]['client_secret'] access_token = get_access_token(auth_url, client_id, client_secret) directory_data = get_directory(access_token, directory_url) From c4a7950c51bfcb7965ba9a88deb1e13052fb3ffc Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 14:22:49 -0700 Subject: [PATCH 28/29] added method to check if a request has returned with an error --- api_intro.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/api_intro.py b/api_intro.py index b314f43..f41e7da 100644 --- a/api_intro.py +++ b/api_intro.py @@ -74,6 +74,20 @@ def get_directory(access_token, apiUrl): f'Please try a different search query.') +def check_request_errors(request): + """Checks if a request has returned with an error + returns False if the status code is equal to 200, otherwise returns True""" + + error = True + + if request.status_code == 200: + error = False + else: + print(request.json()['userMessage']) + + return error + + if __name__ == '__main__': CONFIG_API = 'api' CONFIG_OAUTH = 'oauth2' From b6a5f97c7983a12a231543273d0e809bf66b647a Mon Sep 17 00:00:00 2001 From: Alex Ruef Date: Tue, 25 Jun 2019 14:23:01 -0700 Subject: [PATCH 29/29] added request error handling to methods --- api_intro.py | 30 ++++++++++++++++-------------- 1 file changed, 16 insertions(+), 14 deletions(-) diff --git a/api_intro.py b/api_intro.py index f41e7da..71fabe5 100644 --- a/api_intro.py +++ b/api_intro.py @@ -41,13 +41,14 @@ def get_person(access_token, api_url): 'Authorization': f'Bearer {access_token}'} request = requests.get(api_url, params=params, headers=headers) - response = request.json() - response_data = response['data'] - if response_data: - return response_data - else: - print(f'No data found for \"{onid}\". ' - f'Please try a different search query.') + if not check_request_errors(request): + response = request.json() + response_data = response['data'] + if response_data: + return response_data + else: + print(f'No data found for \"{onid}\". ' + f'Please try a different search query.') def get_directory(access_token, apiUrl): @@ -65,13 +66,14 @@ def get_directory(access_token, apiUrl): 'Authorization': f'Bearer {access_token}'} request = requests.get(apiUrl, params=params, headers=headers) - response = request.json() - response_data = response['data'] - if response_data: - return response['data'] - else: - print(f'No data found for \"{query}\". ' - f'Please try a different search query.') + if not check_request_errors(request): + response = request.json() + response_data = response['data'] + if response_data: + return response['data'] + else: + print(f'No data found for \"{query}\". ' + f'Please try a different search query.') def check_request_errors(request):