Created
May 14, 2016 13:59
-
-
Save psaitu/94fa4a46f7fa8d02fa82a9ba5df4c775 to your computer and use it in GitHub Desktop.
Sample Moya Target
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
| // | |
| // Zomato.swift | |
| // BiryaniAF | |
| // | |
| // Created by Prabhu Saitu on 13/05/16. | |
| // Copyright © 2016 Prabhu Saitu. All rights reserved. | |
| // | |
| import Foundation | |
| import Moya | |
| import CoreLocation | |
| enum Zomato { | |
| case Location(lat:CLLocationDegrees, lng:CLLocationDegrees) | |
| case Biryani(entity_id: Int, entity_type: String, cuisines:Int) | |
| } | |
| func url(route: Zomato) -> String { | |
| return route.baseURL.URLByAppendingPathComponent(route.path).absoluteString | |
| } | |
| extension Zomato: TargetType { | |
| var baseURL:NSURL { | |
| return NSURL(string: "https://developers.zomato.com/api/v2.1/")! | |
| } | |
| var path: String { | |
| switch self { | |
| case .Location(_,_): | |
| return "/locations" | |
| case .Biryani(_, _, _): | |
| return "/search" | |
| default: | |
| return "/" | |
| } | |
| } | |
| var method: Moya.Method { | |
| switch self { | |
| case .Location: | |
| return .GET | |
| default: | |
| return .GET | |
| } | |
| } | |
| var parameters: [String: AnyObject]? { | |
| switch self { | |
| case .Location(let lat, let lng): | |
| return ["lat": lat, "lon": lng] | |
| case .Biryani(let entity_id, let entity_type, let cuisines): | |
| return ["entity_id": entity_id, "entity_type": entity_type, "cuisines": cuisines] | |
| default: | |
| return nil | |
| } | |
| } | |
| var sampleData: NSData { | |
| switch self { | |
| case .Location(let lat, let lng): | |
| return "{\(lat) - \(lng)}".UTF8EncodedData | |
| case .Biryani(let entity_id, let entity_type, let cuisines): | |
| return "{\(entity_id) - \(entity_type) - \(cuisines)}".UTF8EncodedData | |
| default: | |
| return "{}".UTF8EncodedData | |
| } | |
| } | |
| } | |
| let endpointZomato = { (target: Zomato) -> Endpoint<Zomato> in | |
| let endpoint: Endpoint<Zomato> = Endpoint<Zomato>(URL: url(target), sampleResponseClosure: {.NetworkResponse(200, target.sampleData)}, method: target.method, parameters: target.parameters) | |
| return endpoint.endpointByAddingHTTPHeaderFields(["user-key": "", "Accept": "application/json"]) | |
| } | |
| // MARK: - Helpers | |
| private extension String { | |
| var URLEscapedString: String { | |
| return self.stringByAddingPercentEncodingWithAllowedCharacters(NSCharacterSet.URLHostAllowedCharacterSet())! | |
| } | |
| var UTF8EncodedData: NSData { | |
| return self.dataUsingEncoding(NSUTF8StringEncoding)! | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment