How ronasit/swift-networking works
Project type and purpose
• This is an open-source Swift network abstraction library (“Networking”) built on top of Alamofire, plus an example iOS app (“NetworkingExample”) and a test suite.
• It provides a high-level API for making HTTP requests, handling errors, adapting requests (e.g. adding tokens or headers), automatic token refreshing, reachability, and response serialization.
High-level architectural components
a. Client/Consumer
• The Example iOS App (NetworkingExample) or any downstream app that imports the Networking package via Swift Package Manager or Xcode.
b. Networking Core Library
• NetworkService & subclasses — base class for API calls
• Endpoint protocols — define path, method, parameters, encoding
• RequestAdaptingService & RequestAdapter chain — header/token injection
• ErrorHandlingService & ErrorHandler chain — logging, retry, token refresh
• UnauthorizedErrorHandler & TokenRequestAdapter — built-in support for 401 → refresh flow
• ReachabilityService — network status monitoring via Combine
• Serializers — JSON, String, Data, Empty response serializers
• Models, Headers, Errors, Extensions — supporting types
c. Underlying Transport
• Alamofire → URLSession → TCP/IP
Interactions & Data flows
• The example app calls a method on a NetworkService subclass (e.g., AuthService.signIn()).
• NetworkService builds an Endpoint instance.
• It passes the request to RequestAdaptingService, which iterates its RequestAdapters (e.g. TokenRequestAdapter, CustomHeaderAdapter) and mutates the underlying Alamofire request.
• The adapted request is executed via Alamofire.
• Upon receiving a response, Networking Core uses appropriate ResponseSerializer to decode into Decodable, Data, String, etc.
• On success, the result is dispatched via the service’s success callback.
• On failure, the error is wrapped into an ErrorPayload and sent to ErrorHandlingService.
• Built-in GeneralErrorHandler maps URLError/AFError codes to GeneralRequestError.
• If status is 401 and you’ve provided an UnauthorizedErrorHandler + TokenRequestAdapter + AccessTokenSupervisor, the chain triggers a token refresh, then retries the original request.
• Custom ErrorHandlers (e.g. LoggingErrorHandler) can log or divert flow (continue, retry, or fail).
• ReachabilityService runs in parallel; it monitors network status and exposes a Combine publisher to the app.
Architectural patterns & design principles
• Chain of Responsibility — for RequestAdapters and ErrorHandlers.
• Template Method & Inheritance — NetworkService subclasses define concrete API methods.
• Protocol-oriented design — many protocols (Endpoint, UploadEndpoint, RequestAdapter, ErrorHandler, AccessTokenSupervisor, etc).
• Separation of concerns — request building, adapting, transport, serialization, error handling, token management, and reachability are decoupled.