KBAPISupport 4.0.0-b01

KBAPISupport 4.0.0-b01

Kirill byss Bystrov 维护。



KBAPISupport 4.0.0-b01

  • Kirill byss Bystrov

KBAPISupport

基于 NSURLSession 的轻量级 Swifty 包装器,受到了 Moya/Alamofire 的强烈影响。

许可

MIT

快速入门


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)");
	}
};

更多文档!

![KBAPISupportDemo.playground] 包含上述示例代码,你可以对其进行调整并立即得到结果,或者在 Xcode 中检查其他用法模式和各种技巧。此外,通过整个库的 openpublic 符号应该有很好的文档说明。