<commit_before>//
//  AtheleteRequests.swift
//  Leichatletik
//
//  Created by <NAME> on 28.01.17.
//  Copyright © 2017 freiraum. All rights reserved.
//

import Foundation
import DBNetworkStack

public struct AthleteWebService {
    
    private let baseURL: URL
    
    public init(baseURL: URL) {
       self.baseURL = baseURL
    }
    
    public func searchAthlets(with name: String, `in` region: Region? = nil, forYear year: Int? = nil) -> Resource<[Athlete]> {
        var parameter =  ["query": "*\(name)*"]
        parameter["lv"] = region?.id
        parameter["datayear"] = "\(year)"

        let request = URLRequest(path: "athletQuery", baseURL: baseURL, parameters: parameter)
        
        return Resource(request: request, parse: { data in
            let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
            let array = json as! [Any]
            
            return try Array(JSONArray: array)
        })
    }
    
    public func athletDeatils(for athlets: [AthleteDescribing], year: Int) -> Resource<[AthletDetails]> {
        let ids = athlets.map { $0.id }
        return athletDeatils(for: ids, year: year)
    }
    
    public func athletDeatils(for athlet: AthleteDescribing, year: Int) -> Resource<AthletDetails?> {
        return athletDeatils(for: [athlet], year: year).map(transform: { $0.first })
    }
    
    public func athletDeatils(for athletIds: [Int], year: Int) -> Resource<[AthletDetails]> {
        let ids = athletIds.map({ "\($0)" }).joined(separator: ",")
        let parameter = ["id": ids, "all": "true", "datayear": "\(year)"]
        let request = URLRequest(path: "athletDetail", baseURL: baseURL, parameters: parameter)<commit_msg>fixes optional<commit_after>//
//  AtheleteRequests.swift
//  Leichatletik
//
//  Created by <NAME> on 28.01.17.
//  Copyright © 2017 freiraum. All rights reserved.
//

import Foundation
import DBNetworkStack

public struct AthleteWebService {
    
    private let baseURL: URL
    
    public init(baseURL: URL) {
       self.baseURL = baseURL
    }
    
    public func searchAthlets(with name: String, `in` region: Region? = nil, forYear year: Int? = nil) -> Resource<[Athlete]> {
        var parameter =  ["query": "*\(name)*"]
        parameter["lv"] = region?.id
        parameter["datayear"] = year.map { "\($0)" }

        let request = URLRequest(path: "athletQuery", baseURL: baseURL, parameters: parameter)
        
        return Resource(request: request, parse: { data in
            let json = try JSONSerialization.jsonObject(with: data, options: .allowFragments)
            let array = json as! [Any]
            
            return try Array(JSONArray: array)
        })
    }
    
    public func athletDeatils(for athlets: [AthleteDescribing], year: Int) -> Resource<[AthletDetails]> {
        let ids = athlets.map { $0.id }
        return athletDeatils(for: ids, year: year)
    }
    
    public func athletDeatils(for athlet: AthleteDescribing, year: Int) -> Resource<AthletDetails?> {
        return athletDeatils(for: [athlet], year: year).map(transform: { $0.first })
    }
    
    public func athletDeatils(for athletIds: [Int], year: Int) -> Resource<[AthletDetails]> {
        let ids = athletIds.map({ "\($0)" }).joined(separator: ",")
        let parameter = ["id": ids, "all": "true", "datayear": "\(year)"]
        let request = URLRequest(path: "athletDetail", baseURL: baseURL, parameters: parameter)
{'fraction_non_alphanumeric': 0.13683602771362588, 'fraction_numerical': 0.00779445727482679, 'mean_word_length': 2.75, 'pattern_counts': {'":': 8, '<': 13, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '1341643', 'n_tokens_mistral': 1088, 'n_tokens_neox': 1050, 'n_words': 287}