Network Layer Architecture
Relevant source files
The following files were used as context for generating this wiki page:
The Network Layer Architecture of Social-Connect provides a centralized system for managing HTTP communications, authentication persistence, and deep-link-driven network actions. It abstracts the complexities of URLSession and security handling into a modular service-based structure, allowing high-level features like user social interactions to execute network requests seamlessly.
The architecture is built on a layered approach where the APIClient handles low-level request construction and execution, while domain-specific services like UserService manage business logic and endpoint definitions. External triggers, such as deep links, interface with this layer through a dedicated handler that translates URL schemes into service calls.
Sources: APIClient.swift, UserService.swift, DeepLinkHandler.swift
Core Components
The network architecture consists of several specialized components working together to facilitate communication with the Social-Connect API.
| Component | Description |
|---|---|
APIClient | The foundation class responsible for configuring URLRequest objects, attaching headers, and executing URLSession data tasks. |
UserService | A singleton service that abstracts user-related network operations like following/unfollowing users. |
KeychainManager | A security utility used to retrieve sensitive authentication tokens required for authorized API requests. |
DeepLinkHandler | A logic layer that parses incoming URLs to trigger specific network-bound actions within the application. |
Sources: APIClient.swift:3-4, UserService.swift:3-6, KeychainManager.swift:4-7, DeepLinkHandler.swift:3-6
Class Relationships
The following diagram illustrates the structural relationships between the core networking and security components.
Request Execution Flow
The network layer uses a standardized flow for executing HTTP POST requests. When a service requests an operation, the APIClient constructs a URL by appending the endpoint to a base URL (https://api.socialconnect.com/v1/).
Authentication and Headers
Every request automatically attempts to include an "Authorization" header. The APIClient queries the KeychainManager for a stored authToken. If found, it is added as a "Bearer" token. Additionally, the "Content-Type" is strictly set to "application/json".
// Request configuration snippet
var request = URLRequest(url: url)
request.httpMethod = "POST"
request.addValue("application/json", forHTTPHeaderField: "Content-Type")
if let token = KeychainManager.shared.getAuthToken() {
request.addValue("Bearer \(token)", forHTTPHeaderField: "Authorization")
}
Sources: APIClient.swift:4, APIClient.swift:14-22
Data Flow Sequence
This sequence diagram shows the path from a service call through the networking stack to the remote server.
Social Interaction Services
The UserService acts as a high-level interface for social networking features. It manages endpoints specifically related to user relationships.
| Feature | Endpoint Pattern | Notification Triggered |
|---|---|---|
| Follow User | users/{userId}/follow | .userFollowStatusChanged |
| Unfollow User | users/{userId}/unfollow | .userFollowStatusChanged |
When a network request succeeds, the service broadcasts a NotificationCenter event to allow the UI to update dynamically without direct coupling.
Sources: UserService.swift:9-26
Deep Link Integration
The application leverages deep links to trigger network-based navigation and actions from outside the app environment. The AppDelegate captures the URL and passes it to the DeepLinkHandler.
Supported Deep Link Hosts
The DeepLinkHandler supports several host types that map to internal functions:
- profile: Navigates to a specific user profile using a
userIdquery item. - follow: Directly triggers a network call via
UserServiceto follow a user. - message / post: Reserved for future messaging and content viewing implementations.
Sources: DeepLinkHandler.swift:15-26, DeepLinkHandler.swift:31-48
Security and Persistence
Sensitive data handling is isolated within the KeychainManager. It uses the iOS Security framework to interact with the system keychain. The network layer relies on this for retrieving the authToken using the kSecClassGenericPassword attribute and the account identifier "authToken".
Sources: KeychainManager.swift:11-21
Conclusion
The Social-Connect Network Layer Architecture provides a robust foundation for mobile-to-server communication. By combining a centralized APIClient with secure token management via KeychainManager and high-level abstractions in UserService, the system ensures that network operations are consistent, secure, and easily maintainable. The integration with DeepLinkHandler further extends the network layer's reach, allowing external events to drive complex internal social interactions.