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

Draft: Implement new API routes to fetch blog posts by authorIds and to update a post #2

Open
wants to merge 79 commits into
base: main
Choose a base branch
from

Conversation

huaszu
Copy link
Owner

@huaszu huaszu commented Apr 12, 2023

#1

… the authors specified in the authorIds parameter and returns these posts in the JSON response
api/posts.py Outdated Show resolved Hide resolved
api/posts.py Outdated Show resolved Hide resolved
huaszu and others added 20 commits April 13, 2023 00:02
Correct jsonify() argument and return 200 success code

Co-authored-by: Jan B <janhenrikbern@users.noreply.github.com>
… information to match specification for JSON Response Body.
…lp subsequent data manipulation. Implement sorting of blog posts.
…hen value for query parameter is in incorrect format. Create global variable for options on which we can sort posts.
…h introducing reverse_boolean variable closer to where we use it. Separate the error checking and the assigning of reverse_boolean.
…when request asks for posts of author id(s) that are not in the user table in the database. Handle case when the GET request is made correctly and there are no posts to return.
@huaszu
Copy link
Owner Author

huaszu commented Apr 16, 2023

I believe /tests/test_posts.py has one test, test_get_posts, that tests what I have written so far.  It passed. 

I had not used type hints before you mentioned them, @janhenrikbern .  Helped me to update variable names where I was previously indicating the type in the name to help myself remember. 

Here are several areas in which I have doubts: 

Efficiency

"Sort the blog posts based on the provided query parameters" and "remove any duplicate blog posts (try to be efficient when doing this)" from the issue

  • What efficiency improvements can I make? 

Error handling

Thanks @hdenisenko for emphasizing error checking in the past.  I restructured my code significantly after thinking through errors, which I was not expecting!  In a few cases where different errors could look the same to the user, I wanted to separate where possible to provide clarity.

  • What edge cases have I missed? 
  • If someone includes a query parameter we have not planned for, it seems the request would carry on with the rest of the parameters provided, if any.  I am not doing anything for this case right now and it seems a nice-to-have to let the user know their excess parameter is irrelevant. Screenshot:

Screenshot 2023-04-15 at 9 25 28 PM

  • I don't know what the REST best practices are for status codes and implemented based on what I read online.  I use 400 a bunch of times. Here is an example when I use 200 with an error message because it's not that the user made the request incorrectly but that certain data does not show up in the db: return jsonify({"error": "None of the author id(s) you requested exist in the database."}), 200 
  • Verify that the user is allowed to perform this action - if the user is not, I suppose I can give a 401 with a message.  Right now I don't know how to log out in my terminal  🤔.  I couldn't remind myself what currently happens when not logged in.  I opened a new terminal window, did not provide an x-access-token header in the request, and was able to get the same responses as in the window I have been testing in.  Haha possibly if I wait, my current token will just expire.  Tangentially, a strange observation is that when I removed the last character from the token as a test, my request still went through and gave a response providing the information.  I looked at Flask documentation and don't understand how @auth_required works yet. 

Organization

  • Should I put this function in another file and import the function to /api/posts.py?  Advice on how to name that file? 
    def sort_posts_on(item):
        return item[1][sort_by_input]
  • I made a new file crud.py that currently has one function used in /api/posts.py.  Should I name or organize that file in the directory structure better? 

I don't know how code reviews would work on a real team so you tell me and look at what interests you?

api/posts.py Outdated Show resolved Hide resolved
api/posts.py Outdated Show resolved Hide resolved
api/posts.py Outdated Show resolved Hide resolved
api/posts.py Outdated Show resolved Hide resolved
api/posts.py Outdated Show resolved Hide resolved
api/posts.py Outdated
author_id = int(author_id_input)
if crud.check_user_exists(author_id):
author_ids.add(author_id)
except:
Copy link
Collaborator

Choose a reason for hiding this comment

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

i'm not sure what in your try would hit the except - does crud.check_user_exists return an error?

Copy link
Owner Author

@huaszu huaszu Apr 17, 2023

Choose a reason for hiding this comment

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

no, crud.check_user_exists does not return an error. if the user does not exist, the db query in crudwould return None per my understanding and crud.check_user_exists would return False. thanks for the question! i had to remind myself what error i was checking.

i want to hit the except when the request has an invalid authorIds query parameter value, for instance when a character other than "," is used to separate ids or when ids are not typed as numbers (see screenshot). i don't know whether testing for an error in author_ids.split(",") is a great translation of that intent.

Screenshot 2023-04-17 at 3 31 56 AM

Copy link
Owner Author

Choose a reason for hiding this comment

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

an update: crud.check_user_exists can return an error if int(author_id) returns an error, e.g., in the case of the bottom request in the screenshot. whether author_id can be converted to an integer is also part of checking for intended behavior

Copy link
Owner Author

Choose a reason for hiding this comment

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

i suppose we could be more specific to the requester whether the error was that the query parameter value had no integers at all (e.g., "all"), or had integers but incorrectly separated (e.g., "1+2"), or so on

@huaszu huaszu changed the title Draft: Implement new API route to fetch blog posts by authorIds Draft: Implement new API routes to fetch blog posts by authorIds and to update a post May 4, 2023
sort_by=sort_by,
direction=direction)

return jsonify({"posts": result}), 200
Copy link
Collaborator

Choose a reason for hiding this comment

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

I'd recommend returning a flask response object here instead of 2 parameters: https://tedboy.github.io/flask/generated/generated/flask.Response.html

api/posts.py Outdated Show resolved Hide resolved
api/posts.py Outdated Show resolved Hide resolved
db/models/post.py Outdated Show resolved Hide resolved
api/posts.py Outdated Show resolved Hide resolved
huaszu added 15 commits June 25, 2023 19:08
…pers_to_fetch_posts.py is part of the service layer, calls that function, and applies business logic. In db/models/post.py, make code less repetitive and more readable by pulling out direction == desc into its own variable.
…ory layer that contains functions that interact with the database. Improve factoring of code into functions for extensibility, maintability, and readability. Remove print statements from debugging. Make code more concise. Decrease repetitive code. Write comments where helpful. Pass tests.
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