<commit_before>//
//  APILexicalModelRepository.swift
//  KeymanEngine
//
//  Created by <NAME> on 3/19/19.
//  Copyright © 2019 SIL International. All rights reserved.
//

import Foundation
import UIKit // for UIDevice

public enum APILexicalModelFetchError: Error {
  case networkError(Error)
  case noData
  case parsingError(Error)
}

public class APILexicalModelRepository: LexicalModelRepository {
  private let modelsAPIURL = URLComponents(string: "https://api.keyman.com/model")! //?q=bcp47:en
  
  public weak var delegate: LexicalModelRepositoryDelegate?
  public private(set) var languages: [String: Language]?
  public private(set) var lexicalModels: [String: LexicalModel]?
  
  public func fetchList(languageID: String, completionHandler: @escaping ListCompletionHandler) {
    var urlComponents = modelsAPIURL
    let bcp47Value = "bcp47:" + languageID
    urlComponents.queryItems = [
      URLQueryItem(name: "q", value: bcp47Value)
    ]
    log.info("Connecting to Keyman cloud: \(urlComponents.url!).")
    let task = URLSession.shared.dataTask(with: urlComponents.url!) { (data, response, error) in
      self.apiListCompletionHandler(data: data, response: response, error: error,
                                fetchCompletionHandler: completionHandler)
    }
    task.resume()
  }
  
  private func apiListCompletionHandler(data: Data?,
                                    response: URLResponse?,
                                    error: Error?,
                                    fetchCompletionHandler: @escaping ListCompletionHandler) {
    let errorHandler = { (error: Error) -> Void in
      DispatchQueue.main.async {
        self.delegate?.lexicalModelRepository(self, didFailFetch: error)
        fetchCompletionHandler(nil, error)
      }
    }
    
    if let error = error {
      log.error("Network error fetching languages: \(error)")
      errorHandler(APILexicalModelFetchError.networkError(error))
      return
    }
    guard let data = data else {
      log.error("Language API did not return data")
      errorHandler(APILexicalModelFetchError.noData)
      return
    }
    
    let decoder = JSONDecoder()
    decoder.dateDecodingStrategy = .secondsSince1970
    let result: LexicalModelAPICall
    do {
      result = try decoder.decode(LexicalModelAPICall.self, from: data)
    } catch {
      log.error("Failed parsing API lexical models: \(error)")
      errorHandler(APILexicalModelFetchError.parsingError(error))
      return
    }
    
    let lexicalModels = result.lexicalModels
    self.lexicalModels = Dictionary(uniqueKeysWithValues: result.lexicalModels.map { ($0.id, $0) })

    
    log.info("Request list completed -- \(result.lexicalModels.count) lexical models.")
    DispatchQueue.main.async {
      self.delegate?.lexicalModelRepositoryDidFetch(self)
      fetchCompletionHandler(lexicalModels, nil)
    }
  }
}
<commit_msg>removed comment  showing query to be added<commit_after>//
//  APILexicalModelRepository.swift
//  KeymanEngine
//
//  Created by <NAME> on 3/19/19.
//  Copyright © 2019 SIL International. All rights reserved.
//

import Foundation
import UIKit // for UIDevice

public enum APILexicalModelFetchError: Error {
  case networkError(Error)
  case noData
  case parsingError(Error)
}

public class APILexicalModelRepository: LexicalModelRepository {
  private let modelsAPIURL = URLComponents(string: "https://api.keyman.com/model")!
  
  public weak var delegate: LexicalModelRepositoryDelegate?
  public private(set) var languages: [String: Language]?
  public private(set) var lexicalModels: [String: LexicalModel]?
  
  public func fetchList(languageID: String, completionHandler: @escaping ListCompletionHandler) {
    var urlComponents = modelsAPIURL
    let bcp47Value = "bcp47:" + languageID
    urlComponents.queryItems = [
      URLQueryItem(name: "q", value: bcp47Value)
    ]
    log.info("Connecting to Keyman cloud: \(urlComponents.url!).")
    let task = URLSession.shared.dataTask(with: urlComponents.url!) { (data, response, error) in
      self.apiListCompletionHandler(data: data, response: response, error: error,
                                fetchCompletionHandler: completionHandler)
    }
    task.resume()
  }
  
  private func apiListCompletionHandler(data: Data?,
                                    response: URLResponse?,
                                    error: Error?,
                                    fetchCompletionHandler: @escaping ListCompletionHandler) {
    let errorHandler = { (error: Error) -> Void in
      DispatchQueue.main.async {
        self.delegate?.lexicalModelRepository(self, didFailFetch: error)
        fetchCompletionHandler(nil, error)
      }
    }
    
    if let error = error {
      log.error("Network error fetching languages: \(error)")
      errorHandler(APILexicalModelFetchError.networkError(error))
      return
    }
    guard let data = data else {
      log.error("Language API did not return data")
      errorHandler(APILexicalModelFetchError.noData)
      return
    }
    
    let decoder = JSONDecoder()
    decoder.dateDecodingStrategy = .secondsSince1970
    let result: LexicalModelAPICall
    do {
      result = try decoder.decode(LexicalModelAPICall.self, from: data)
    } catch {
      log.error("Failed parsing API lexical models: \(error)")
      errorHandler(APILexicalModelFetchError.parsingError(error))
      return
    }
    
    let lexicalModels = result.lexicalModels
    self.lexicalModels = Dictionary(uniqueKeysWithValues: result.lexicalModels.map { ($0.id, $0) })

    
    log.info("Request list completed -- \(result.lexicalModels.count) lexical models.")
    DispatchQueue.main.async {
      self.delegate?.lexicalModelRepositoryDidFetch(self)
      fetchCompletionHandler(lexicalModels, nil)
    }
  }
}

{'fraction_non_alphanumeric': 0.09398172098637696, 'fraction_numerical': 0.007587515088808415, 'mean_word_length': 3.002760524499655, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4631590', 'n_tokens_mistral': 1680, 'n_tokens_neox': 1612, 'n_words': 381}