Skip to content

Instantly share code, notes, and snippets.

@anhhtz
Created December 16, 2017 22:57
Show Gist options
  • Save anhhtz/e44faa4f2dc25f90da96f409ae3d79bf to your computer and use it in GitHub Desktop.
Save anhhtz/e44faa4f2dc25f90da96f409ae3d79bf to your computer and use it in GitHub Desktop.
Define api request by enum in swift
//
// ServicesAPI+WebServices.swift
// gist
//
// Created by Allan Vialatte on 28/07/16.
// Copyright © 2016 Allan Vialatte. All rights reserved.
//
//MARK: - WebServices
extension ServicesAPI {
enum WebServices {
//MARK: - WebServices
private static let url = "/api/mobility"
//////////////////////
//MARK: Vehicle
/////////////////////
enum Vehicle {
case find
case get(id:String)
case update(id:String)
case saveEquipment(id:String)
case deleteEquipment(equipmentId:String, forVehicleId:String)
func builder() -> ApiRequestBuilder {
switch self {
case .find:
return (url + "/vehicle", .GET)
case let .get(id:identifier):
return (url + "/vehicle/\(identifier)", .GET)
case let .update(id:identifier):
return (url + "/vehicle/\(identifier)", .PUT)
case let .saveEquipment(id:identifier):
return (url + "/vehicle/\(identifier)/equipment", .POST)
case let .deleteEquipment(equipmentId:equip, forVehicleId:identifier):
return (url + "/vehicle/\(identifier)/equipment/\(equip)", .DELETE)
}
}
}
//////////////////////
//MARK: Other
/////////////////////
}
}
//
// ServicesAPI.swift
// gist
//
// Created by Allan Vialatte on 28/07/16.
// Copyright © 2016 Allan Vialatte. All rights reserved.
//
import UIKit
import Alamofire
import SwiftyJSON
typealias ApiRequestBuilder = (url: String, action: Alamofire.Method)
typealias ApiRequestParams = [String:AnyObject]
typealias ApiRequestResult = (result: JSON?, error: NSError?) -> ()
class ServicesAPI: NSObject {
private static let sharedManager = Manager(configuration: ServicesAPI.defaultSession())
private static func defaultSession() -> NSURLSessionConfiguration {
let configuration = NSURLSessionConfiguration.defaultSessionConfiguration()
let xHTTPAdditionalHeaders: [NSObject : AnyObject] = Manager.defaultHTTPHeaders
configuration.HTTPMaximumConnectionsPerHost = 1
configuration.HTTPAdditionalHeaders = xHTTPAdditionalHeaders
return configuration
}
}
extension ServicesAPI {
private static func router(builder: ApiRequestBuilder, params: ApiRequestParams?) -> URLRequestConvertible {
let url = NSURL(string: Config.WebServices.rootUrl + builder.url) ?? NSURL()
let request = NSMutableURLRequest(URL: url)
let encoding = Alamofire.ParameterEncoding.URL
request.HTTPMethod = builder.action.rawValue
request.timeoutInterval = NSTimeInterval(10 * 1000)
return encoding.encode(request, parameters: params).0
}
struct Request {
let builder: ApiRequestBuilder
let parameters: ApiRequestParams?
init(builder: ApiRequestBuilder, params: ApiRequestParams?) {
self.builder = builder
self.parameters = params
}
func run(closure: ApiRequestResult) {
let request = ServicesAPI.router(builder, params: parameters)
let manager = ServicesAPI.sharedManager
manager.request(request).validate()
.responseJSON { response in
switch response.result {
case .Success:
if let value = response.result.value {
closure(result: JSON(value), error: nil)
}
closure(result: nil, error: nil)
case .Failure(let error):
closure(result: nil, error: error)
}
}
}
}
}
func findVehicle(id:String) {
let builder = ServicesAPI.WebServices.Vehicle.get(id: id).builder()
let request = ServicesAPI.Request(builder: builder, params: nil)
request.run { (result, error) in
print(result)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment