Video demo: API Request Logger
### Ghi nhật ký API Request của Viindoo
Giải pháp toàn diện để quản lý và theo dõi các yêu cầu API trong Odoo.
Ghi nhật ký API Request của Viindoo cung cấp cho các lập trình viên một hệ thống tập trung để ghi lại và phân tích các tương tác API, đảm bảo hiệu quả kiểm tra lỗi, tuân thủ quy định và cung cấp thông tin chi tiết về tích hợp với các dịch vụ bên ngoài.
#### Các tính năng nổi bật:
- Tự động ghi lại tất cả các yêu cầu API và phản hồi, bao gồm tham số, kết quả và thời gian thực thi.
- Theo dõi hoạt động người dùng và thông tin về mô hình hoặc bản ghi khởi tạo yêu cầu.
- Duy trì nhật ký phục vụ kiểm tra lỗi, phân tích hiệu suất và audit.
- Dễ dàng tích hợp với bất kỳ module Odoo nào thông qua kiến trúc mixin.
#### Dành cho:
- Lập trình viên Odoo thường xuyên làm việc với các tích hợp API bên ngoài.
- Doanh nghiệp yêu cầu khả năng truy vết các yêu cầu API để tuân thủ và kiểm tra lỗi.
- Các nhóm quản lý tích hợp hệ thống phức tạp.
#### Cách thức hoạt động:
1. Tích hợp: Thêm viin_api_request_logger vào depends của module của bạn.
2. Sử dụng: Kế thừa api.request.mixin trong các mô hình của bạn để kích hoạt tính năng ghi nhật ký tự động.
3. Phân tích: Truy cập dữ liệu nhật ký từ giao diện để kiểm tra lỗi hoặc audit.
Truy cập [tài liệu hướng dẫn](https://viindoo.com) để biết thêm chi tiết về triển khai.
Ấn bản được Hỗ trợ
- Ấn bản Community
- Ấn bản Enterprise
API Request Logger Documentation
Overview
The viin_api_request_logger module provides a framework for logging and monitoring API requests in Odoo. Designed for developers and administrators, it tracks all details of API interactions, including:
- User initiating the request
- Related model and record
- Request parameters, headers, and payload
- API response data and errors
- Request duration
Key Features
- Detailed Logging: Capture all request/response details, including errors.
- Error Monitoring: Automatically logs failed API requests for better debugging.
- Extensible Integration: Easily integrate with models using the provided mixins.
- User-Friendly API Log Viewer: Quickly access logs related to any model record.
- Customizable: Fully adaptable for specific business needs.
Installation
Install the module via the Odoo Apps interface or the command line:
odoo-bin -c <config_file> -i viin_api_request_logger
Configure user access rights to ensure secure access to logs.
Usage
Logging API Requests
To enable automatic logging for API requests, inherit the api.request.mixin in your model and use the make_request method. Here's an example:
class ExampleIntegration(models.AbstractModel):
_name = 'example.integration'
_inherit = 'api.request.mixin'
def example_api_call(self):
response = self.make_request(
method="GET",
url="https://api.example.com/data",
headers={"Authorization": "Bearer <token>"},
params={"key": "value"},
res_model="example.model",
res_id=1,
)
return response.json()
Viewing API Logs
To allow users to view related logs, inherit the api.log.viewer.mixin in your model. This mixin provides:
- `last_api_request_log_id`: A computed field showing the most recent API log.
- `action_view_api_logs`: An action to display all related logs.
Example:
class ExampleModel(models.Model):
_name = 'example.model'
_inherit = 'api.log.viewer.mixin'
Logs can also be accessed via the "API Logs" menu in the Odoo interface.
Customizing the Logger
Override _prepare_log_values to include custom fields in the log:
def _prepare_log_values(self, method, url, headers, data, response, duration, res_model=None, res_id=None, error_message=None):
values = super()._prepare_log_values(method, url, headers, data, response, duration, res_model, res_id, error_message)
values.update({"custom_field": "Custom Value"})
return values
Configuration
The module works out-of-the-box but can be extended for advanced configurations.
Advanced Scenarios
- Custom Headers: Use the headers parameter in make_request to add headers such as authentication tokens.
- API Monitoring: Leverage the response_headers and response_data fields to debug or analyze responses.
- Integration with External Tools: Export logs for use with monitoring tools like ELK Stack or Datadog.
Phần mềm này và các tệp liên kết ("Phần mềm")
được sử dụng (chạy, tuỳ biến, chạy sau khi được tuỳ biến)
chỉ khi bạn mua được giấy phép có hiệu lực từ tác giả,
điển hình như qua các Ứng dụng Odoo, hoặc trong trường hợp
bạn nhận được thoả thuận bằng văn bản từ tác giả của Phần mềm (chi tiết tại tệp COPYRIGHT).
Bạn có thể phát triển các phân hệ Odoo có sử dụng Phần mềm như một Thư viện (thường là phụ thuộc vào, nhập vào và sử dụng nguồn của nó)
nhưng không sao chéo bất kỳ mã nguồn hay tài liệu nào thuộc Phần mềm. Bạn có thể phân phối những phân hệ này theo giấy phép
mà bạn lựa chọn, miễn sao nội dung giấy phép đó tương tích với điều khoản của Giấy phép Phần mềm Độc quyền Odoo
(ví dụ: LGPL, MIT hay bất kỳ loại giấy phép phần mềm độc quyền nào tương tự vậy).
Nghiêm cấm phát hành, phân phối, cấp phép lại hoặc bán bản sao của Phần mềm hoặc
bản sao
Phần mềm đã được sửa đổi.
Thông báo bản quyền và chấp thuận nêu trên buộc phải được bao gồm trong tất cả các bản sao hoặc các phần quan trọng của Phần mềm.
PHẦN MỀM ĐƯỢC CUNG CẤP "NGUYÊN TRẠNG", KHÔNG BẢO ĐẢM DƯỚI BẤT KỲ HÌNH THỨC NÀO, ĐƯỢC THỂ HIỆN RÕ RÀNG HOẶC NGỤ Ý, KHÔNG GIỚI HẠN
ĐẢM BẢO VỀ CÁC BẢO ĐẢM NGỤ Ý VỀ KHẢ NĂNG THƯƠNG MẠI, PHÙ HỢP VỚI MỤC ĐÍCH CỤ THỂ VÀ KHÔNG VI PHẠM.
TRONG MỌI TRƯỜNG HỢP SẼ KHÔNG CÓ TÁC GIẢ HOẶC CHỦ SỞ HỮU BẢN QUYỀN NÀO CHỊU TRÁCH NHIỆM VỀ BẤT KỲ KHIẾU NẠI,
THIỆT HẠI HOẶC TRÁCH NHIỆM PHÁP LÝ KHÁC NÀO TRONG PHẠM VI HỢP ĐỒNG, CÁC THIỆT HẠI HOẶC CÁCH KHÁC, PHÁT SINH TỪ, NGOÀI HOẶC
CÓ LIÊN KẾT VỚI PHẦN MỀM HOẶC VIỆC SỬ DỤNG HOẶC KINH DOANH KHÁC TẠI PHẦN MỀM.