This commit is contained in:
Tony Tam
2015-10-20 10:28:49 -07:00
parent 2f3a2413c7
commit b41a40f9be
14 changed files with 266 additions and 201 deletions
+1 -1
View File
@@ -1,2 +1,2 @@
github "Alamofire/Alamofire" >= 1.2
github "Alamofire/Alamofire" >= 2.0.0
github "mxcl/PromiseKit" >=1.5.3
@@ -7,5 +7,5 @@ Pod::Spec.new do |s|
s.license = 'Apache License, Version 2.0'
s.source_files = 'PetstoreClient/Classes/Swaggers/**/*.swift'
s.dependency 'PromiseKit', '~> 2.1'
s.dependency 'Alamofire', '~> 1.3'
s.dependency 'Alamofire', '~> 2.0.0'
end
@@ -6,19 +6,19 @@
import Foundation
class OneteamAPI {
static let basePath = "http://ec2-52-68-31-200.ap-northeast-1.compute.amazonaws.com/"
public class PetstoreClientAPI {
static let basePath = "http://petstore.swagger.io/v2"
static var credential: NSURLCredential?
static var requestBuilderFactory: RequestBuilderFactory = AlamofireRequestBuilderFactory()
}
class APIBase {
public class APIBase {
func toParameters(encodable: JSONEncodable?) -> [String: AnyObject]? {
let encoded: AnyObject? = encodable?.encodeToJSON()
if encoded! is [AnyObject] {
var dictionary = [String:AnyObject]()
for (index, item) in enumerate(encoded as! [AnyObject]) {
for (index, item) in (encoded as! [AnyObject]).enumerate() {
dictionary["\(index)"] = item
}
return dictionary
@@ -28,7 +28,7 @@ class APIBase {
}
}
class RequestBuilder<T> {
public class RequestBuilder<T> {
var credential: NSURLCredential?
var headers: [String:String] = [:]
let parameters: [String:AnyObject]?
@@ -36,24 +36,24 @@ class RequestBuilder<T> {
let method: String
let URLString: String
required init(method: String, URLString: String, parameters: [String:AnyObject]?, isBody: Bool) {
required public init(method: String, URLString: String, parameters: [String:AnyObject]?, isBody: Bool) {
self.method = method
self.URLString = URLString
self.parameters = parameters
self.isBody = isBody
}
func execute(completion: (response: Response<T>?, erorr: NSError?) -> Void) { }
public func execute(completion: (response: Response<T>?, erorr: ErrorType?) -> Void) { }
func addHeader(#name: String, value: String) -> Self {
public func addHeader(name name: String, value: String) -> Self {
if !value.isEmpty {
headers[name] = value
}
return self
}
func addCredential() -> Self {
self.credential = OneteamAPI.credential
public func addCredential() -> Self {
self.credential = PetstoreClientAPI.credential
return self
}
}
@@ -10,7 +10,7 @@ import PromiseKit
extension PetstoreClientAPI {
class PetAPI: APIBase {
public class PetAPI: APIBase {
/**
@@ -22,19 +22,19 @@ extension PetstoreClientAPI {
- type: oauth2
- name: petstore_auth
:param: body (body) Pet object that needs to be added to the store
- parameter body: (body) Pet object that needs to be added to the store
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func updatePet(#body: Pet?) -> RequestBuilder<Void> {
public class func updatePet(body body: Pet?) -> RequestBuilder<Void> {
let path = "/pet"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let parameters = body?.encodeToJSON() as? [String:AnyObject]
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "PUT", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "PUT", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -47,19 +47,19 @@ extension PetstoreClientAPI {
- type: oauth2
- name: petstore_auth
:param: body (body) Pet object that needs to be added to the store
- parameter body: (body) Pet object that needs to be added to the store
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func addPet(#body: Pet?) -> RequestBuilder<Void> {
public class func addPet(body body: Pet?) -> RequestBuilder<Void> {
let path = "/pet"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let parameters = body?.encodeToJSON() as? [String:AnyObject]
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "POST", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "POST", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -87,7 +87,11 @@ extension PetstoreClientAPI {
} ], contentType=application/json}, {example=<Pet>
<id>123456</id>
<name>doggie</name>
<photoUrls>string</photoUrls>
<photoUrls>
<photoUrls>string</photoUrls>
</photoUrls>
<tags>
</tags>
<status>string</status>
</Pet>, contentType=application/xml}]
- examples: [{example=[ {
@@ -106,17 +110,21 @@ extension PetstoreClientAPI {
} ], contentType=application/json}, {example=<Pet>
<id>123456</id>
<name>doggie</name>
<photoUrls>string</photoUrls>
<photoUrls>
<photoUrls>string</photoUrls>
</photoUrls>
<tags>
</tags>
<status>string</status>
</Pet>, contentType=application/xml}]
:param: status (query) Status values that need to be considered for filter
- parameter status: (query) Status values that need to be considered for filter
:returns: Promise<Response<[Pet]>>
- returns: RequestBuilder<[Pet]>
*/
func findPetsByStatus(#status: [String]?) -> RequestBuilder<[Pet]> {
public class func findPetsByStatus(status status: [String]?) -> RequestBuilder<[Pet]> {
let path = "/pet/findByStatus"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [
"status": status
@@ -125,7 +133,7 @@ extension PetstoreClientAPI {
let requestBuilder: RequestBuilder<[Pet]>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "GET", URLString: url, parameters: parameters, isBody: false)
return requestBuilder.init(method: "GET", URLString: URLString, parameters: parameters, isBody: false)
}
/**
@@ -153,7 +161,11 @@ extension PetstoreClientAPI {
} ], contentType=application/json}, {example=<Pet>
<id>123456</id>
<name>doggie</name>
<photoUrls>string</photoUrls>
<photoUrls>
<photoUrls>string</photoUrls>
</photoUrls>
<tags>
</tags>
<status>string</status>
</Pet>, contentType=application/xml}]
- examples: [{example=[ {
@@ -172,17 +184,21 @@ extension PetstoreClientAPI {
} ], contentType=application/json}, {example=<Pet>
<id>123456</id>
<name>doggie</name>
<photoUrls>string</photoUrls>
<photoUrls>
<photoUrls>string</photoUrls>
</photoUrls>
<tags>
</tags>
<status>string</status>
</Pet>, contentType=application/xml}]
:param: tags (query) Tags to filter by
- parameter tags: (query) Tags to filter by
:returns: Promise<Response<[Pet]>>
- returns: RequestBuilder<[Pet]>
*/
func findPetsByTags(#tags: [String]?) -> RequestBuilder<[Pet]> {
public class func findPetsByTags(tags tags: [String]?) -> RequestBuilder<[Pet]> {
let path = "/pet/findByTags"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [
"tags": tags
@@ -191,7 +207,7 @@ extension PetstoreClientAPI {
let requestBuilder: RequestBuilder<[Pet]>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "GET", URLString: url, parameters: parameters, isBody: false)
return requestBuilder.init(method: "GET", URLString: URLString, parameters: parameters, isBody: false)
}
/**
@@ -222,7 +238,11 @@ extension PetstoreClientAPI {
}, contentType=application/json}, {example=<Pet>
<id>123456</id>
<name>doggie</name>
<photoUrls>string</photoUrls>
<photoUrls>
<photoUrls>string</photoUrls>
</photoUrls>
<tags>
</tags>
<status>string</status>
</Pet>, contentType=application/xml}]
- examples: [{example={
@@ -241,25 +261,29 @@ extension PetstoreClientAPI {
}, contentType=application/json}, {example=<Pet>
<id>123456</id>
<name>doggie</name>
<photoUrls>string</photoUrls>
<photoUrls>
<photoUrls>string</photoUrls>
</photoUrls>
<tags>
</tags>
<status>string</status>
</Pet>, contentType=application/xml}]
:param: petId (path) ID of pet that needs to be fetched
- parameter petId: (path) ID of pet that needs to be fetched
:returns: Promise<Response<Pet>>
- returns: RequestBuilder<Pet>
*/
func getPetById(#petId: Int) -> RequestBuilder<Pet> {
public class func getPetById(petId petId: Int) -> RequestBuilder<Pet> {
var path = "/pet/{petId}"
path = path.stringByReplacingOccurrencesOfString("{petId}", withString: "\(petId)", options: .LiteralSearch, range: nil)
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [:]
let parameters = APIHelper.rejectNil(nillableParameters)
let requestBuilder: RequestBuilder<Pet>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "GET", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "GET", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -272,23 +296,26 @@ extension PetstoreClientAPI {
- type: oauth2
- name: petstore_auth
:param: petId (path) ID of pet that needs to be updated
:param: name (form) Updated name of the pet
:param: status (form) Updated status of the pet
- parameter petId: (path) ID of pet that needs to be updated
- parameter name: (form) Updated name of the pet
- parameter status: (form) Updated status of the pet
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func updatePetWithForm(#petId: String, name: String?, status: String?) -> RequestBuilder<Void> {
public class func updatePetWithForm(petId petId: String, name: String?, status: String?) -> RequestBuilder<Void> {
var path = "/pet/{petId}"
path = path.stringByReplacingOccurrencesOfString("{petId}", withString: "\(petId)", options: .LiteralSearch, range: nil)
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [:]
let nillableParameters: [String:AnyObject?] = [
"name": name,
"status": status
]
let parameters = APIHelper.rejectNil(nillableParameters)
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "POST", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "POST", URLString: URLString, parameters: parameters, isBody: false)
}
/**
@@ -301,21 +328,21 @@ extension PetstoreClientAPI {
- type: oauth2
- name: petstore_auth
:param: petId (path) Pet id to delete
- parameter petId: (path) Pet id to delete
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func deletePet(#petId: Int) -> RequestBuilder<Void> {
public class func deletePet(petId petId: Int) -> RequestBuilder<Void> {
var path = "/pet/{petId}"
path = path.stringByReplacingOccurrencesOfString("{petId}", withString: "\(petId)", options: .LiteralSearch, range: nil)
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [:]
let parameters = APIHelper.rejectNil(nillableParameters)
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "DELETE", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "DELETE", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -328,23 +355,26 @@ extension PetstoreClientAPI {
- type: oauth2
- name: petstore_auth
:param: petId (path) ID of pet to update
:param: additionalMetadata (form) Additional data to pass to server
:param: file (form) file to upload
- parameter petId: (path) ID of pet to update
- parameter additionalMetadata: (form) Additional data to pass to server
- parameter file: (form) file to upload
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func uploadFile(#petId: Int, additionalMetadata: String?, file: NSData?) -> RequestBuilder<Void> {
public class func uploadFile(petId petId: Int, additionalMetadata: String?, file: NSURL?) -> RequestBuilder<Void> {
var path = "/pet/{petId}/uploadImage"
path = path.stringByReplacingOccurrencesOfString("{petId}", withString: "\(petId)", options: .LiteralSearch, range: nil)
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [:]
let nillableParameters: [String:AnyObject?] = [
"additionalMetadata": additionalMetadata,
"file": file
]
let parameters = APIHelper.rejectNil(nillableParameters)
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "POST", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "POST", URLString: URLString, parameters: parameters, isBody: false)
}
}
@@ -10,7 +10,7 @@ import PromiseKit
extension PetstoreClientAPI {
class StoreAPI: APIBase {
public class StoreAPI: APIBase {
/**
@@ -23,23 +23,23 @@ extension PetstoreClientAPI {
- name: api_key
- examples: [{example={
"key" : 123
}, contentType=application/json}, {example=not implemented io.swagger.models.properties.MapProperty@3e, contentType=application/xml}]
}, contentType=application/json}, {example=not implemented io.swagger.models.properties.MapProperty@d1e580af, contentType=application/xml}]
- examples: [{example={
"key" : 123
}, contentType=application/json}, {example=not implemented io.swagger.models.properties.MapProperty@3e, contentType=application/xml}]
}, contentType=application/json}, {example=not implemented io.swagger.models.properties.MapProperty@d1e580af, contentType=application/xml}]
:returns: Promise<Response<[String:Int]>>
- returns: RequestBuilder<[String:Int]>
*/
func getInventory() -> RequestBuilder<[String:Int]> {
public class func getInventory() -> RequestBuilder<[String:Int]> {
let path = "/store/inventory"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [:]
let parameters = APIHelper.rejectNil(nillableParameters)
let requestBuilder: RequestBuilder<[String:Int]>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "GET", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "GET", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -54,12 +54,12 @@ extension PetstoreClientAPI {
"complete" : true,
"status" : "aeiou",
"quantity" : 123,
"shipDate" : "2015-06-27T13:41:28.102+0000"
"shipDate" : "2015-10-20T06:12:32.347+0000"
}, contentType=application/json}, {example=<Order>
<id>123456</id>
<petId>123456</petId>
<quantity>0</quantity>
<shipDate>2015-06-27T22:41:28.105Z</shipDate>
<shipDate>2015-10-19T23:12:32.350Z</shipDate>
<status>string</status>
<complete>true</complete>
</Order>, contentType=application/xml}]
@@ -69,29 +69,29 @@ extension PetstoreClientAPI {
"complete" : true,
"status" : "aeiou",
"quantity" : 123,
"shipDate" : "2015-06-27T13:41:28.102+0000"
"shipDate" : "2015-10-20T06:12:32.347+0000"
}, contentType=application/json}, {example=<Order>
<id>123456</id>
<petId>123456</petId>
<quantity>0</quantity>
<shipDate>2015-06-27T22:41:28.105Z</shipDate>
<shipDate>2015-10-19T23:12:32.350Z</shipDate>
<status>string</status>
<complete>true</complete>
</Order>, contentType=application/xml}]
:param: body (body) order placed for purchasing the pet
- parameter body: (body) order placed for purchasing the pet
:returns: Promise<Response<Order>>
- returns: RequestBuilder<Order>
*/
func placeOrder(#body: Order?) -> RequestBuilder<Order> {
public class func placeOrder(body body: Order?) -> RequestBuilder<Order> {
let path = "/store/order"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let parameters = body?.encodeToJSON() as? [String:AnyObject]
let requestBuilder: RequestBuilder<Order>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "POST", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "POST", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -106,12 +106,12 @@ extension PetstoreClientAPI {
"complete" : true,
"status" : "aeiou",
"quantity" : 123,
"shipDate" : "2015-06-27T13:41:28.106+0000"
"shipDate" : "2015-10-20T06:12:32.351+0000"
}, contentType=application/json}, {example=<Order>
<id>123456</id>
<petId>123456</petId>
<quantity>0</quantity>
<shipDate>2015-06-27T22:41:28.106Z</shipDate>
<shipDate>2015-10-19T23:12:32.351Z</shipDate>
<status>string</status>
<complete>true</complete>
</Order>, contentType=application/xml}]
@@ -121,31 +121,31 @@ extension PetstoreClientAPI {
"complete" : true,
"status" : "aeiou",
"quantity" : 123,
"shipDate" : "2015-06-27T13:41:28.106+0000"
"shipDate" : "2015-10-20T06:12:32.351+0000"
}, contentType=application/json}, {example=<Order>
<id>123456</id>
<petId>123456</petId>
<quantity>0</quantity>
<shipDate>2015-06-27T22:41:28.106Z</shipDate>
<shipDate>2015-10-19T23:12:32.351Z</shipDate>
<status>string</status>
<complete>true</complete>
</Order>, contentType=application/xml}]
:param: orderId (path) ID of pet that needs to be fetched
- parameter orderId: (path) ID of pet that needs to be fetched
:returns: Promise<Response<Order>>
- returns: RequestBuilder<Order>
*/
func getOrderById(#orderId: String) -> RequestBuilder<Order> {
public class func getOrderById(orderId orderId: String) -> RequestBuilder<Order> {
var path = "/store/order/{orderId}"
path = path.stringByReplacingOccurrencesOfString("{orderId}", withString: "\(orderId)", options: .LiteralSearch, range: nil)
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [:]
let parameters = APIHelper.rejectNil(nillableParameters)
let requestBuilder: RequestBuilder<Order>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "GET", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "GET", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -155,21 +155,21 @@ extension PetstoreClientAPI {
- DELETE /store/order/{orderId}
- For valid response try integer IDs with value < 1000. Anything above 1000 or nonintegers will generate API errors
:param: orderId (path) ID of the order that needs to be deleted
- parameter orderId: (path) ID of the order that needs to be deleted
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func deleteOrder(#orderId: String) -> RequestBuilder<Void> {
public class func deleteOrder(orderId orderId: String) -> RequestBuilder<Void> {
var path = "/store/order/{orderId}"
path = path.stringByReplacingOccurrencesOfString("{orderId}", withString: "\(orderId)", options: .LiteralSearch, range: nil)
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [:]
let parameters = APIHelper.rejectNil(nillableParameters)
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "DELETE", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "DELETE", URLString: URLString, parameters: parameters, isBody: true)
}
}
@@ -10,7 +10,7 @@ import PromiseKit
extension PetstoreClientAPI {
class UserAPI: APIBase {
public class UserAPI: APIBase {
/**
@@ -19,19 +19,19 @@ extension PetstoreClientAPI {
- POST /user
- This can only be done by the logged in user.
:param: body (body) Created user object
- parameter body: (body) Created user object
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func createUser(#body: User?) -> RequestBuilder<Void> {
public class func createUser(body body: User?) -> RequestBuilder<Void> {
let path = "/user"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let parameters = body?.encodeToJSON() as? [String:AnyObject]
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "POST", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "POST", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -41,19 +41,19 @@ extension PetstoreClientAPI {
- POST /user/createWithArray
-
:param: body (body) List of user object
- parameter body: (body) List of user object
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func createUsersWithArrayInput(#body: [User]?) -> RequestBuilder<Void> {
public class func createUsersWithArrayInput(body body: [User]?) -> RequestBuilder<Void> {
let path = "/user/createWithArray"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let parameters = body?.encodeToJSON() as? [String:AnyObject]
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "POST", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "POST", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -63,19 +63,19 @@ extension PetstoreClientAPI {
- POST /user/createWithList
-
:param: body (body) List of user object
- parameter body: (body) List of user object
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func createUsersWithListInput(#body: [User]?) -> RequestBuilder<Void> {
public class func createUsersWithListInput(body body: [User]?) -> RequestBuilder<Void> {
let path = "/user/createWithList"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let parameters = body?.encodeToJSON() as? [String:AnyObject]
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "POST", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "POST", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -87,14 +87,14 @@ extension PetstoreClientAPI {
- examples: [{example="aeiou", contentType=application/json}, {example=string, contentType=application/xml}]
- examples: [{example="aeiou", contentType=application/json}, {example=string, contentType=application/xml}]
:param: username (query) The user name for login
:param: password (query) The password for login in clear text
- parameter username: (query) The user name for login
- parameter password: (query) The password for login in clear text
:returns: Promise<Response<String>>
- returns: RequestBuilder<String>
*/
func loginUser(#username: String?, password: String?) -> RequestBuilder<String> {
public class func loginUser(username username: String?, password: String?) -> RequestBuilder<String> {
let path = "/user/login"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [
"username": username,
@@ -104,7 +104,7 @@ extension PetstoreClientAPI {
let requestBuilder: RequestBuilder<String>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "GET", URLString: url, parameters: parameters, isBody: false)
return requestBuilder.init(method: "GET", URLString: URLString, parameters: parameters, isBody: false)
}
/**
@@ -114,18 +114,18 @@ extension PetstoreClientAPI {
- GET /user/logout
-
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func logoutUser() -> RequestBuilder<Void> {
public class func logoutUser() -> RequestBuilder<Void> {
let path = "/user/logout"
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [:]
let parameters = APIHelper.rejectNil(nillableParameters)
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "GET", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "GET", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -145,21 +145,21 @@ extension PetstoreClientAPI {
"userStatus" : 0
}, contentType=application/json}]
:param: username (path) The name that needs to be fetched. Use user1 for testing.
- parameter username: (path) The name that needs to be fetched. Use user1 for testing.
:returns: Promise<Response<User>>
- returns: RequestBuilder<User>
*/
func getUserByName(#username: String) -> RequestBuilder<User> {
public class func getUserByName(username username: String) -> RequestBuilder<User> {
var path = "/user/{username}"
path = path.stringByReplacingOccurrencesOfString("{username}", withString: "\(username)", options: .LiteralSearch, range: nil)
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [:]
let parameters = APIHelper.rejectNil(nillableParameters)
let requestBuilder: RequestBuilder<User>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "GET", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "GET", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -169,21 +169,21 @@ extension PetstoreClientAPI {
- PUT /user/{username}
- This can only be done by the logged in user.
:param: username (path) name that need to be deleted
:param: body (body) Updated user object
- parameter username: (path) name that need to be deleted
- parameter body: (body) Updated user object
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func updateUser(#username: String, body: User?) -> RequestBuilder<Void> {
public class func updateUser(username username: String, body: User?) -> RequestBuilder<Void> {
var path = "/user/{username}"
path = path.stringByReplacingOccurrencesOfString("{username}", withString: "\(username)", options: .LiteralSearch, range: nil)
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let parameters = body?.encodeToJSON() as? [String:AnyObject]
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "PUT", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "PUT", URLString: URLString, parameters: parameters, isBody: true)
}
/**
@@ -193,21 +193,21 @@ extension PetstoreClientAPI {
- DELETE /user/{username}
- This can only be done by the logged in user.
:param: username (path) The name that needs to be deleted
- parameter username: (path) The name that needs to be deleted
:returns: Promise<Response<Void>>
- returns: RequestBuilder<Void>
*/
func deleteUser(#username: String) -> RequestBuilder<Void> {
public class func deleteUser(username username: String) -> RequestBuilder<Void> {
var path = "/user/{username}"
path = path.stringByReplacingOccurrencesOfString("{username}", withString: "\(username)", options: .LiteralSearch, range: nil)
let url = PetstoreClientAPI.basePath + path
let URLString = PetstoreClientAPI.basePath + path
let nillableParameters: [String:AnyObject?] = [:]
let parameters = APIHelper.rejectNil(nillableParameters)
let requestBuilder: RequestBuilder<Void>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()
return requestBuilder(method: "DELETE", URLString: url, parameters: parameters, isBody: true)
return requestBuilder.init(method: "DELETE", URLString: URLString, parameters: parameters, isBody: true)
}
}
@@ -20,7 +20,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody)
}
override func execute(completion: (response: Response<T>?, erorr: NSError?) -> Void) {
override func execute(completion: (response: Response<T>?, erorr: ErrorType?) -> Void) {
let managerId = NSUUID().UUIDString
// Create a new manager for each request to customize its request header
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
@@ -29,23 +29,57 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
managerStore[managerId] = manager
let encoding = isBody ? Alamofire.ParameterEncoding.JSON : Alamofire.ParameterEncoding.URL
let request = manager.request(Alamofire.Method(rawValue: method)!, URLString, parameters: parameters, encoding: encoding)
let xMethod = Alamofire.Method(rawValue: method)
let fileKeys = parameters == nil ? [] : parameters!.filter { $1.isKindOfClass(NSURL) }
.map { $0.0 }
if fileKeys.count > 0 {
manager.upload(
xMethod!, URLString, headers: nil,
multipartFormData: { mpForm in
for (k, v) in self.parameters! {
switch v {
case let fileURL as NSURL:
mpForm.appendBodyPart(fileURL: fileURL, name: k)
break
case let string as NSString:
mpForm.appendBodyPart(data: string.dataUsingEncoding(NSUTF8StringEncoding)!, name: k)
break
case let number as NSNumber:
mpForm.appendBodyPart(data: number.stringValue.dataUsingEncoding(NSUTF8StringEncoding)!, name: k)
break
default:
fatalError("Unprocessable value \(v) with key \(k)")
break
}
}
},
encodingMemoryThreshold: Manager.MultipartFormDataEncodingMemoryThreshold,
encodingCompletion: { encodingResult in
switch encodingResult {
case .Success(let upload, _, _):
self.processRequest(upload, managerId, completion)
case .Failure(let encodingError):
completion(response: nil, erorr: encodingError)
}
}
)
} else {
processRequest(manager.request(xMethod!, URLString, parameters: parameters, encoding: encoding), managerId, completion)
}
}
private func processRequest(request: Request, _ managerId: String, _ completion: (response: Response<T>?, erorr: ErrorType?) -> Void) {
if let credential = self.credential {
request.authenticate(usingCredential: credential)
}
request.responseJSON(options: .AllowFragments) { (req, res, json, error) in
request.responseJSON(options: .AllowFragments) { (req, res, result) in
managerStore.removeValueForKey(managerId)
if let error = error {
completion(response: nil, erorr: error)
return
}
if res!.statusCode >= 400 {
//TODO: Add error entity
let userInfo: [NSObject : AnyObject] = (json != nil) ? ["data": json!] : [:]
let error = NSError(domain: res!.URL!.URLString, code: res!.statusCode, userInfo: userInfo)
completion(response: nil, erorr: error)
if result.isFailure {
completion(response: nil, erorr: result.error)
return
}
@@ -54,7 +88,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
completion(response: response, erorr: nil)
return
}
if let json: AnyObject = json {
if let json: AnyObject = result.value {
let body = Decoders.decode(clazz: T.self, source: json)
let response = Response(response: res!, body: body)
completion(response: response, erorr: nil)
@@ -66,7 +100,7 @@ class AlamofireRequestBuilder<T>: RequestBuilder<T> {
completion(response: response, erorr: nil)
return
}
completion(response: nil, erorr: NSError(domain: "localhost", code: 500, userInfo: ["reason": "unreacheable code"]))
}
}
@@ -65,7 +65,7 @@ extension NSDate: JSONEncodable {
}
extension RequestBuilder {
func execute() -> Promise<Response<T>> {
public func execute() -> Promise<Response<T>> {
let deferred = Promise<Response<T>>.defer()
self.execute { (response: Response<T>?, error: NSError?) in
if let response = response {
@@ -10,18 +10,18 @@ protocol JSONEncodable {
func encodeToJSON() -> AnyObject
}
class Response<T> {
let statusCode: Int
let header: [String: String]
let body: T
public class Response<T> {
public let statusCode: Int
public let header: [String: String]
public let body: T
init(statusCode: Int, header: [String: String], body: T) {
public init(statusCode: Int, header: [String: String], body: T) {
self.statusCode = statusCode
self.header = header
self.body = body
}
convenience init(response: NSHTTPURLResponse, body: T) {
public convenience init(response: NSHTTPURLResponse, body: T) {
let rawHeader = response.allHeaderFields
var header = [String:String]()
for (key, value) in rawHeader {
@@ -35,17 +35,17 @@ private var once = dispatch_once_t()
class Decoders {
static private var decoders = Dictionary<String, ((AnyObject) -> AnyObject)>()
static func addDecoder<T>(#clazz: T.Type, decoder: ((AnyObject) -> T)) {
static func addDecoder<T>(clazz clazz: T.Type, decoder: ((AnyObject) -> T)) {
let key = "\(T.self)"
decoders[key] = { decoder($0) as! AnyObject }
}
static func decode<T>(#clazz: [T].Type, source: AnyObject) -> [T] {
static func decode<T>(clazz clazz: [T].Type, source: AnyObject) -> [T] {
let array = source as! [AnyObject]
return array.map { Decoders.decode(clazz: T.self, source: $0) }
}
static func decode<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
static func decode<T, Key: Hashable>(clazz clazz: [Key:T].Type, source: AnyObject) -> [Key:T] {
let sourceDictinoary = source as! [Key: AnyObject]
var dictionary = [Key:T]()
for (key, value) in sourceDictinoary {
@@ -54,7 +54,7 @@ class Decoders {
return dictionary
}
static func decode<T>(#clazz: T.Type, source: AnyObject) -> T {
static func decode<T>(clazz clazz: T.Type, source: AnyObject) -> T {
initialize()
if source is T {
return source as! T
@@ -68,7 +68,7 @@ class Decoders {
}
}
static func decodeOptional<T>(#clazz: T.Type, source: AnyObject?) -> T? {
static func decodeOptional<T>(clazz clazz: T.Type, source: AnyObject?) -> T? {
if source is NSNull {
return nil
}
@@ -77,7 +77,7 @@ class Decoders {
}
}
static func decodeOptional<T>(#clazz: [T].Type, source: AnyObject?) -> [T]? {
static func decodeOptional<T>(clazz clazz: [T].Type, source: AnyObject?) -> [T]? {
if source is NSNull {
return nil
}
@@ -86,7 +86,7 @@ class Decoders {
}
}
static func decodeOptional<T, Key: Hashable>(#clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
static func decodeOptional<T, Key: Hashable>(clazz clazz: [Key:T].Type, source: AnyObject?) -> [Key:T]? {
if source is NSNull {
return nil
}
@@ -100,6 +100,7 @@ class Decoders {
let formatters = [
"yyyy-MM-dd",
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
"yyyy-MM-dd'T'HH:mm:ss'Z'"
].map { (format: String) -> NSDateFormatter in
let formatter = NSDateFormatter()
@@ -120,7 +121,7 @@ class Decoders {
// Decoder for User
Decoders.addDecoder(clazz: User.self) { (source: AnyObject) -> User in
let sourceDictionary = source as! [NSObject:AnyObject]
var instance = User()
let instance = User()
instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
instance.username = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["username"])
instance.firstName = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["firstName"])
@@ -136,7 +137,7 @@ class Decoders {
// Decoder for Category
Decoders.addDecoder(clazz: Category.self) { (source: AnyObject) -> Category in
let sourceDictionary = source as! [NSObject:AnyObject]
var instance = Category()
let instance = Category()
instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
instance.name = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["name"])
return instance
@@ -146,7 +147,7 @@ class Decoders {
// Decoder for Pet
Decoders.addDecoder(clazz: Pet.self) { (source: AnyObject) -> Pet in
let sourceDictionary = source as! [NSObject:AnyObject]
var instance = Pet()
let instance = Pet()
instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
instance.category = Decoders.decodeOptional(clazz: Category.self, source: sourceDictionary["category"])
instance.name = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["name"])
@@ -160,7 +161,7 @@ class Decoders {
// Decoder for Tag
Decoders.addDecoder(clazz: Tag.self) { (source: AnyObject) -> Tag in
let sourceDictionary = source as! [NSObject:AnyObject]
var instance = Tag()
let instance = Tag()
instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
instance.name = Decoders.decodeOptional(clazz: String.self, source: sourceDictionary["name"])
return instance
@@ -170,7 +171,7 @@ class Decoders {
// Decoder for Order
Decoders.addDecoder(clazz: Order.self) { (source: AnyObject) -> Order in
let sourceDictionary = source as! [NSObject:AnyObject]
var instance = Order()
let instance = Order()
instance.id = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["id"])
instance.petId = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["petId"])
instance.quantity = Decoders.decodeOptional(clazz: Int.self, source: sourceDictionary["quantity"])
@@ -8,10 +8,10 @@
import Foundation
class Category: JSONEncodable {
public class Category: JSONEncodable {
var id: Int?
var name: String?
public var id: Int?
public var name: String?
// MARK: JSONEncodable
@@ -8,21 +8,21 @@
import Foundation
class Order: JSONEncodable {
public class Order: JSONEncodable {
enum Status: String {
public enum Status: String {
case Placed = "placed"
case Approved = "approved"
case Delivered = "delivered"
}
var id: Int?
var petId: Int?
var quantity: Int?
var shipDate: NSDate?
public var id: Int?
public var petId: Int?
public var quantity: Int?
public var shipDate: NSDate?
/** Order Status */
var status: Status?
var complete: Bool?
public var status: Status?
public var complete: Bool?
// MARK: JSONEncodable
@@ -8,21 +8,21 @@
import Foundation
class Pet: JSONEncodable {
public class Pet: JSONEncodable {
enum Status: String {
public enum Status: String {
case Available = "available"
case Pending = "pending"
case Sold = "sold"
}
var id: Int?
var category: Category?
var name: String?
var photoUrls: [String]?
var tags: [Tag]?
public var id: Int?
public var category: Category?
public var name: String?
public var photoUrls: [String]?
public var tags: [Tag]?
/** pet status in the store */
var status: Status?
public var status: Status?
// MARK: JSONEncodable
@@ -8,10 +8,10 @@
import Foundation
class Tag: JSONEncodable {
public class Tag: JSONEncodable {
var id: Int?
var name: String?
public var id: Int?
public var name: String?
// MARK: JSONEncodable
@@ -8,17 +8,17 @@
import Foundation
class User: JSONEncodable {
public class User: JSONEncodable {
var id: Int?
var username: String?
var firstName: String?
var lastName: String?
var email: String?
var password: String?
var phone: String?
public var id: Int?
public var username: String?
public var firstName: String?
public var lastName: String?
public var email: String?
public var password: String?
public var phone: String?
/** User Status */
var userStatus: Int?
public var userStatus: Int?
// MARK: JSONEncodable