Skip to content

Instantly share code, notes, and snippets.

@gtokman
Last active October 6, 2021 19:23
Show Gist options
  • Save gtokman/503029bd72656f5724b9ab0c11a58875 to your computer and use it in GitHub Desktop.
Save gtokman/503029bd72656f5724b9ab0c11a58875 to your computer and use it in GitHub Desktop.
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