Michael Long
1 min readDec 21, 2024

--

Endpoint is a bit unwieldy as presented, with a lot of duplicated code. Further, all of those structs would need to be changed should the endpoint or required parameters change.

Better to extend the Endpoint protocol with, say, protocol ToDoEndpoint: Endpoint, and then add an extension that defines the default baseURL and headers. Makes the other endpoint structs a lot easier to code.

To be flexible, you really should add default encoders/decoders variables as well, since not every API will work with the base versions. (Say, something that needs ISO dates.)

And I'd probably split up "fetch" into two methods, one that fetches raw data, and the second that decodes it.

How would one add, say, a persistent auth token?

From a Clean code perspective, we should follow separation of concerns where the View Model should probably be talking to a ToDoService or repository. It shouldn't know anything about the network layer or network manager.

What if, say, we wanted to store to do's in a database instead? Or cache them? Or also save them to disk for offline use?

The VM shouldn't be aware of any of that, nor even the specific request structs.

Good start on a classic network manager though.

--

--

Michael Long
Michael Long

Written by Michael Long

I write about Apple, Swift, and SwiftUI in particular, and technology in general. I'm also a Lead iOS Engineer at InRhythm, a modern digital consulting firm.

No responses yet