Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
//
// RackJsonSchemaApi.swift
//
// Auto generated by Aries on 2015/04/11.
// Do not modify this file directory
//
import Alamofire
import SwiftTask
import URITemplate
typealias RackJsonSchemaApiResponse = Task<Int,AnyObject,NSError>
protocol RackJsonSchemaApiDelegate {
func customReqeust(request: NSMutableURLRequest) -> NSMutableURLRequest
func onDefaultSuccess(response: AnyObject) -> AnyObject
func onDefaultFailure(err: NSError) -> NSError
}
class RackJsonSchemaApi {
private struct BaseUrlStringStruct{ static var url = "http://localhost:5000" }
class var BaseUrlString: String {
get{ return BaseUrlStringStruct.url }
set{ BaseUrlStringStruct.url = newValue }
}
// workaround tips
// After updated swift 1.2, use ( static var someVariable: Int = 0 ) instead
private struct delegateStruct{ static var detegator: RackJsonSchemaApiDelegate? }
class var delegate: RackJsonSchemaApiDelegate? {
get{ return delegateStruct.detegator }
set{ delegateStruct.detegator = newValue }
}
private class func defaultURLRequest(let path: String, let method: Alamofire.Method) -> NSMutableURLRequest {
let URL = NSURL(string: RackJsonSchemaApi.BaseUrlString)!
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path))
mutableURLRequest.HTTPMethod = method.rawValue
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
mutableURLRequest.setValue("application/json", forHTTPHeaderField: "Accept")
if let delegate = self.delegate?{
return delegate.customReqeust(mutableURLRequest)
}
return mutableURLRequest
}
private class func generateURI(template: String, params: [String: AnyObject]?) -> String {
if let params = params {
return URITemplate(template: template).expand(params)
}
return template
}
class Base {
private class func request(URLRequest: URLRequestConvertible) -> RackJsonSchemaApiResponse {
return RackJsonSchemaApiResponse {(progress, fulfill, reject, configure) in
let alam = Alamofire.request(URLRequest)
.validate()
.responseJSON({ (req, res, data, err) -> Void in
if let err = err {
reject(self.buildErr(res, data: data, err: err))
return
}
fulfill(data!)
})
debugPrintln(alam)
configure.pause = { [weak alam] in if let alam = alam { alam.suspend() } }
configure.resume = { [weak alam] in if let alam = alam { alam.resume() } }
configure.cancel = { [weak alam] in if let alam = alam { alam.cancel() } }
}.then({ (data, errorInfo) -> RackJsonSchemaApiResponse in
return RackJsonSchemaApiResponse { (progress, fulfill, reject, configure) in
if(errorInfo == nil){
if let delegate = RackJsonSchemaApi.delegate {
let newData: AnyObject = delegate.onDefaultSuccess(data!)
fulfill(newData)
return
}
fulfill(data!)
return
}else{
let (err, isCancelled) = errorInfo!
if let err = err {
if let delegate = RackJsonSchemaApi.delegate{
let newErr = delegate.onDefaultFailure(err)
reject(newErr)
return
}
reject(err)
return
}
}
}
})
}
private class func buildErr(res: NSHTTPURLResponse?, data: AnyObject?, err: NSError?) -> NSError {
var userInfo = [String: AnyObject]()
if let res = res {
userInfo["statusCode"] = res.statusCode
}else{
userInfo["statusCode"] = 502
}
if let data: AnyObject = data {
userInfo["response"] = data
}
if let err = err {
userInfo["originalError"] = err
}
var error = NSError(domain: "com.aries.error", code: -1, userInfo: userInfo)
return error
}
}
class App: Base {
class func Create(name: String? = nil) -> RackJsonSchemaApiResponse {
var params = [String: AnyObject]()
if let name = name {
params["name"] = name
}
return request(Router.Create(params))
}
class func Delete(appId: String) -> RackJsonSchemaApiResponse {
var params = [String: AnyObject]()
params["appId"] = appId
return request(Router.Delete(params))
}
class func Info(appId: String) -> RackJsonSchemaApiResponse {
var params = [String: AnyObject]()
params["appId"] = appId
return request(Router.Info(params))
}
class func Info(appId: String) -> RackJsonSchemaApiResponse {
var params = [String: AnyObject]()
params["appId"] = appId
return request(Router.Info(params))
}
class func List() -> RackJsonSchemaApiResponse {
var params: [String: AnyObject]? = nil
return request(Router.List(params))
}
class func Update(appId: String, name: String? = nil) -> RackJsonSchemaApiResponse {
var params = [String: AnyObject]()
params["appId"] = appId
if let name = name {
params["name"] = name
}
return request(Router.Update(params))
}
class func Create(appId: String, file: String? = nil) -> RackJsonSchemaApiResponse {
var params = [String: AnyObject]()
params["appId"] = appId
if let file = file {
params["file"] = file
}
return request(Router.Create(params))
}
private enum Router: URLRequestConvertible {
case Create([String: AnyObject]?)
case Delete([String: AnyObject]?)
case Info([String: AnyObject]?)
case Info([String: AnyObject]?)
case List([String: AnyObject]?)
case Update([String: AnyObject]?)
case Create([String: AnyObject]?)
var method: Alamofire.Method {
switch self {
case .Create:
return .POST
case .Delete:
return .DELETE
case .Info:
return .GET
case .Info:
return .GET
case .List:
return .GET
case .Update:
return .PATCH
case .Create:
return .POST
}
}
var path: String {
switch self {
case .Create(let params):
return RackJsonSchemaApi.generateURI("/apps", params: params)
case .Delete(let params):
return RackJsonSchemaApi.generateURI("/apps/{appId}", params: params)
case .Info(let params):
return RackJsonSchemaApi.generateURI("/apps/{appId}", params: params)
case .Info(let params):
return RackJsonSchemaApi.generateURI("/apps/{appId}/text", params: params)
case .List(let params):
return RackJsonSchemaApi.generateURI("/apps", params: params)
case .Update(let params):
return RackJsonSchemaApi.generateURI("/apps/{appId}", params: params)
case .Create(let params):
return RackJsonSchemaApi.generateURI("/apps/{appId}/files", params: params)
}
}
// MARK: URLRequestConvertible
var URLRequest: NSURLRequest {
switch self{
case .Create(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
case .Delete(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
case .Info(let parameters):
return Alamofire.ParameterEncoding.URL.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
case .Info(let parameters):
return Alamofire.ParameterEncoding.URL.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
case .List(let parameters):
return Alamofire.ParameterEncoding.URL.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
case .Update(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
case .Create(let parameters):
return Alamofire.ParameterEncoding.JSON.encode(RackJsonSchemaApi.defaultURLRequest(path, method: method), parameters: parameters).0
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment