Giter Club home page Giter Club logo

drf-base's Introduction

Tech Stack

Sources

Core

Github

Samples

Best Practices

  • Use Django & DRF Business Logic Layer (Services) to test better
  • Use Layers Presentation Layer (Views), Business Logic Layer (Services), Data Access Layer (Models)
  • Use Custom Response Model
  • Client-Server Architecture
  • Ensure that the API scales
  • Use an international design standard The OpenAPI v3
  • Cacheable -> Caching Responses -> Cache frequently requested API responses to improve performance and reduce the load on your server. DRF supports various caching strategies that can be easily integrated.
  • Stateless
  • Use Nouns Instead of Verbs in Endpoints -> https://mysite.com/posts not https://mysite.com/createPost
  • Don't use POST: /articles/createNewArticle/ Do use POST: /articles/
  • Name Collections with Plural Nouns -> So, instead of https://mysite.com/post/123, it should be https://mysite.com/posts/123, GET /cars/123, POST /cars, GET /cars
  • Use Status Codes in Error Handling -> Informational Responses, Redirects, Client-side errors, Server-side errors
  • Use Nesting on Endpoints to Show Relationships and Nested Serializers and Related Data -> https://mysite.com/posts/postId/comments, You should avoid nesting that is more than 3 levels deep as this can make the API less elegant and readable
    • /users // list all users
    • /users/123 // specific user
    • /users/123/orders // list of orders that belong to a specific user
    • /users/123/orders/0001 // specific order of a specific users order list
  • Use Filtering, Sorting, and Pagination to Retrieve the Data Requested -> https://mysite.com/posts?sortBy=createdAt&sortOrder=desc&limit=10&offset=0
  • Use SSL for Security -> https://mysite.com/posts
  • Return Error Details in the Response Body -> { "error": "Invalid payload.", "detail": { "surname": "This field is required." } }
  • Provide Accurate API Documentation -> Documentation with Swagger and API Docs for API Consumers
    • The documentation should contain:
      • relevant endpoints of the API
      • example requests of the endpoints
      • implementation in several programming languages
      • messages listed for different errors with their status codes
  • Tests should cover all API endpoints, be sure to use mock to mock external API calls, Be sure to include tests that cover all possible error conditions, Write comprehensive unit tests using DRF’s testing tools to validate the functionality of your API endpoints. Test-driven development (TDD) ensures a robust and bug-free API.
  • Check that valid data is returned for 201 or 200 responses, make sure the proper error codes/messages are being returned for 4xx responses
  • Use exception handling and custom response model
  • Response message with status codes { ‘status’:’success|error’, ‘data’:{ 'result':{} || [] , '' }, 200 OK — Success — GET/PUT — return resource/status message 201 Created — Success — POST — provide status message or return newly created object 204 No Content — Success — DELETE 304 Unchanged — Redirect — ALL — Indicates no changes since last request 400 Bad Request — Failure — GET/PUT/POST — invalid request, return error messages 401 Unauthorized — Failure — ALL — missing credentials/Authentication required 403 Forbidden — Failure — ALL — restricted content 404 Not Found — Failure — Resource not found 405 Method Not Allowed Failure — Failure — ALL — An invalid HTTP method was attempted
  • Versioning Your APIs -> https://mysite.com/v2 for version 2, Implement API versioning from the beginning to ensure backward compatibility as your API evolves. DRF provides easy-to-use tools for versioning, allowing you to handle changes gracefully.
  • Validation and Error Handling -> DRF provides comprehensive validation tools to ensure data integrity. Handle errors gracefully and provide meaningful error responses to API consumers.
  • Optimizing Database Queries -> Avoid the N+1 query problem by using DRF’s queryset optimization techniques like select_related and prefetch_related to minimize database queries.
  • Use UUIDS for Primary Keys -> Use UUIDs instead of auto-incrementing integers for primary keys to avoid exposing internal IDs to API consumers.

Business Logic Layer (Services)

Structure

myproject_website/ ├── commands/ ├── db_backups/ ├── mockups/ ├── src/ │ └── django-myproject/ │ ├── externals/ │ │ ├── apps/ │ │ │ └── README.md │ │ └── libs/ │ │ └── README.md │ ├── locale/ │ ├── media/ │ ├── myproject/ │ │ ├── apps/ │ │ │ ├── core/ │ │ │ │ ├── init.py │ │ │ │ └── versioning.py │ │ │ └── init.py │ │ ├── settings/ │ │ │ ├── init.py │ │ │ ├── _base.py │ │ │ ├── dev.py │ │ │ ├── production.py │ │ │ ├── sample_secrets.json │ │ │ ├── secrets.json │ │ │ ├── staging.py │ │ │ └── test.py │ │ ├── site_static/ │ │ │ └── site/ │ │ │ django-admin.py startproject myproject ├── css/ │ │ │ │ └── style.css │ │ │ ├── img/ │ │ │ │ ├── favicon-16x16.png │ │ │ │ ├── favicon-32x32.png │ │ │ │ └── favicon.ico │ │ │ ├── js/ │ │ │ │ └── main.js │ │ │ └── scss/ │ │ │ └── style.scss │ │ ├── templates/ │ │ │ ├── base.html │ │ │ └── index.html │ │ ├── init.py │ │ ├── urls.py │ │ └── wsgi.py │ ├── requirements/ │ │ ├── _base.txt │ │ ├── dev.txt │ │ ├── production.txt │ │ ├── staging.txt │ │ └── test.txt │ ├── static/ │ ├── LICENSE │ └── manage.py └── env/

TODOs

  • //TODO

drf-base's People

Contributors

mustafaakgul avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.