Skip to content

Instantly share code, notes, and snippets.

@iSapozhnik
Last active January 13, 2017 11:54
Show Gist options
  • Save iSapozhnik/2be12abff878f1f7d8c9113cfa94eeeb to your computer and use it in GitHub Desktop.
Save iSapozhnik/2be12abff878f1f7d8c9113cfa94eeeb to your computer and use it in GitHub Desktop.
//
// APIService.swift
//
// Created by Sapozhnik Ivan on 10.06.16.
// Copyright © 2016 Sapozhnik Ivan. All rights reserved.
//
import Foundation
import Alamofire
import AlamofireObjectMapper
class APIService {
static func login(login: String, password: String, completion:(response: Response<Token, NSError>) -> Void) {
Alamofire.request(AppRouter.Login(login, password)).responseObject { response in
completion(response: response)
}
}
}
//
// AppRouter.swift
//
// Created by Sapozhnik Ivan on 09.06.16.
// Copyright © 2016 Sapozhnik Ivan. All rights reserved.
//
import Foundation
import Alamofire
enum AppRouter: URLRequestConvertible {
case Login(String, String)
case Logout
case State
case GetCategories
case UserDetails
case Store
case MyFeatures
case FeatureDetails(String) // Store feature
case MyFeatureDetails(String)
case BuyFeature(String)
case ChangeState(String, String)
case NotificationsSubscribe(String)
case NotificationsUnsubscribe(String)
var method: Alamofire.Method {
switch self {
case .State,
.GetCategories,
.UserDetails,
.Store,
.MyFeatures,
.FeatureDetails,
.MyFeatureDetails,
.Logout:
return .GET
case .Login, BuyFeature, .ChangeState, NotificationsSubscribe:
return .POST
case .NotificationsUnsubscribe:
return .DELETE
}
}
var path: String {
switch self {
case .Login(_,_):
return "user/login"
case .State:
return "user/state"
case .GetCategories:
return "feature/categories"
case .UserDetails:
return "user/details"
case .Store:
return "feature/features"
case .MyFeatures:
return "myfeature/myfeatures"
case .FeatureDetails(let featureId):
return "feature/features/\(featureId)"
case .MyFeatureDetails(let featureId):
return "myfeature/myfeatures/\(featureId)"
case .BuyFeature(_):
return "purchase/purchase"
case .ChangeState(let featureId, _):
return "myfeature/myfeatures/\(featureId)"
case .NotificationsSubscribe:
return "subscriptions/subscribe"
case .NotificationsUnsubscribe:
return "subscriptions/unsubscribe"
case .Logout:
return "user/logout"
}
}
var URLRequest: NSMutableURLRequest {
let URL = Configuration.sharedInstance.currentConfig.baseURL()
let mutableURLRequest = NSMutableURLRequest(URL: URL.URLByAppendingPathComponent(path)!)
mutableURLRequest.HTTPMethod = method.rawValue
if let token = DataManager.sharedInstance.authToken.tokenString {
mutableURLRequest.setValue(token, forHTTPHeaderField: "api-token")
}
switch self {
case .Login(let login, let password):
let parameters = ["login":login, "password":password]
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .BuyFeature(let featureId):
let parameters = ["featureId":featureId]
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .ChangeState(_, let stateString):
let parameters = ["desiredState":stateString]
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: parameters).0
case .NotificationsSubscribe(let token):
let data = ["deviceId": token]
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: data).0
case .NotificationsUnsubscribe(let token):
let data = ["deviceId": token]
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: data).0
default:
return Alamofire.ParameterEncoding.JSON.encode(mutableURLRequest, parameters: nil).0
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment