Last active
October 6, 2021 19:23
-
-
Save gtokman/503029bd72656f5724b9ab0c11a58875 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
extension YelpEndpoint { | |
/// Endpoint URL Path | |
var path: String { | |
switch self { | |
case .search: | |
return "/v3/businesses/search" | |
case let .detail(id): | |
return "/v3/businesses/\(id)" | |
} | |
} | |
/// URL parameters | |
var queryItems: [URLQueryItem] { | |
switch self { | |
case let .search(term, location): | |
return [ | |
"term": term, | |
"latitude": String(location.coordinate.latitude), | |
"longitude": String(location.coordinate.longitude) | |
].map(URLQueryItem.init) | |
case .detail: | |
return [] | |
} | |
} | |
/// HTTP Method | |
var method: String { | |
switch self { | |
case .search: | |
return "GET" | |
case .detail: | |
return "GET" | |
} | |
} | |
/// URLRequest | |
var request: URLRequest { | |
var urlComponents = URLComponents(string: "https://api.yelp.com")! | |
urlComponents.path = path | |
urlComponents.queryItems = queryItems | |
var request = URLRequest(url: urlComponents.url!) | |
request.httpMethod = method | |
request.addValue("Bearer API_KEY", forHTTPHeaderField: "Authorization") | |
return request | |
} | |
}… |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment