Created
July 11, 2017 02:53
-
-
Save mosluce/8b73bff5911305de4e7f27514dc2dbc7 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // API.swift | |
| // KLCC-SIGNBOOK | |
| // | |
| // Created by 默司 on 2017/7/4. | |
| // Copyright © 2017年 默司. All rights reserved. | |
| // | |
| import UIKit | |
| import RxSwift | |
| enum HttpMethod: String { | |
| case post = "POST" | |
| case get = "GET" | |
| case put = "PUT" | |
| case delete = "DELETE" | |
| } | |
| enum HttpError: Error { | |
| case bad(String?) | |
| case unauthorized | |
| case unknown | |
| } | |
| class API: NSObject { | |
| static var shared: API { | |
| return API() | |
| } | |
| private var baseUrl = "http://mobi.klcc.com.tw/api" | |
| func request(_ method: HttpMethod = .get, _ path: String, headers: [String: String] = [:], params: [String: Any]? = nil) -> Observable<Any> { | |
| let url = URL(string: baseUrl.appending(path)) | |
| var req = URLRequest(url: url!) | |
| req.httpMethod = method.rawValue | |
| if let params = params, let data = try? JSONSerialization.data(withJSONObject: params, options: .prettyPrinted) { | |
| req.httpBody = data | |
| } | |
| headers.forEach { (key, val) in | |
| req.setValue(val, forHTTPHeaderField: key) | |
| } | |
| req.setValue("application/json", forHTTPHeaderField: "Content-Type") | |
| if let token = Auth.shared.token.value { | |
| req.setValue(token, forHTTPHeaderField: "token") | |
| } | |
| let response = URLSession.shared.rx | |
| .response(request: req) | |
| .map({ (res, data) -> Any in | |
| guard res.statusCode == 200 else { | |
| switch res.statusCode { | |
| case 400: | |
| throw HttpError.bad("bad request") | |
| case 401: | |
| throw HttpError.unauthorized | |
| default: | |
| throw HttpError.unknown | |
| } | |
| } | |
| guard let json = (try? JSONSerialization.jsonObject(with: data, options: .mutableContainers)) as? [String: Any] else { | |
| throw HttpError.unknown | |
| } | |
| guard let success = json[bool: "success"], success else { | |
| throw HttpError.bad(json[string: "message"]) | |
| } | |
| return json["data"] ?? [:] | |
| }) | |
| .catchError({ (error) -> Observable<Any> in | |
| Auth.shared.logout() | |
| return Observable.error(error) | |
| }) | |
| .observeOn(MainScheduler.instance) | |
| return response | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // Auth.swift | |
| // KLCC-SIGNBOOK | |
| // | |
| // Created by 默司 on 2017/7/10. | |
| // Copyright © 2017年 默司. All rights reserved. | |
| // | |
| import UIKit | |
| import RxSwift | |
| class Auth: NSObject { | |
| private let domainName = "89248531" | |
| var token: Variable<String?> | |
| var user: Variable<User?> = Variable(nil) | |
| public static var shared: Auth { | |
| return Auth() | |
| } | |
| override init() { | |
| self.token = Variable(nil) | |
| self.user = Variable(nil) | |
| } | |
| func logout() { | |
| Auth.shared.token.value = nil | |
| Auth.shared.user.value = nil | |
| } | |
| func login(account: String, password: String) { | |
| let response = API.shared.request(.post, "/auth/login", params: ["account": account, "password": password, "domainName": domainName]) | |
| response.subscribe(onNext: { (result) in | |
| print(result) | |
| }).addDisposableTo(AppDelegate.disposeBag) | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // | |
| // NSDictionary.swift | |
| // KLCC-SIGNBOOK | |
| // | |
| // Created by 默司 on 2017/7/10. | |
| // Copyright © 2017年 默司. All rights reserved. | |
| // | |
| import UIKit | |
| extension Dictionary { | |
| subscript(string key: Key) -> String? { | |
| return self[key] as? String ?? nil | |
| } | |
| subscript(bool key: Key) -> Bool? { | |
| return self[key] as? Bool ?? nil | |
| } | |
| subscript(float key: Key) -> Float? { | |
| return self[key] as? Float ?? nil | |
| } | |
| subscript(double key: Key) -> Double? { | |
| return self[key] as? Double ?? nil | |
| } | |
| subscript(int key: Key) -> Int? { | |
| return self[key] as? Int ?? nil | |
| } | |
| subscript(array key: Key) -> Array<Any>? { | |
| return self[key] as? Array<Any> ?? nil | |
| } | |
| subscript(dictionary key: Key) -> Dictionary<AnyHashable, Any>? { | |
| return self[key] as? Dictionary ?? nil | |
| } | |
| } | |
| extension Array { | |
| subscript(string key: Index) -> String? { | |
| return self[key] as? String ?? nil | |
| } | |
| subscript(bool key: Index) -> Bool? { | |
| return self[key] as? Bool ?? nil | |
| } | |
| subscript(float key: Index) -> Float? { | |
| return self[key] as? Float ?? nil | |
| } | |
| subscript(double key: Index) -> Double? { | |
| return self[key] as? Double ?? nil | |
| } | |
| subscript(int key: Index) -> Int? { | |
| return self[key] as? Int ?? nil | |
| } | |
| subscript(array key: Index) -> Array<Any>? { | |
| return self[key] as? Array<Any> ?? nil | |
| } | |
| subscript(dictionary key: Index) -> Dictionary<AnyHashable, Any>? { | |
| return self[key] as? Dictionary ?? nil | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment