Skip to content

byss/KBAPISupport

Repository files navigation

KBAPISupport

Thin Swifty wrapper for NSURLSession that is strongly influenced by Moya/Alamofire.

License

MIT

Quickstart


import KBAPISupport

/// Arbitrary Decodable type
internal struct Client: Decodable {
	internal let identifier: UUID;
	internal let name: String;
	internal let balance: Decimal;
	internal let creditCardNumber: String;
}

/// Request parameters storage
internal struct MyRequest: KBAPIRequest {
	internal typealias ResponseType = [Client]; /// "Successful" response type
	
	internal let serializer: RequestSerializer = URLEncodingSerializer (); /// Serializer for reqeuest's URL, HTTP Headers and such
	
	internal let responseSerializer = ResponseSerializerType (); /// Serializer (deserializer to be precise) dedicated to response handling
	
	/// Serves as prefix or starting point of request's URL, does not usually change during app lifetime
	internal let baseURL = URL (string: "https://my-glorious-service.com/api/")!;
	
	/// URL "Suffix", usually differs between various request kinds.
	internal var path: String {
		return "clients/get";
	}
	
	/// Splitting URL into base part and suffix is purely optional. One may use generic `url` property to
	/// take full control and responsibility in theirs hands. Or not.
	/// Its value is equal to `URL (string: self.path, relativeTo: self.baseURL)` by default.
	/*
	internal var url: URL {
		return URL (string: "https://en.wikipedia.org/wiki/FTPS_shta_eta");
	}
	*/
}


// Class that actually performs networking
KBAPIConnection (request: MyRequest ()).start {
	// Response block is called asynchronously but on main thread by default
	switch ($0) { // Response type is Optional-like enum called (surprise surpsise) Result.
		case .success (let clients):
			// Networking, backend and response decoding went well, `clients` array is containing `Client` struct instances  
			print ("Let's rock!");
			print (clients.map { $0.creditCardNumber });
		
	case .failure (let error):
		// Any other result is treated as error and is acccompanied by `error` value that contains more details about this incident.
		print ("OH NOES: \(error)");
	}
};

More docs!

![KBAPISupportDemo.playground] contains the example code above, so you may tweak it and get result delivered immediately, or examine other usage patterns and various tricks right in Xcode. Furthermore, open and public symbols throught the library. should be markup-documented quite well.

About

Some support classes for easier HTTP API access.

Resources

License

Stars

Watchers

Forks

Packages