DevPrep

All topics › Networking & APIs

How do you design a networking layer on URLSession: endpoint, client, errors?

Middle PRO

The gist

A good networking layer separates three responsibilities: the request description (Endpoint — a value with no execution logic), the client that turns the description into a URLRequest, executes it and validates the response, and typed errors the UI uses to decide what to show. Cross-cutting concerns — auth, logging, common headers — live in interceptors instead of being copied into every endpoint.

Quality bar: a new request is added with one declaration, and the client is tested without the network and without changing production code.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store