Python restful API design

WYH grass sample 2021-02-22 11:21:19
python restful api design


What is? REST?

Six design specifications define a REST Characteristics of the system :

  • client - The server : Isolation between client and server , Server provides services , Client consumption .
  • No state : Every request from the client to the server must contain the information necessary to understand the request . let me put it another way , The server will not store the information requested by the client for the next use .
  • Caching : The server must indicate whether client requests can be cached .
  • Layered system : The communication between client and server should be in a standard way , When the middle tier responds instead of the server , The client doesn't need to make any changes .
  • Unified interface : The communication method between server and client must be unified .
  • On demand coding : The server can provide executable code or scripts , For clients to execute in their environment . This constraint is the only one that is optional .
What is a RESTful Of web service?

REST The original purpose of architecture is to adapt to the world wide web HTTP agreement .
RESTful web services The core of the concept is “ resources ”. Resources can be used URI To express . Client side usage HTTP Protocol defined methods to send requests to these URIs, Of course, it may lead to these being visited ” resources “ A change of state .

HTTP The standard methods are as follows :

========== ===================== ==================================
HTTP Method Behavior Example
========== ===================== ==================================
GET Get information about resources http://example.com/api/orders
GET Get information about a particular resource http://example.com/api/orders/123
POST Create new resources http://example.com/api/orders
PUT Update resources http://example.com/api/orders/123
DELETE Delete resources http://example.com/api/orders/123
========== ====================== ==================================

REST Design doesn't require a specific data format . In the request, the data can be JSON form , Or sometimes as url Query parameter items in .

Design a simple web service

insist REST Design a web service perhaps API The task becomes an exercise of identifying resources to be displayed and how they are affected by different request methods .
for instance , We're going to write a to-do application and we want to design one for it web service. The first thing to do is to decide what kind of root to use URL To access the service . for example , We can visit through this :http://[hostname]/todo/api/v1.0/
Here I've decided to URL Contains the name of the app and API Version number of . stay URL The inclusion of application names in helps to provide a namespace that distinguishes other services on the same system . stay URL Including the version number in can help with future updates , If there are new and potentially incompatible features in the new version , It doesn't affect applications that rely on older features .

The next step is to choose which one will be exposed by the service ( Exhibition ) Resources for . This is a very simple application , We have a mission , So the only resource in our to-do list is the task .
Our mission resources are going to use HTTP The method is as follows :

========== =============================================== =============================
HTTP Method URL action
========== =============================================== ==============================
GET http://[hostname]/todo/api/v1.0/tasks Retrieve the task list
GET http://[hostname]/todo/api/v1.0/tasks/[task_id] Retrieve a task
POST http://[hostname]/todo/api/v1.0/tasks Create a new task
PUT http://[hostname]/todo/api/v1.0/tasks/[task_id] Update task
DELETE http://[hostname]/todo/api/v1.0/tasks/[task_id] Delete task
========== ================================================ =============================

The tasks we define have the following properties :

  • id: Unique identifier of the task . Numeric type .
  • title: A brief description of the task . String type .
  • description: Specific task description . Text type .
  • done: The state of mission completion . Boolean value .

So far about us web service The design is basically completed . The rest is to achieve it !

Use Python and Flask Realization RESTful services

Falsk Large project tutorials
Python Chinese learning base

版权声明
本文为[WYH grass sample]所创,转载请带上原文链接,感谢
https://pythonmana.com/2021/02/20210221115743670S.html

  1. Detailed usage of urllib in Python 3 (header, proxy, timeout, authentication, exception handling)
  2. python 第三方库paramiko
  3. python 第三方库paramiko
  4. Python third party library paramiko
  5. Python third party library paramiko
  6. 卸载 PyCharm!这才是 Python 小白的最理想的 IDE
  7. 卸载 PyCharm!这才是 Python 小白的最理想的 IDE
  8. Uninstall pycharm! This is the ideal IDE for Python Xiaobai
  9. django学习-27.admin管理后台里:对列表展示页面的数据展示进行相关优化
  10. Uninstall pycharm! This is the ideal IDE for Python Xiaobai
  11. Django learning - 27. Admin management background: optimize the data display of the list display page
  12. python day2
  13. python day2
  14. Python 内存泄漏问题排查
  15. Troubleshooting of Python memory leak
  16. Python 与 excel的简单应用
  17. Simple application of Python and excel
  18. Python 与 excel的简单应用
  19. Simple application of Python and excel
  20. 2.7万 Star!最全面的 Python 设计模式集合
  21. 27000 stars! The most comprehensive collection of Python design patterns
  22. python day3
  23. python day3
  24. Commonly used data operation functions of Python
  25. (数据科学学习手札108)Python+Dash快速web应用开发——静态部件篇(上)
  26. (learning notes of data science 108) Python + dash rapid web application development -- static components (I)
  27. (数据科学学习手札108)Python+Dash快速web应用开发——静态部件篇(上)
  28. (learning notes of data science 108) Python + dash rapid web application development -- static components (I)
  29. [Python] Matplotlib 图表的绘制和美化技巧
  30. Drawing and beautifying skills of [Python] Matplotlib chart
  31. [Python] Matplotlib 图表的绘制和美化技巧
  32. Drawing and beautifying skills of [Python] Matplotlib chart
  33. Virtual environment of Python project
  34. 翻译:《实用的Python编程》02_01_Datatypes
  35. Translation: practical Python Programming 02_ 01_ Datatypes
  36. 翻译:《实用的Python编程》02_01_Datatypes
  37. 翻译:《实用的Python编程》02_01_Datatypes
  38. Translation: practical Python Programming 02_ 01_ Datatypes
  39. Translation: practical Python Programming 02_ 01_ Datatypes
  40. Python 3 入门,看这篇就够了
  41. Python 3 entry, see this is enough
  42. 华为大佬打造的400集Python视频学起来,学完万物皆可爬
  43. 400 episodes of Python video created by Huawei boss
  44. django之csrf_exempt解决跨域请求的问题
  45. CSRF of Django_ Exempt solves the problem of cross domain requests
  46. 1.7 万 Star!一个简单实用的 Python 进度条库
  47. 17000 stars! A simple and practical Python progress bar library
  48. Python爬虫:设置Cookie解决网站拦截并爬取蚂蚁短租
  49. Python crawler: setting cookie to solve website interception and crawling ant short rent
  50. Python-Net编程
  51. Python net programming
  52. 学习Python数学英语基础重要吗?Python教程!
  53. Is it important to learn the basics of math and English in Python!
  54. Python数据分析常用库有哪些?Python学习!
  55. What are the common libraries for Python data analysis? Learn Python!
  56. win 创建python虚拟环境
  57. Creating Python virtual environment with win
  58. In order to automatically collect B station barrage, I developed a tool in Python
  59. 用Python编程语言来实现阿姆斯特朗数的检查
  60. Using python programming language to check Armstrong number