revisiting decorators for actions/properties/etc.
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Cần làm rõ
- Mức độ hoạt động
- Đình trệ
- Lĩnh vực
- api, backend-api-design
Hướng nghiên cứu
Xem xét các pattern hiện có của ActionView và PropertyView, việc đăng ký Extension và hành vi của APISpec được mô tả trong issue. Trước tiên, xác định liệu các hàm và decorator action/property tường minh có nên thay thế pattern wrapper hiện tại hay không; công việc được xem là hoàn tất khi có một thiết kế đã được thống nhất cho việc truy cập HTTP/Python, tái sử dụng tham số và docstring, cùng các ánh xạ phương thức WoT mặc định.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
I am sure we have discussed in the past the merits of defining a View subclass for each endpoint, e.g. in an Extension. I have a nasty feeling that I probably argued strongly against using decorators because they often make life quite confusing. However, I'm coming round to the idea. My use case is this:
I'm writing some code in an extension, which includes a function to do something. For argument's sake let's say that function should be an Action but I think the same consideration applies to properties. Ideally, I'd like it to be accessible either via HTTP or via Python. Currently, what I do is:
- Write the Python function that does what I want (and give it a helpful docstring) as a method of my Extension
- Define another class wrapping that Python function in a way that can be called nicely from the API
- Reference said class from within the Extension class so it appears in the AP
There are several things that are less than ideal here:
- You end up defining your arguments in multiple places - specifically, in both the method and the View class.
- Docstrings don't propagate from the method to the View, so the HTTP API docs are not great
I can see a few ways to make it better:
- Be more explicit about the fact that
ActionViewclasses are more or less always wrapping a function to do an action. This would, for example, mean that instead of writing apostmethod, I would supply anactionfunction. Doing that explicitly makes it way easier to wrap the action function nicely, e.g. propagate the docstrings. - Provide a decorator to put the definition of the
ActionViewright next to the function it's wrapping. This makes it harder to miss things being out of sync, and could also eliminate the need to register views explicitly.
I think if I argued against custom decorators in the past, it was because I felt they were non-standard ways of doing things, and wouldn't make sense to someone familiar with Flask. That's true, but I think there's enough that's non-standard about e.g. the way we generate our APISpec that this battle is possibly lost anyway. Also, if we define a more "helpful" ActionView subclass, it potentially provides a point of familiarity for Flask developers - so if they understand how @action maps onto ActionViewWrapper (which is potentially quite simple), it's easier to figure out what happens once you have a View.
Also, I think when we made this decision, it was more likely that you would want control over the get/post/delete/whatever methods. Now that the WoT specification is finalised, it's pretty well defined what each HTTP method should do, and I'm in favour of providing a mapping from HTTP methods to meaningful WoT interactions. I.e. a PropertyView always reads a property with GET and writes it with PUT and may define a way to update it with POST. So probably we should, by default, define a getter, setter, and update method - not all of which must exist. Similarly, an ActionView will always run actions with POST and return status with GET, so it makes sense to simply have a function responsible for running the action (which is what's currently implemented in ActionView.post. This also means that overriding ActionView.post would allow an action to do something custom before it's spawned in a background thread, such as validating arguments and returning an HTTP error code if they're wrong.
I'm raising this issue so @jtc42 can tell me why I was dead against this in the past, or if he remembers anything important that's not reflected in the discussion above.
- Ngôn ngữ chính
- Python
- Star
- 19
- Fork
- 2
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Hướng dẫn đóng góp
Chưa lập chỉ mục được hướng dẫn đóng góp cho kho mã nguồn này
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của labthings/python-labthings
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
labthings/python-labthings#324 ·
-
Updating dependencies... Đang mở
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 20/100
labthings/python-labthings#323 · 3 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 15/100
labthings/python-labthings#302 · 2 bình luận ·
-
Độ khó 5/5 Hơn một tuần Mức phù hợp với người mới 30/100
labthings/python-labthings#300 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 35/100
Tất cả issue của labthings/python-labthings
Issue tương tự
-
enhancement
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
canonical/paas-charm#368 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
tech debt
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 75/100
-
addition to tracking list Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
StevenBlack/hosts#3256 ·
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
qualcomm/qai-appbuilder#275 ·