All Projects → radianttap → Avenue

radianttap / Avenue

Licence: mit
Wrapper around URLSession and URLSessionTask to enable seamless integration with Operation / OperationQueue.

Programming Languages

swift
15916 projects

Projects that are alternatives of or similar to Avenue

Macfinder
An iOS Library that helps you find the MAC Address of a specific IP
Stars: ✭ 57 (-1.72%)
Mutual labels:  network, networking
Ineter
Fast Java library for working with IP addresses, ranges, and subnets
Stars: ✭ 39 (-32.76%)
Mutual labels:  network, networking
Apipie
Transform api declaration to js object for frontend. Inspired by VueRouter, koa-middleware and axios.
Stars: ✭ 29 (-50%)
Mutual labels:  api-client, api-wrapper
Metta
An information security preparedness tool to do adversarial simulation.
Stars: ✭ 867 (+1394.83%)
Mutual labels:  network, networking
Pnet
High level Java network library
Stars: ✭ 49 (-15.52%)
Mutual labels:  network, networking
Bash Toolkit
Este proyecto esá destinado a ayudar a los sysadmin
Stars: ✭ 13 (-77.59%)
Mutual labels:  network, networking
Dratini
Dratini is a neat network abstraction layer.
Stars: ✭ 38 (-34.48%)
Mutual labels:  network, networking
Slack
🎉✨ Slack API client for Node and browsers.
Stars: ✭ 903 (+1456.9%)
Mutual labels:  api-client, api-wrapper
Vibe.d
Official vibe.d development
Stars: ✭ 1,043 (+1698.28%)
Mutual labels:  async, networking
Uvloop
Ultra fast asyncio event loop.
Stars: ✭ 8,246 (+14117.24%)
Mutual labels:  async, networking
Librg
🚀 Making multi-player gamedev simpler since 2017
Stars: ✭ 813 (+1301.72%)
Mutual labels:  network, networking
Wikipedir
R's MediaWiki API client library
Stars: ✭ 54 (-6.9%)
Mutual labels:  api-client, api-wrapper
Diffios
Cisco IOS diff tool
Stars: ✭ 23 (-60.34%)
Mutual labels:  network, networking
Libzmq
ZeroMQ core engine in C++, implements ZMTP/3.1
Stars: ✭ 7,418 (+12689.66%)
Mutual labels:  network, networking
Groovehq
Ruby gem for GrooveHQ api
Stars: ✭ 22 (-62.07%)
Mutual labels:  api-client, api-wrapper
Xdp
Package xdp allows one to use XDP sockets from the Go programming language.
Stars: ✭ 36 (-37.93%)
Mutual labels:  network, networking
Nexer
Content based network multiplexer or redirector made with love and Go
Stars: ✭ 7 (-87.93%)
Mutual labels:  network, networking
P2p
Practice project to demonstrate p2p file sharing.
Stars: ✭ 16 (-72.41%)
Mutual labels:  network, networking
Dknetworking
基于 AFNetworking + YYCache 的二次封装,支持缓存策略的网络请求框架
Stars: ✭ 41 (-29.31%)
Mutual labels:  network, networking
React Native Netinfo
React Native Network Info API for Android & iOS
Stars: ✭ 1,049 (+1708.62%)
Mutual labels:  network, networking

platforms: iOS|tvOS|watchOS
Carthage compatible CocoaPods compatible

Avenue

Micro-library designed to allow seamless cooperation between URLSession(Data)Task and Operation(Queue) APIs.

Why?

URLSession framework is, on its own, incompatible with Operation API. A bit of trickery is required to make them cooperate. (note: do read those blog posts)

I have extended URLSessionTask with additional properties of specific closure types which allows you to overcome this incompatibility.

OperationQueue and Operation 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 at Alley — it’s much simpler but surprisingly capable.

Installation

Manually

  • If you are not using Swift Essentials already, make sure to include Essentials folder from here into your project
  • Also add Avenue and Alley, just copy them into your project.
  • To handle self-signed SSL, pinned certificates and other similar security stuff - add ServerTrust as well.

· · ·

If you prefer to use dependency managers, see below. Releases are tagged with Semantic Versioning in mind.

CocoaPods

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 your Podfile:

pod 'Avenue', 	:git => 'https://github.com/radianttap/Avenue.git'

Carthage

Carthage is a decentralized dependency manager that automates the process of adding frameworks to your Cocoa application.

You can install Carthage with Homebrew using the following command:

$ brew update
$ brew install carthage

To integrate Avenue into your Xcode project using Carthage, specify it in your Cartfile:

github "radianttap/Avenue"

Usage

(1) Subclass NetworkSession to create your API wrapper, configure URLSession for the given service endpoints and make an OperationQueue instance.

final class WebService: NetworkSession {
	private init() {
		queue = {
			let oq = OperationQueue()
			oq.qualityOfService = .userInitiated
			return oq
		}()

		let urlSessionConfiguration: URLSessionConfiguration = {
			let c = URLSessionConfiguration.default
			c.allowsCellularAccess = true
			c.httpCookieAcceptPolicy = .never
			c.httpShouldSetCookies = false
			c.requestCachePolicy = .reloadIgnoringLocalCacheData
			return c
		}()

		super.init(urlSessionConfiguration: urlSessionConfiguration)
	}

	//	Local stuff

	private var queue: OperationQueue
}

(2) Model API endpoints in any way you want. See IvkoService example in the Demo app for one possible way, using enum with associated values.

The end result of that model would be URLRequest instance.

(3) Create an instance of NetworkOperation and add it to the queue

let op = NetworkOperation(urlRequest: urlRequest, urlSession: urlSession) {
	payload in
	//   ...process NetworkPayload...
}
queue.addOperation(op)

It will be automatically executed. You can also supply the desired number of automatic retries, among other arguments.

See AssetManager and IvkoService in the Demo project, as examples. Write as many of these as you need.

Tips

  • 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.

  • NetworkPayload is particularly useful struct since it aggregates URLRequest + response headers, data and error and gives you simple speed metering capability by recording start and end of each network call.

  • ServerTrustPolicy is directly picked up from Alamofire v4; it’s great as it is and there’s no need for me to reinvent the wheel.

  • Set ServerTrustPolicy.defaultPolicy in 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 own simple subclass which makes sure that Operation is marked finished only when the network async callback returns. Atomic.swift is required by AsyncOperation.

Compatibility

Platform and Swift compatibility is listed at the top of this document.

License

MIT License, like all my open source code.

Credits

  • 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 this great talk which got me started to write this library. There’s a blog 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.

Learn more

Helpful articles

Tools

  • Bad SSL in many ways, fantastic resource to test your code.

  • nscurl --help (in your macOS Terminal)

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].