- Notifications
You must be signed in to change notification settings - Fork6
Wrapper around URLSession and URLSessionTask to enable seamless integration with Operation / OperationQueue.
License
radianttap/Avenue
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Micro-library designed to allow seamless cooperation between URLSession(Data)Task and Operation(Queue) APIs.
URLSession framework is, on its own,incompatible with Operation API. A bit oftrickery is required to make them cooperate.(note: do read those blog posts)
I haveextended URLSessionTask with additional properties of specific closure types which allows you to overcome this incompatibility.
OperationQueue andOperation are great API to use when...
- your network requests are inter-dependent on each other
- need to implement OAuth2 or any other kind of asynchronous 3rd-party authentication mechanism
- tight control over the number of concurrent requests towards a particular host is paramount
- etc.
If this is too complex for your needs, take a look atAlley — it’s much simpler but surprisingly capable.
- If you are not usingSwift Essentials already, make sure to include
Essentialsfolder from here into your project - Also add
AvenueandAlley, just copy them into your project. - To handle self-signed SSL, pinned certificates and other similar security stuff - add
ServerTrustas well.
· · ·
If you prefer to use dependency managers, see below.Releases are tagged withSemantic Versioning in mind.
CocoaPods is a dependency manager for Cocoa projects. For usage and installation instructions, visit their website. To integrate Avenue into your Xcode project using CocoaPods, specify it in yourPodfile:
pod'Avenue',:git=>'https://github.com/radianttap/Avenue.git'
Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.
You can install Carthage withHomebrew using the following command:
$ brew update$ brew install carthage
To integrate Avenue into your Xcode project using Carthage, specify it in yourCartfile:
github "radianttap/Avenue"(1) SubclassNetworkSession to create your API wrapper, configureURLSession for the given service endpoints and make anOperationQueue instance.
finalclassWebService:NetworkSession{privateinit(){queue={letoq=OperationQueue()oq.qualityOfService=.userInitiatedreturn oq}()leturlSessionConfiguration:URLSessionConfiguration={letc=URLSessionConfiguration.defaultc.allowsCellularAccess=truec.httpCookieAcceptPolicy=.neverc.httpShouldSetCookies=falsec.requestCachePolicy=.reloadIgnoringLocalCacheDatareturn c}()super.init(urlSessionConfiguration: urlSessionConfiguration)}//Local stuffprivatevarqueue:OperationQueue}
(2) Model API endpoints in any way you want. SeeIvkoService example in the Demo app for one possible way, using enum with associated values.
The end result of that model would beURLRequest instance.
(3) Create an instance ofNetworkOperation and add it to thequeue
letop=NetworkOperation(urlRequest: urlRequest, urlSession: urlSession){payloadin// ...process NetworkPayload...}queue.addOperation(op)
It will be automatically executed. You can also supply the desired number of automatic retries, among other arguments.
See
AssetManagerandIvkoServicein the Demo project, as examples. Write as many of these as you need.
Avenue handles just the URLSession boilerplate: URLErrors, HTTP Auth challenges, Server Trust Policy etc.
The only assumption Avenue makes is that web service you connect to is HTTP(S) based.
NetworkPayloadis particularly useful struct since it aggregatesURLRequest+ response headers, data and errorand gives you simple speed metering capability by recording start and end of each network call.ServerTrustPolicyis directly picked up fromAlamofire v4; it’s great as it is and there’s no need for me to reinvent the wheel.Set
ServerTrustPolicy.defaultPolicyin your project configuration file (or wherever is appropriate) to the value you need for each app target you have. For example, if you connect to some self-signed demo API host:ServerTrustPolicy.defaultPolicy = .disableEvaluation
Note:AsyncOperation is my ownsimple subclass which makes sure thatOperation is markedfinished only when the network async callback returns.Atomic.swift is required byAsyncOperation.
Platform and Swift compatibility is listed at the top of this document.
MIT License, like all my open source code.
Alamofire community for their invaluable work over the years. I don’t use the library itself, but there are re-usable gems in it (like ServerTrustPolicy handling).
Marcus Zarra for thisgreat talk which got me started to write this library. There’s ablog post on his blog too.
I want re-iterate what Marcus said at the end of his talk:
Write it [network code] yourself. I guarantee code you write yourself will be faster than any generic code, that is the law. Whenever you write something that is very specific, it is going to be faster than generics.
Alley – automatic retries for
URLSessionDataTaskWWDC 2017, Session 709:Advances in Networking, Part 2
Security analysis of ATS
Alamofirenotes on ATS
ResolvingATS issues
Bad SSL in many ways, fantastic resource to test your code.
nscurl --help(in your macOS Terminal)
About
Wrapper around URLSession and URLSessionTask to enable seamless integration with Operation / OperationQueue.
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.