Skip to content

Instantly share code, notes, and snippets.

@linktohack
Last active July 11, 2018 09:15
Show Gist options
  • Save linktohack/d80422849a984110d15fee8966a693e6 to your computer and use it in GitHub Desktop.
Save linktohack/d80422849a984110d15fee8966a693e6 to your computer and use it in GitHub Desktop.
Swagger codegen for loopback + Swift
diff --git a/Hello/Api/SwaggerClient/Classes/Swaggers/APIs/AccountAPI.swift b/Hello/Api/SwaggerClient/Classes/Swaggers/APIs/AccountAPI.swift
index 00c997c..1aa62f8 100644
--- a/Hello/Api/SwaggerClient/Classes/Swaggers/APIs/AccountAPI.swift
+++ b/Hello/Api/SwaggerClient/Classes/Swaggers/APIs/AccountAPI.swift
@@ -788,7 +788,7 @@ open class AccountAPI {
- parameter include: (query) Related objects to include in the response. See the description of return value for more details. (optional)
- parameter completion: completion handler to receive the data and the error objects
*/
- open class func accountLogin(credentials: String, include: String? = nil, completion: @escaping ((_ data: Any?,_ error: Error?) -> Void)) {
+ open class func accountLogin(credentials: Any, include: String? = nil, completion: @escaping ((_ data: Any?,_ error: Error?) -> Void)) {
accountLoginWithRequestBuilder(credentials: credentials, include: include).execute { (response, error) -> Void in
completion(response?.body, error);
}
@@ -801,7 +801,7 @@ open class AccountAPI {
- parameter include: (query) Related objects to include in the response. See the description of return value for more details. (optional)
- returns: Observable<Any>
*/
- open class func accountLogin(credentials: String, include: String? = nil) -> Observable<Any> {
+ open class func accountLogin(credentials: Any, include: String? = nil) -> Observable<Any> {
return Observable.create { observer -> Disposable in
accountLogin(credentials: credentials, include: include) { data, error in
if let error = error {
@@ -825,7 +825,7 @@ open class AccountAPI {
- returns: RequestBuilder<String>
*/
- open class func accountLoginWithRequestBuilder(credentials: String, include: String? = nil) -> RequestBuilder<String> {
+ open class func accountLoginWithRequestBuilder(credentials: Any, include: String? = nil) -> RequestBuilder<String> {
let path = "/Accounts/login"
let URLString = SwaggerClientAPI.basePath + path
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: credentials)
@@ -4477,7 +4477,7 @@ open class AccountAPI {
- parameter options: (body)
- parameter completion: completion handler to receive the data and the error objects
*/
- open class func accountResetPassword(options: String, completion: @escaping ((_ error: Error?) -> Void)) {
+ open class func accountResetPassword(options: Any, completion: @escaping ((_ error: Error?) -> Void)) {
accountResetPasswordWithRequestBuilder(options: options).execute { (response, error) -> Void in
completion(error);
}
@@ -4489,7 +4489,7 @@ open class AccountAPI {
- parameter options: (body)
- returns: Observable<Void>
*/
- open class func accountResetPassword(options: String) -> Observable<Void> {
+ open class func accountResetPassword(options: Any) -> Observable<Void> {
return Observable.create { observer -> Disposable in
accountResetPassword(options: options) { error in
if let error = error {
@@ -4511,7 +4511,7 @@ open class AccountAPI {
- returns: RequestBuilder<Void>
*/
- open class func accountResetPasswordWithRequestBuilder(options: String) -> RequestBuilder<Void> {
+ open class func accountResetPasswordWithRequestBuilder(options: Any) -> RequestBuilder<Void> {
let path = "/Accounts/reset"
let URLString = SwaggerClientAPI.basePath + path
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: options)
diff --git a/Hello/Api/SwaggerClient/Classes/Swaggers/APIs/ContainerAPI.swift b/Hello/Api/SwaggerClient/Classes/Swaggers/APIs/ContainerAPI.swift
index b2397d2..4bc392a 100644
--- a/Hello/Api/SwaggerClient/Classes/Swaggers/APIs/ContainerAPI.swift
+++ b/Hello/Api/SwaggerClient/Classes/Swaggers/APIs/ContainerAPI.swift
@@ -17,7 +17,7 @@ open class ContainerAPI {
- parameter options: (body) (optional)
- parameter completion: completion handler to receive the data and the error objects
*/
- open class func containerCreateContainer(options: String? = nil, completion: @escaping ((_ data: Any?,_ error: Error?) -> Void)) {
+ open class func containerCreateContainer(options: Any? = nil, completion: @escaping ((_ data: Any?,_ error: Error?) -> Void)) {
containerCreateContainerWithRequestBuilder(options: options).execute { (response, error) -> Void in
completion(response?.body, error);
}
@@ -28,7 +28,7 @@ open class ContainerAPI {
- parameter options: (body) (optional)
- returns: Observable<Any>
*/
- open class func containerCreateContainer(options: String? = nil) -> Observable<Any> {
+ open class func containerCreateContainer(options: Any? = nil) -> Observable<Any> {
return Observable.create { observer -> Disposable in
containerCreateContainer(options: options) { data, error in
if let error = error {
@@ -50,7 +50,7 @@ open class ContainerAPI {
- returns: RequestBuilder<String>
*/
- open class func containerCreateContainerWithRequestBuilder(options: String? = nil) -> RequestBuilder<String> {
+ open class func containerCreateContainerWithRequestBuilder(options: Any? = nil) -> RequestBuilder<String> {
let path = "/Containers"
let URLString = SwaggerClientAPI.basePath + path
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: options)
diff --git a/Hello/Api/SwaggerClient/Classes/Swaggers/CodableHelper.swift b/Hello/Api/SwaggerClient/Classes/Swaggers/CodableHelper.swift
index 5cbf3d9..323715c 100644
--- a/Hello/Api/SwaggerClient/Classes/Swaggers/CodableHelper.swift
+++ b/Hello/Api/SwaggerClient/Classes/Swaggers/CodableHelper.swift
@@ -40,10 +40,6 @@ open class CodableHelper {
}
open class func encode<T>(_ value: T, prettyPrint: Bool = false) -> EncodeResult where T : Encodable {
- if value is String {
- return ((value as! String).data(using: .utf8), nil)
- }
-
var returnedData: Data?
var returnedError: Error? = nil
codegen:
java -jar swagger-codegen-cli-2.3.1.jar generate -l swift4 -i def.yaml -o ~/Downloads/Hello/Hello/Api -DresponseAs=RxSwift
cd ~/Downloads/Hello/Hello && \
sed -i~ 's/: Any\?/: String?/g' Api/SwaggerClient/Classes/Swaggers/Models/*.swift && \
sed -i~ 's/Any\.self/String.self/g' Api/SwaggerClient/Classes/Swaggers/Models/*.swift && \
sed -i~ 's/XString/XAny/g' Api/SwaggerClient/Classes/Swaggers/Models/*.swift && \
sed -i~ 's/RequestBuilder<Any>/RequestBuilder<String>/g' Api/SwaggerClient/Classes/Swaggers/APIs/*.swift && \
if [ -f api.patch ]; then patch -p2 -R < api.patch; fi
extension Dictionary {
func asJSON() -> String? {
guard let data = try? JSONSerialization.data(withJSONObject: self, options: .prettyPrinted) else {
return nil
}
return String(data: data, encoding: .utf8)
}
}
extension String {
func asError() -> Error {
return NSError(domain: "", code: 0, userInfo: [NSLocalizedDescriptionKey: self])
}
}
let credentials = ["username": username.text ?? "",
"password": password.text ?? ""].asJSON()!
AccountAPI
.accountLogin(credentials: credentials)
.flatMap({ str -> Observable<Bool> in
guard let str = str as? String,
let data = str.data(using: .utf8) else {
throw "Shouldn't it crash?".asError()
}
let (token1, error) = CodableHelper.decode(AccountAccessToken.self, from: data)
guard let token = token1 else {
return Observable.error(error!)
}
UserDefaults.standard.set("token", forKey: "token")
SwaggerClientAPI.customHeaders["X-Access-Token"] = token.id
return .of(true)
})
.subscribe {
print("account")
self.performSegue(withIdentifier: "loginToMain", sender: self)
}
.disposed(by: bag)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment