GithubHelp home page GithubHelp logo

byss / kbapisupport Goto Github PK

View Code? Open in Web Editor NEW
4.0 4.0 2.0 512 KB

Some support classes for easier HTTP API access.

License: MIT License

Objective-C 8.89% Ruby 0.83% C 0.69% Swift 76.56% Objective-C++ 13.03%

kbapisupport's Introduction

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.

kbapisupport's People

Contributors

byss avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Forkers

imofas carabina

kbapisupport's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.