0

I am hooking into the Yelp Api to get data which I am then showing in a card format. However, my issue is that I can't seem to pass the yelp data into this array in order to pull the yelp data. How do I do this? How can I do this on a scale of 20 restaurants?

This is how I am getting the information:

//
//  Get Restaurants.swift
//  Pickt
//
//  Created by Morris Richman on 8/29/21.
//

import Foundation
import Moya

private let apiKey = "<api key>"

enum YelpService {
    enum BuisnessesProvider: TargetType {
        var baseURL: URL {
            return URL(string: "https://api.yelp.com/v3/businesses")!
        }
        
        var path: String {
            switch self {
            case .search:
                return "/search"
            }
        }
        
        var method: Moya.Method {
            return .get
        }
        
        var sampleData: Data {
            return Data()
        }
        
        var task: Task {
            switch self {
            case let .search(loc):
                return .requestParameters(parameters: ["location": loc, "term": "restaurants", "limit": 1], encoding: URLEncoding.queryString)
            }
        }
        
        var headers: [String : String]? {
            return ["Authorization": "Bearer \(apiKey)"]
        }
        
        case search(loc: String)
        
    }
}

struct Root: Codable {
    let businesses: [Business]
}

struct Business: Codable {
    let id: String
    let name: String
    let imageUrl: URL
    let rating: Double
    let url: String
}

struct RestaurantListViewModel {
    let name: String
    let imageUrl: URL
    let id: String
    let rating: Double
    let url: String
}


extension RestaurantListViewModel {
    init(business: Business) {
        self.name = business.name
        self.id = business.id
        self.imageUrl = business.imageUrl
        self.rating = business.rating
        self.url = business.url
    }
}

Calling the get func:

let service = MoyaProvider<YelpService.BuisnessesProvider>()
        let jsonDecoder = JSONDecoder()
        jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
        service.request(.search(loc: "98117")) { result in
            switch result {
            case .success(let response):
                let root = try? self.jsonDecoder.decode(Root.self, from: response.data)
                let viewModels = root?.businesses.compactMap(RestaurantListViewModel.init)
                print(viewModels!)
                print(Restaurants.mock)
                Restaurants.viewModels = viewModels!
//                print(try? JSONSerialization.jsonObject(with: response.data, options: []))
            case .failure(let error):
                print("Error = \(error)")
            }
        }

And finally retrieving it:

    let name: String
    let imageUrl: URL
    let id: String
    let rating: Double
    let url: String
    
    static var viewModels: [RestaurantListViewModel] = []
    static var mock: [Restaurants] = [
        Restaurants(name: "Silverio's Mexican Kitchen", imageUrl: URL(string: "https://mainsite-prod-cdn.azureedge.net/partner-images/432257/micrositeimage_p1.jpg")!, id: "hjkhjhjh", rating: 2, url: "https://google.com"),
        Restaurants(name: "Taqueria La Esquinita", imageUrl: URL(string: "https://s3-media0.fl.yelpcdn.com/bphoto/x-KCQ7osmvBWLA9WpPdO_Q/o.jpg")!, id: "hjdha", rating: 3, url: "https://google.com")
    ]

I think I got the answer to this using CDYelpFusionKit. My only problem is when I install through Cocoapods, I get this error:

Multiple commands produce '/Users/mcrich/Library/Developer/Xcode/DerivedData/Pickt-fpbpwbgjikipuabwpwmkowpgeelv/Build/Products/Debug-iphonesimulator/Pickt.app/Frameworks/CDYelpFusionKit.framework': 1) Target 'Pickt' has copy command from '/Users/mcrich/Library/Developer/Xcode/DerivedData/Pickt-fpbpwbgjikipuabwpwmkowpgeelv/Build/Products/Debug-iphonesimulator/CDYelpFusionKit.framework' to '/Users/mcrich/Library/Developer/Xcode/DerivedData/Pickt-fpbpwbgjikipuabwpwmkowpgeelv/Build/Products/Debug-iphonesimulator/Pickt.app/Frameworks/CDYelpFusionKit.framework' 2) That command depends on command in Target 'Pickt': script phase “[CP] Embed Pods Frameworks”
Mcrich
  • 75
  • 9

2 Answers2

0

Why you are not using the "Business" model to save in the Array. which already have value you want to copy in "RestaurantListViewModel"

>     let array = [Business]()
>     let service = MoyaProvider<YelpService.BuisnessesProvider>()
>             let jsonDecoder = JSONDecoder()
>             jsonDecoder.keyDecodingStrategy = .convertFromSnakeCase
>             service.request(.search(loc: "98117")) { result in
>                 switch result {
>                 case .success(let response):
>                     let root = try? self.jsonDecoder.decode(Root.self, from: response.data)
>                     array.append(contentsOf:root.businesses)
>     //                print(try? JSONSerialization.jsonObject(with: response.data, options: []))
>                 case .failure(let error):
>                     print("Error = \(error)")
>                 }
>             }

also, use limit and offset query parameter to get the page wise info and use array.count() as offset and limit is 20 by default in yelp API

enter image description here

Prabhat Kasera
  • 1,129
  • 11
  • 28
0

Fixed it, it was getting the data and appending it, The content just wasn't showing, because it would load before that was done.

Mcrich
  • 75
  • 9