Search code examples
iosswiftyelpyelp-fusion-api

Yelp Fusion API


I am a beginner trying to incorporate yelp's fusions API into my app but I couldn't find any proper resources on how to use the API. I am trying to use the business Search endpoint. can someone help me? Thanks.


Solution

  • I am using yelp for my location based restaurant viewing application.

    I use Moya for the network requests. I have example for you:

    In your codebase define your own api key!

    import Foundation
    import Moya
    
    private let apiKey = MyConstants.shared.apiKey
    
    enum YelpService {
    enum BusinessProvider: TargetType {
        case search(lat: Double, long: Double)
        case details(id: String)
    
        var baseURL: URL {
            return URL(string: "https://api.yelp.com/v3/businesses")!
        }
    
        var path: String {
            switch self {
            case .search:
                return "/search"
            case let .details(id):
                return "/\(id)"
            }
        }
    
        var method: Moya.Method {
            return .get
        }
    
        var sampleData: Data {
            return Data()
        }
    
        var task: Task {
            switch self {
            case let .search(lat, long):
                return .requestParameters(parameters: ["latitude": lat, "longitude": long, "limit": 10], encoding: URLEncoding.queryString)
            case .details:
                return .requestPlain
            }
        }
    
        var headers: [String: String]? {
            return ["Authorization": "Bearer \(apiKey)"]
        }
    }
    }
    

    Your api link should look like this : https://api.yelp.com/v3/businesses/search