Skip to main content

Network Client

PrismNetwork provides a protocol-oriented HTTP client layer built on URLSession. Define type-safe requests, decode responses automatically, and swap implementations for testing.

Architecture

PrismNetworkClient

Protocol defining request(on:with:) and redirect(from:). Program against this for testability.

PrismNetworkAdapter

Actor-based URLSession implementation with caching, redirect capture, and status code validation.

PrismNetworkRequest

Ties an endpoint to a response type. Default decoding via PrismEntity.

Basic Usage

1. Define an Endpoint

Endpoints describe a single API call — host, path, method, headers, and body:
UserEndpoint.swift

2. Define a Request

A request ties an endpoint to a Decodable response type:
UserRequest.swift

3. Execute the Request

Fetching Users

Custom Date Formatting

Pass a DateFormatter when your API returns non-standard date formats:
Custom Date Decoding

Redirect Handling

Capture redirect URLs without following them:
Capture Redirect

PrismNetworkAdapter Configuration

The adapter wraps URLSession and supports custom session configurations and caching:
Custom Adapter

Error Handling

All errors are typed as PrismNetworkError:
Handling Errors
PrismNetworkError automatically maps HTTP status codes: 400 → .badRequest, 401 → .unauthorized, 403 → .forbidden, 408/429/5xx → .serverError. It also maps URLError codes like .notConnectedToInternet to .noConnectivity.

Testing with Mock Clients

Because PrismNetworkClient is a protocol, you can create mock implementations:
Mock Client

Next Steps

Type-Safe Endpoints

Learn the full endpoint protocol and enum-based patterns.

Caching & Retry

Add response caching and automatic retry policies.

WebSocket Client

Real-time communication from client apps.

Server HTTP Client

Make outbound HTTP requests from PrismServer.