Skip to content
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

Add new Origin Trials API #3214

Merged
merged 29 commits into from
Jul 31, 2023
Merged

Add new Origin Trials API #3214

merged 29 commits into from
Jul 31, 2023

Conversation

DanielRyanSmith
Copy link
Collaborator

This PR adds the new Origin Trials API that will be used to interact with the OT API on the server side. A new endpoint is also created to query the OT API for origin trial data.

DanielRyanSmith and others added 2 commits July 25, 2023 12:02
Co-authored-by: James C Scott III <[email protected]>
api/origin_trials_api.py Outdated Show resolved Hide resolved
api/origin_trials_api.py Outdated Show resolved Hide resolved
api/origin_trials_api.py Outdated Show resolved Hide resolved
api/origin_trials_api.py Outdated Show resolved Hide resolved
api/origin_trials_api.py Outdated Show resolved Hide resolved
Copy link
Collaborator

@jcscottiii jcscottiii left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The frameworks/origin_trials_client.py should not have knowledge of the type of http status codes or message to return to the frontend. Instead of returning a tuple of value and error, you need to raise an error.

https://google.github.io/styleguide/pyguide.html#244-decision

Then in api/origin_trials_api.py, you can do things like:

try:
  trials_list = origin_trials_client.get_trials_list()
except ExceptionType1:
  self.abort(500, 'Error obtaining origin trial data from API')
except ExceptionType2:
  self.abort(500, 'Malformed response from origin trials API')

Regarding creating your own Exception types vs re-raising existing ones: I'm okay re-raising the existing ones for now. We can always refactor that later on.

internals/data_types.py Outdated Show resolved Hide resolved
internals/data_types.py Show resolved Hide resolved
internals/data_types.py Show resolved Hide resolved
f'{settings.OT_API_URL}/v1/trials',
params={'prettyPrint': 'false', 'key': key})
response.raise_for_status()
except requests.exceptions.HTTPError:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should either re-raise this exception or create your own exception like class FailedToGetDataError(requests.exceptions.HTTPError)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I want to make sure I'm doing this right - should I not be passing the error info in the return value and instead be returning an actual error? Or am I raising the error in the get_trials_list() function and then catching it in the origin_trials_api.do_get() to raise it again there?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll do this part Or am I raising the error in the get_trials_list() function and then catching it in the origin_trials_api.do_get().

Then in do_get(), it will catch that specific exception and call the right abort:

try:
  trials_list = origin_trials_client.get_trials_list()
except ExceptionType1: # the http one
  self.abort(500, 'Error obtaining origin trial data from API')
except ExceptionType2: # The valueerror one
  self.abort(500, 'Malformed response from origin trials API')


response_json = response.json()
if 'trials' not in response_json:
return [], (500, 'Malformed response from origin trials API')
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same as above. But the error could be ValueError or a new class error based on ValueError.

framework/origin_trials_client.py Outdated Show resolved Hide resolved
framework/origin_trials_client.py Outdated Show resolved Hide resolved
framework/origin_trials_client.py Outdated Show resolved Hide resolved
if key == None:
return [], None

response = requests.get(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should still have the try-except for the request.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I wasn't sure if I was supposed to leave the try/except block in here along with the one in the origin_trials_api function

framework/origin_trials_client.py Outdated Show resolved Hide resolved
Comment on lines 26 to 27
# Data type for information if an error occurred. Error code + message.
ERROR_INFO_TYPE = tuple[int, str]|None
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Data type for information if an error occurred. Error code + message.
ERROR_INFO_TYPE = tuple[int, str]|None

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, I remember removing the code for the error returns and the type hinting, but it doesn't show up here. It might have been part of an abandoned change. Either way, I should have caught it before sending it over for re-review!

framework/origin_trials_client.py Outdated Show resolved Hide resolved
framework/origin_trials_client.py Outdated Show resolved Hide resolved
framework/origin_trials_client.py Outdated Show resolved Hide resolved
framework/origin_trials_client.py Show resolved Hide resolved
api/origin_trials_api.py Outdated Show resolved Hide resolved
Base automatically changed from 2023-07-25_ot-api-key to main July 31, 2023 22:14
@DanielRyanSmith DanielRyanSmith merged commit 88972b5 into main Jul 31, 2023
3 checks passed
@DanielRyanSmith DanielRyanSmith deleted the 2023-07-25_add-ot-endpoint branch July 31, 2023 22:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants