<commit_before>//
//  Yelp.swift
//  LaDalle
//
//  Created by <NAME> on 9/18/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import UIKit



class Yelp {
    private let server = "https://api.yelp.com/v3/"
    
    //static let sharedInstance = Yelp()
    var token: String? = nil
    
    init() {
    }

}


//Get Yelp Token
extension Yelp {
    func getToken() {
        
        print("Calling Yelp to get a token")
        // create the url
        //let link = server + "oauth2/token"
        let link = "https://api.yelp.com/oauth2/token"
        guard let url = URL(string: link) else { return }
        
        //set headers
        let headers = [
            "content-type": "application/x-www-form-urlencoded"
        ]
        
        let clientId = valueForAPIKey(named: "YELP_API_CLIENT_ID")
        let clientSecret = valueForAPIKey(named: "YELP_API_SECRET_ID")
        
        // set body
        let bodyData = "client_id=\(clientId)&client_secret=\(clientSecret)&grant_type=client_credentials"
        
        //set request
        var request = URLRequest.init(url: url)
        
        request.httpMethod = "POST"
        request.allHTTPHeaderFields = headers
        request.httpBody = bodyData.data(using: .utf8)
        
        
        //create sharedSession
        let sharedSession = URLSession.shared
        
        // setup completion handler
        let completionHandler: (Data?, URLResponse?, Error?) -> Void = { data, response, error in
            
            guard let data = data, error == nil else {
                // check for networking error
                print("error=\(error)")
                return
            }
            
            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response)")
            }
            
            do {
                let jsonObject = try JSONSerialization.jsonObject(with: data, options:
                    JSONSerialization.ReadingOptions.allowFragments)
                
                guard let result = jsonObject as? NSDictionary else { return }
                guard let accessToken = result["access_token"] as? String else { return }
                
                self.token = accessToken
                
                //store token in plist
                
            } catch {
                print("Could not get an Access Token")
                return
            }
            
        }
        
        // set dataTask
        let dataTask = sharedSession.dataTask(with: request, completionHandler: completionHandler)
        
        //resume dataTask
        dataTask.resume()
        
    }
}

// Get Local Places
extension Yelp {
    
    class func getLocalPlaces(forCategory category: String, coordinates: Coordinates, completionHandler: @escaping ([Business]) -> ()) {
        
        let dataManager = DataManager.sharedInstance

        var arrayOfBusinesses: [Business] = []
        
        let accessToken = valueForAPIKey(named: "YELP_API_ACCESS_TOKEN")
        
        //if radius return 0 results then increase the radius
        let radius = 2011
        
        // limit distance and limit to open only.
        let link = "https://api.yelp.com/v3/businesses/search?categories=\(category)&latitude=\(coordinates.latitude)&longitude=\(coordinates.longitude)&radius=\(radius)&open_now=true"
        
        //set headers
        let headers = [
            "Authorization": "Bearer \(accessToken)"
        ]
        
        guard let url = URL(string: link) else { return }
        
        //set request
        var request = URLRequest.init(url: url)
        
        request.httpMethod = "GET"
        request.allHTTPHeaderFields = headers
        
        //        request.httpBody = bodyData.data(using: .utf8)
        
        
        //create sharedSession
        let sharedSession = URLSession.shared
        
        // setup completion handler<commit_msg>Update yelp query<commit_after>//
//  Yelp.swift
//  LaDalle
//
//  Created by <NAME> on 9/18/16.
//  Copyright © 2016 <NAME>. All rights reserved.
//

import Foundation
import UIKit



class Yelp {
    private let server = "https://api.yelp.com/v3/"
    
    //static let sharedInstance = Yelp()
    var token: String? = nil
    
    init() {
    }

}


//Get Yelp Token
extension Yelp {
    func getToken() {
        
        print("Calling Yelp to get a token")
        // create the url
        //let link = server + "oauth2/token"
        let link = "https://api.yelp.com/oauth2/token"
        guard let url = URL(string: link) else { return }
        
        //set headers
        let headers = [
            "content-type": "application/x-www-form-urlencoded"
        ]
        
        let clientId = valueForAPIKey(named: "YELP_API_CLIENT_ID")
        let clientSecret = valueForAPIKey(named: "YELP_API_SECRET_ID")
        
        // set body
        let bodyData = "client_id=\(clientId)&client_secret=\(clientSecret)&grant_type=client_credentials"
        
        //set request
        var request = URLRequest.init(url: url)
        
        request.httpMethod = "POST"
        request.allHTTPHeaderFields = headers
        request.httpBody = bodyData.data(using: .utf8)
        
        
        //create sharedSession
        let sharedSession = URLSession.shared
        
        // setup completion handler
        let completionHandler: (Data?, URLResponse?, Error?) -> Void = { data, response, error in
            
            guard let data = data, error == nil else {
                // check for networking error
                print("error=\(error)")
                return
            }
            
            if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {           // check for http errors
                print("statusCode should be 200, but is \(httpStatus.statusCode)")
                print("response = \(response)")
            }
            
            do {
                let jsonObject = try JSONSerialization.jsonObject(with: data, options:
                    JSONSerialization.ReadingOptions.allowFragments)
                
                guard let result = jsonObject as? NSDictionary else { return }
                guard let accessToken = result["access_token"] as? String else { return }
                
                self.token = accessToken
                
                //store token in plist
                
            } catch {
                print("Could not get an Access Token")
                return
            }
            
        }
        
        // set dataTask
        let dataTask = sharedSession.dataTask(with: request, completionHandler: completionHandler)
        
        //resume dataTask
        dataTask.resume()
        
    }
}

// Get Local Places
extension Yelp {
    
    class func getLocalPlaces(forCategory category: String, coordinates: Coordinates, completionHandler: @escaping ([Business]) -> ()) {
        
        let dataManager = DataManager.sharedInstance

        var arrayOfBusinesses: [Business] = []
        
        let accessToken = valueForAPIKey(named: "YELP_API_ACCESS_TOKEN")
        
        //if radius return 0 results then increase the radius
        let radius = 2011
        
        // limit distance and limit to open only.
        let link = "https://api.yelp.com/v3/businesses/search?categories=\(category)&latitude=\(coordinates.latitude)&longitude=\(coordinates.longitude)&radius=\(radius)" //&open_now=true
        
        //set headers
        let headers = [
            "Authorization": "Bearer \(accessToken)"
        ]
        
        guard let url = URL(string: link) else { return }
        
        //set request
        var request = URLRequest.init(url: url)
        
        request.httpMethod = "GET"
        request.allHTTPHeaderFields = headers
        
        //        request.httpBody = bodyData.data(using: .utf8)
        
        
        //create sharedSession
        let sharedSession = URLSession.shared
        
        // setup completion handler
{'fraction_non_alphanumeric': 0.09345115603437841, 'fraction_numerical': 0.006294637453092846, 'mean_word_length': 1.5948492462311559, 'pattern_counts': {'":': 4, '<': 7, '<?xml version=': 0, '>': 11, 'https://': 6, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5493575', 'n_tokens_mistral': 2240, 'n_tokens_neox': 2140, 'n_words': 560}