Adding New API Endpoints
Relevant source files
The following files were used as context for generating this wiki page:
Introduction
This guide outlines the process of adding new API endpoints to the Social-Connect project. The architecture relies on a centralized APIClient for network requests, service-level abstractions like UserService for business logic, and a KeychainManager for secure authentication.
Adding a new endpoint typically involves defining the request logic in a service class, configuring the network call via the APIClient, and optionally linking the functionality to external triggers such as deep links handled by the AppDelegate.
Core Networking Architecture
The networking layer is built around the APIClient class, which manages the base URL and request execution. It uses URLSession for data tasks and automatically attaches authentication headers.
APIClient Components
The APIClient provides a standard template for POST requests. It handles URL construction by appending endpoint strings to a fixed base URL: https://api.socialconnect.com/v1/.
| Component | Description | Source |
|---|---|---|
baseURL | The root URL for all API calls: https://api.socialconnect.com/v1/ | APIClient.swift:4 |
post(endpoint:completion:) | Executes a POST request to a specific endpoint | APIClient.swift:7-40 |
Authorization | Bearer token retrieved from Keychain for authenticated requests | APIClient.swift:18-20 |
Data Flow for API Requests
The following diagram illustrates how a request moves from a service call through the networking layer to the remote server.
Implementation Steps
1. Define Service-Level Logic
New API endpoints should be encapsulated within service classes (e.g., UserService). These classes act as an intermediary between the UI/Handlers and the APIClient.
func followUser(userId: String, completion: ((Bool, Error?) -> Void)? = nil) {
let endpoint = "users/\(userId)/follow"
apiClient.post(endpoint: endpoint) { success, error in
if success {
NotificationCenter.default.post(name: .userFollowStatusChanged, object: nil)
}
completion?(success, error)
}
}
Sources: UserService.swift:8-17
2. Handle Authentication
The APIClient automatically interacts with KeychainManager to fetch the authToken. When adding an endpoint, ensure the user is logged in so that KeychainManager.shared.getAuthToken() returns a valid string for the Authorization header.
Sources: APIClient.swift:17-20, KeychainManager.swift:10-25
3. Integrating with Deep Links (Optional)
If the new endpoint needs to be accessible via a deep link, it must be registered in the DeepLinkHandler. The AppDelegate passes the incoming URL to the handler, which parses the host and parameters.
Security and Authentication
All outgoing requests through APIClient are configured with a Content-Type: application/json header. If a token exists in the system keychain under the account "authToken", it is injected as a Bearer token.
- Keychain Key:
authToken - Security Class:
kSecClassGenericPassword - Header Format:
Authorization: Bearer <token>
Sources: APIClient.swift:15-20, KeychainManager.swift:11-16
Conclusion
To add a new API endpoint, developers must determine the endpoint path, implement a corresponding method in a service class that calls APIClient.post(), and ensure the KeychainManager is populated with a valid token for authenticated routes. Success states should be communicated back to the application using completion handlers or NotificationCenter.