Adding New Services
Guide for implementing new services in the Zyeta backend
This guide outlines the process for adding new services to the Zyeta backend codebase.
Overview
Services in our application form the business logic layer, connecting API endpoints with database models. Each service is responsible for a specific domain of functionality, such as user management, authentication, or agent operations.
Step 1: Create the Service Directory
Create a new directory for your service in the src/services
directory with a descriptive name that follows the naming convention of existing services (e.g., my_feature
).
Step 2: Define Schema Models
Create the schema.py
file to define Pydantic models for your service’s input and output data validation:
Step 3: Implement the Service
Create the service.py
file implementing your service class:
Step 4: Register the Service
The service auto-discovery system will automatically register your service based on the http_exposed
attribute in your service class.
However, ensure your service is imported somewhere in the application. You may need to add an import to src/app.py
or create a module-level import in the respective service package.
Service Implementation Best Practices
-
http_exposed
Attribute:- Format:
["<http_method>=<service_method>", ...]
- Example:
["get=list", "post=create"]
- This maps HTTP methods to service methods for API exposure
- Format:
-
Method Parameters:
- Use FastAPI’s
Depends
for common dependencies like database sessions - Include RBAC for proper permission checking
- Use typed parameters with appropriate annotations
- Use FastAPI’s
-
Response Types:
- Explicitly define return types for all methods
- Use Pydantic models for consistent serialization/validation
-
Error Handling:
- Raise appropriate HTTP exceptions for error conditions
- Include descriptive error messages
- Handle edge cases gracefully
-
Database Operations:
- Use SQLAlchemy’s async operations for database queries
- Structure queries for efficient execution
- Handle database errors appropriately
Example Service Methods
Create Method Example
List Method Example
Testing Services
After creating your service, you should thoroughly test its functionality:
-
Unit Tests:
- Test individual service methods
- Mock database dependencies
- Verify correct error handling
-
Integration Tests:
- Test complete service workflows
- Verify database interactions
- Test permissions and access control
-
API Tests:
- Test HTTP endpoint exposure
- Verify correct request/response handling
- Test error responses
Troubleshooting
Common Issues
-
Service not exposed:
- Check the
http_exposed
attribute for correct format - Verify service is imported and accessible
- Check the
-
Permission errors:
- Check RBAC configuration for correct resource/action
- Verify user has appropriate permissions
-
Database errors:
- Check query syntax and structure
- Verify model relationships
- Handle potential null values
Getting Help
If you encounter issues with service implementation, consult:
- The FastAPI documentation
- The SQLAlchemy documentation
- Existing service implementations in the codebase
- Reach out to the development team for assistance
Was this page helpful?