API Client Architecture
Relevant source files
The following files were used as context for generating this wiki page:
The API Client Architecture in Social-Connect provides a centralized and structured way to handle network communications with the backend services. It is designed around a core APIClient that manages HTTP requests, authentication through the KeychainManager, and specialized service layers like UserService that abstract specific domain logic.
This architecture ensures that network logic is decoupled from the user interface and deep link handling, allowing for consistent error handling, authentication injection, and status notification across the application.
Core Components
The architecture consists of several specialized classes working in a tiered structure.
APIClient
The APIClient serves as the primary engine for network communication. It defines the base URL https://api.socialconnect.com/v1/ and utilizes URLSession.shared for executing data tasks. It is responsible for constructing URLRequest objects, setting headers, and injecting bearer tokens for authorized requests.
Sources: APIClient.swift:3-6
KeychainManager
A singleton utility that interfaces with the iOS Security framework. It is responsible for retrieving the authToken stored in the device's keychain, which the APIClient then uses for request authorization.
Sources: KeychainManager.swift:4-7, KeychainManager.swift:10-18
Service Layer (UserService)
The UserService acts as a facade between the application logic and the generic APIClient. It provides high-level methods for user-related actions, such as following or unfollowing users, and handles the resulting success by notifying the rest of the application.
Sources: UserService.swift:3-6
| Component | Responsibility | Primary Method/Property |
|---|---|---|
APIClient | HTTP Request execution & Auth Injection | post(endpoint:completion:) |
UserService | User domain business logic | followUser(userId:completion:) |
KeychainManager | Secure token storage & retrieval | getAuthToken() |
DeepLinkHandler | External trigger processing | handleDeepLink(url:) |
Sources: APIClient.swift, UserService.swift, KeychainManager.swift, DeepLinkHandler.swift
Request Flow and Logic
The request flow typically begins with an external trigger or user action, which propagates through the service layer to the API client.
Authentication Flow
Every request made through the APIClient.post method attempts to include an authentication token. The client queries the KeychainManager for a stored token and, if present, appends it to the "Authorization" header using the Bearer scheme.
Sources: APIClient.swift:16-19
Network Execution Diagram
The following diagram illustrates how a deep link trigger leads to an authenticated API call and subsequent system update.
The diagram shows the sequence from deep link entry to the final notification broadcast.
Sources: AppDelegate.swift:15-18, DeepLinkHandler.swift:39-44, UserService.swift:9-12, APIClient.swift:16-19
Implementation Details
API Endpoint Construction
The system uses a path-based approach for endpoint construction. The APIClient holds the base URL, while services provide specific paths.
// APIClient.swift:9-12
guard let url = URL(string: baseURL + endpoint) else {
completion(false, NSError(domain: "APIClient", code: 0, userInfo: [NSLocalizedDescriptionKey: "Invalid URL"]))
return
}
Response Handling and Notifications
Successful API interactions often trigger local state updates via NotificationCenter. For instance, when a user is followed or unfollowed, the UserService broadcasts userFollowStatusChanged.
Sources: UserService.swift:13-15, UserService.swift:23-25
Class Relationships
The following class diagram defines the structural relationships between the networking components.
The diagram illustrates the dependency hierarchy of the API Client architecture.
Sources: APIClient.swift, UserService.swift, DeepLinkHandler.swift, AppDelegate.swift
Conclusion
The API Client architecture in Social-Connect provides a robust foundation for network operations by separating concerns between secure credential management, low-level HTTP communication, and high-level domain services. This structure allows the application to handle complex flows, such as deep-link-triggered actions, with consistent authentication and state synchronization across the system.