-
Notifications
You must be signed in to change notification settings - Fork 2
API Endpoints refactoring #359
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
Open
blu3eee
wants to merge
10
commits into
dev
Choose a base branch
from
leaf/jack/backend-profiles-endpoints
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
35f26ce
refactor endpoints (#358)
blu3eee 96fecbf
remove template for every other endpoints besides the existing ones (…
blu3eee f502a09
rewrite and add more tests for `/profiles` endpoints
blu3eee 51f90e7
adjust test cases
blu3eee 3518aeb
check buckets function to check and cleanup saved images on test requ…
blu3eee f8f674c
reorganizing imports
blu3eee b8bd5cf
lint
blu3eee 30f740a
pipenv sync with current deps
blu3eee d2ddada
docs: backend CONTRIBUTING.md
blu3eee a95f4a4
refactor: use include to remove redundancy
blu3eee File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,114 @@ | ||
| # Contributing to Let's Mesh Backend (API) | ||
|
|
||
| Thank you for considering contributing to Let's Mesh! Here are some guidelines to help you get started. | ||
|
|
||
| ## Table of Contents | ||
|
|
||
| 1. [Issue Tracking and Management](#issue-tracking-and-management) | ||
| 2. [Backend Structure](#backend-structure) | ||
| 3. [Writing tests](#writing-tests) | ||
| 4. [Communication and Support](#communication-and-support) | ||
|
|
||
| ## Issue Tracking and Management | ||
|
|
||
| We use GitHub Projects to manage and track issues for the Let's Mesh project. You can find our [project board here](https://github.com/orgs/LetsMesh/projects/2). | ||
|
|
||
| ### How to Report an Issue | ||
|
|
||
| 1. **Check Existing Issues**: Before creating a new issue (bug/task), please check if the issue has already been reported to avoid duplications. | ||
| 2. **Open a New Issue**: If the issue does not exist, open a new issue and provide as much detail as possible. For bug reporting, please provide the steps to reproduce the bug. | ||
| 3. **Link to Project Board**: Ensure your issue is linked to the project board for better tracking. | ||
|
|
||
| ### How to Work on an Issue | ||
|
|
||
| 1. **Assign Yourself**: If you are interested in working on an issue, assign it to yourself. | ||
| 2. **Create a Branch**: Create a new branch from `dev` with a descriptive name related to the issue. | ||
| 3. **Submit a Pull Request**: Once you have completed your work, submit a pull request (PR) and link it to the issue. | ||
|
|
||
| ## Backend Structure | ||
|
|
||
| Our backend is structured to support both HTTP API requests and WebSocket connections using Django's ASGI and WSGI capabilities. Here’s an overview of our project structure: | ||
|
|
||
| - **`mesh/`**: backend | ||
| - **`helpers/`**: Custom middlewares & helpers for request/response processing. | ||
| - **`utils/`**: Utility functions. | ||
| - **`exceptions/`**: Custom exception handling. | ||
| - **`tests/`**: Test cases for API and WebSocket endpoints. | ||
| - **`asgi.py`**: ASGI configuration for handling asynchronous requests. | ||
| - **`routing.py`**: Routing configuration for WebSocket connections. | ||
| - **`settings.py`**: Django project settings. | ||
| - **`urls.py`**: URL routing configuration. | ||
| - **`wsgi.py`**: WSGI configuration for handling synchronous requests. | ||
| - Other folders (`accounts/`, `accountSettings/`, `auth`, `profiles/`, etc.): Each folder contains its own URLs and models to handle related operations. | ||
| - **`meshapp/`**: frontend | ||
|
|
||
| ### Getting Started | ||
|
|
||
| 1. **Determine if you need a new API/app**: If an API already exists for your use case, continue development there. Otherwise, create a new app. | ||
| 2. **Create a new app**: Ensure you are in the `Site/mesh` directory and run `django-admin startapp api_name`. | ||
| 3. **Add `urls.py` to the new app**: Follow the example in `Site/mesh/`urls.py`` to add URLs and create a new API. | ||
|
|
||
| ### Learn by Example | ||
|
|
||
| Explore the example available in the project: | ||
|
|
||
| - **Files to Check**: | ||
|
|
||
| - `Site/mesh/exampleapi/`urls.py`` | ||
| - `Site/mesh/exampleapi/`views.py`` | ||
| - `Site/mesh/`urls.py`` | ||
|
|
||
| - **Steps to Test**: | ||
| 1. Run `python `manage.py` runserver` in `Site/`. | ||
| 2. Open a browser and go to [http://127.0.0.1:8000/example/](http://127.0.0.1:8000/example/). You should see "You Got Home". | ||
| 3. Check the `index` function in `Site/mesh/exampleapi/`views.py``. | ||
| 4. Check the URL definitions in `Site/mesh/`urls.py`` and `Site/mesh/exampleapi/`urls.py``. | ||
|
|
||
| Visit [http://127.0.0.1:8000/example/helloworld/](http://127.0.0.1:8000/example/helloworld/) to see the "Hello World" example. | ||
|
|
||
| ## Writing Tests | ||
|
|
||
| Here are some guidelines for writing tests for our API endpoints in Django: | ||
|
|
||
| - Our Django setup has `APPEND_SLASH=true` by default, which appends a trailing slash to URLs and permanently redirects requests. | ||
|
|
||
| ### Incorrect Way to Send Test Requests (without trailing slash): | ||
|
|
||
| ```python | ||
| from django.test import TestCase, Client | ||
|
|
||
| class YourTestClass(TestCase): | ||
| def setUp(self): | ||
| self.client = Client() | ||
|
|
||
| def your_test_case(self): | ||
| response = self.client.get("/accounts") | ||
| self.assertEqual(response.status_code, 200) # This will not pass ❌ | ||
| ``` | ||
|
|
||
| This will fail as `response.status_code` is 301 instead of 200, due to the permanent redirect. | ||
|
|
||
| ### Correct Way to Send Test Requests: | ||
|
|
||
| ```python | ||
| from django.test import TestCase, Client | ||
|
|
||
| class YourTestClass(TestCase): | ||
| def setUp(self): | ||
| self.client = Client() | ||
|
|
||
| def your_test_case(self): | ||
| response = self.client.get("/accounts", follow=True) | ||
| self.assertEqual(response.status_code, 200) # This will pass ✅ | ||
|
|
||
| response = self.client.get("/accounts/") | ||
| self.assertEqual(response.status_code, 200) # This will pass ✅ | ||
| ``` | ||
|
|
||
| ## Communication and Support | ||
|
|
||
| For easier communication and support, we invite all contributors to join our Discord server. Our project Discord server is beginner-friendly and a great place to ask questions, get feedback, and stay updated on the latest project developments. [Join our Discord server here](https://discord.gg/eUDKr8u55u). | ||
|
|
||
| We appreciate your contributions and look forward to collaborating with you on Let's Mesh! | ||
|
|
||
| --- |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just curious... wasn't the
validate_json_and_required_fieldsmade for this or is this one a special case that requires us to not use the util function?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tdlr:
validate_json_and_required_fieldswas not made for this. the two use cases are different.validate_json_and_required_fieldsis to validate the request body. It takes in ajsonobject, check if the request body has the required fields for the action (i.e. creating account will need enough information) and returns an indicator saying if the request body is valid for the action.The fields checking in this particular case is getting the
fieldsparam from the request, then parse it into an array and double-check it with valid fields to see if the requested fields are matched with return object available fields. (Kindof mimickinggraphQLbehavior in a sense)twoFactorEnabled,theme, andnotificationSetting. Thefieldsin the request body will indicate how much information the backend is returning, in some cases, we only need partial information. So, instead of returning the whole accountSetting object from the database, we only return the requested fields. If in the request body,fields="theme,twoFactorEnabled", only those information is returned in the response.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay that makes sense, is there no need to split this into its own function as well or does this seem like it'd be a one-off operation?