Skip to content

drinking/SwiftAPI

Repository files navigation

SwiftAPI

SwiftAPI provides an efficient tool to generate executable Swift API codes from API Blueprint documents directly. And it'll relieve you from trivial works on writing API codes. It also includes some tools to convert JSON to various models, Thanks to Nix Zhu's open source project Coolie .

Install

Install Draft Command line tool, which will help to parse API Blueprint into JSON format AST file.

Usage

Build and Run macOS app SwiftAPIBuilder. Then set input path for apib(API Blueprint format) file and out put path for Swift file. After starting listening, it will automatically translate apib into swift and save code at out put path when editing apib file.

APIService

Generated APIServices wrap Alamofire to do networking request, and use ObjectMapper to map JSON to Mappable model which is also generated by APISwiftBuilder. RxSwift is included to make request observable.

public class GITHUBUSER: PUBLICAPIService<Empty,UserInfo> {
    public class func instance()->PUBLICAPIService<Empty,UserInfo>{
        return PUBLICAPIService<Empty,UserInfo>(subPath:"/users/{name}",method:.get)
    }
}

If several requests share the same host and heads, you can set a global one for their base class. It also can set a custom one if necessary.

PUBLICAPIService<Empty,Empty>.setup(host: "https://api.github.com")

//request with no argument and no observable
GITHUBISSUE.instance().send().response { (response) in
	//do sth...
}

//request with path arguments and observable
let get = GITHUBUSER.instance()
get.fillPathArgs("drinking") //to construct https://api.github.com/users/drinking
get.sendSginal().do(onNext: { (userInfo) in
	//do sth...
 })
 
 //request with Mappable params and observable
 let get = SOMEREQUEST.instance()
get.customHost = "https://www.example.com"
 var params = SOMEREQ()
 params.id = "123456"
 params.name = "drinking"
get.sendSginal(params).do(onNext: { (someResult) in
	//do sth...
 })

SwiftAPIBuilder extract most API attributes into DKTransition, and it's possible and easy to translate to other languages.

UnitTest

SwiftAPIService also includes codes that can run UnitTest directly.

GITHUBISSUE.runTest(testor ,host: "https://api.github.com",expect:{
    let issues = $0.entities;
    expect(issues.count) == 30
});

Convertor

Convertor can translate JSON to ObjectiveC Model、ABIP Model and Swift Mappable Model.

Requirements

  • iOS 9.0+
  • Xcode 8.0+
  • Swift 3.0+

Contributing

Bug reports, feature requests and any suggestions on implementations are welcome.

License

MIT License.