MasterJ93 / ATProtoKit

A straightforward solution for using the AT Protocol and Bluesky, written in Swift.
https://atprotokit.cjrriley.com/documentation/atprotokit/
MIT License
26 stars 5 forks source link

[Feature Enhancement]: Update `APIClientService` for Dependency Injection #30

Closed MasterJ93 closed 1 month ago

MasterJ93 commented 1 month ago

Summary

Open up APIClientService to use custom URLSession instances.

Pain points

Currently, APIClientService relies on using static methods and enums on all of the API calls. This isn't ideal as, you may have a situation where there's a configuration of URLSession that doesn't fit your needs. There may also be situations where you want to work on a non-Apple platform (such as Linux) and URLSession isn't fully supported.

To solve this, APIClientService will now be opened as a public class. The init() method will contain a parameter, named configuration. This will have the standard URLSession instance by default if nothing is inserted. It's recommended to not create an instance of APIClientService directly: instead, an instance of ATProtocolConfiguration will give API users the opportunity to add a custom URLSession instance if they need to. ATProtoKitConfiguration-conforming classes will pass this object through, and the API call will grab this information when needed.

If a non-Apple platform is being used (such as Linux), then a different library will be used (possibly swift-nio). The process would be similar, except that, instead of an init() method that takes a type of URLSession, it will instead take an init() method that takes the type of whatever is used from the open source library. This will be conditionally compiled in order to ensure that Apple platforms don't have the baggage of the library, and non-Apple platforms can still use the project without skipping a beat. ATProtocolConfiguration will take this into account, and any ATProtoKitConfiguration-conforming classes will pass things through. By doing this, this will make ATProtoKit one step closer to becoming fully compatible with all versions of Linux that Swift officially supports (as well as, potentially, Windows).

Finally, this has the added benefit of testing APIClientService individually, if needed.

All methods related to preparing the request will remain static, as they have no need to be instantiated.

Considered Alternatives

We could simply use the open source version of Foundation. However, this causes issues with compatibility from previous versions of Swift, as well as the current completeness of URLSession and URLSessionConfiguraton (among other classes related to both).

Is this a breaking change?

No

Additional Context

N/A