# ronasit/swift-networking architecture

> Architecture diagram of the GitHub repository ronasit/swift-networking, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-05-13). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ronasit/swift-networking
- Repository: https://github.com/ronasit/swift-networking

## Overview

##### 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.

## Diagram (Mermaid)

```mermaid
flowchart TD
    %% Client Layer
    App["Example iOS App<br/>(NetworkingExample)"]:::client
    click App "https://github.com/ronasit/swift-networking/tree/master/NetworkingExample/NetworkingExample"

    %% Core Library
    subgraph "Networking Core"
        direction TB

        subgraph "Service Layer"
            direction TB
            NS["NetworkService"]:::core
            click NS "https://github.com/ronasit/swift-networking/blob/master/Sources/Services/NetworkService.swift"
            EP["Endpoint Protocols"]:::core
            click EP "https://github.com/ronasit/swift-networking/blob/master/Sources/Endpoints/Endpoint.swift"
        end

        subgraph "Infrastructure"
            direction TB

            subgraph "Request Adapting"
                RAS["RequestAdaptingService"]:::infra
                click RAS "https://github.com/ronasit/swift-networking/blob/master/Sources/Services/RequestAdaptingService.swift"
                RA_chain["RequestAdapters"]:::infra
                click RA_chain "https://github.com/ronasit/swift-networking/blob/master/Sources/RequestAdapters/RequestAdapter.swift"
            end

            subgraph "Error Handling"
                EHS["ErrorHandlingService"]:::infra
                click EHS "https://github.com/ronasit/swift-networking/blob/master/Sources/Services/ErrorHandlingService.swift"
                EH_chain["ErrorHandlers"]:::infra
                click EH_chain "https://github.com/ronasit/swift-networking/blob/master/Sources/ErrorHandlers/ErrorHandler.swift"
            end

            ATS["AccessTokenSupervisor"]:::infra
            click ATS "https://github.com/ronasit/swift-networking/blob/master/Sources/Services/AccessTokenSupervisor.swift"

            subgraph "Serializers"
                SER["ResponseSerializers"]:::infra
                click SER "https://github.com/ronasit/swift-networking/blob/master/Sources/Serializers/ResponseSerializer.swift"
            end

            RS["ReachabilityService"]:::infra
            click RS "https://github.com/ronasit/swift-networking/blob/master/Sources/Reachability/ReachabilityService.swift"
        end
    end

    %% Transport Layer
    subgraph "Transport"
        AF["Alamofire<br/>(URLSession)"]:::transport
    end

    %% External
    Internet["Internet<br/>(TCP/IP)"]:::external

    %% Data Flows
    App -->|"callsServiceMethod"| NS
    NS -->|"buildsEndpoint"| EP
    NS -->|"HTTPRequest"| RAS
    RAS -->|"appliesAdapters"| AF
    AF -->|"sendsHTTP"| Internet
    Internet -->|"HTTPResponse"| AF
    AF -->|"responseToSerialize"| SER
    SER -->|"decodedModel"| App
    AF -->|"errorResponse"| EHS
    EHS -->|"ErrorPayload"| App
    EHS -->|"on401Trigger"| EH_chain
    EH_chain -->|"UnauthorizedHandler"| ATS
    ATS -->|"refreshAndRetry"| RAS
    RS -->|"monitorNetwork"| Internet
    RS -->|"statusUpdates"| App

    %% Styles
    classDef client fill:#ffd,stroke:#888,stroke-width:1px
    classDef core fill:#def,stroke:#555,stroke-width:1px
    classDef infra fill:#dfe,stroke:#444,stroke-width:1px
    classDef transport fill:#fed,stroke:#666,stroke-width:1px
    classDef external fill:#fdd,stroke:#222,stroke-width:1px
```
