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 publicclass. 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).
Summary
Open up
APIClientService
to use customURLSession
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 ofURLSession
that doesn't fit your needs. There may also be situations where you want to work on a non-Apple platform (such as Linux) andURLSession
isn't fully supported.To solve this,
APIClientService
will now be opened as apublic
class
. Theinit()
method will contain a parameter, namedconfiguration
. This will have the standardURLSession
instance by default if nothing is inserted. It's recommended to not create an instance ofAPIClientService
directly: instead, an instance ofATProtocolConfiguration
will give API users the opportunity to add a customURLSession
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 aninit()
method that takes a type ofURLSession
, it will instead take aninit()
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 anyATProtoKitConfiguration
-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
andURLSessionConfiguraton
(among other classes related to both).Is this a breaking change?
No
Additional Context
N/A