<commit_before>//
//  V2SyncAPI.swift
//  Eurofurence
//
//  Created by <NAME> on 27/02/2018.
//  Copyright © 2018 Eurofurence. All rights reserved.
//

import Foundation

struct V2SyncAPI: SyncAPI {

    var jsonSession: JSONSession
    private let decoder: JSONDecoder

    init(jsonSession: JSONSession) {
        self.jsonSession = jsonSession

        decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .formatted(Iso8601DateFormatter())
    }

    func fetchLatestData(lastSyncTime: Date?, completionHandler: @escaping (APISyncResponse?) -> Void) {
        let url = "https://app.eurofurence.org/api/v2/Sync"

        var headers: [String: String] = [:]
        if let lastSyncTime = lastSyncTime {
            headers["since"] = Iso8601DateFormatter.instance.string(from: lastSyncTime)
        }

        let request = JSONRequest(url: url, body: Data(), headers: headers)

        jsonSession.get(request) { (data, _) in
            var response: APISyncResponse?
            defer { completionHandler(response) }

            if let data = data {
                response = (try? self.decoder.decode(JSONSyncResponse.self, from: data))?.asAPIResponse()
            }
        }
    }
<commit_msg>Print parsing error for debugging aid<commit_after>//
//  V2SyncAPI.swift
//  Eurofurence
//
//  Created by <NAME> on 27/02/2018.
//  Copyright © 2018 Eurofurence. All rights reserved.
//

import Foundation

struct V2SyncAPI: SyncAPI {

    var jsonSession: JSONSession
    private let decoder: JSONDecoder

    init(jsonSession: JSONSession) {
        self.jsonSession = jsonSession

        decoder = JSONDecoder()
        decoder.dateDecodingStrategy = .formatted(Iso8601DateFormatter())
    }

    func fetchLatestData(lastSyncTime: Date?, completionHandler: @escaping (APISyncResponse?) -> Void) {
        let url = "https://app.eurofurence.org/api/v2/Sync"

        var headers: [String: String] = [:]
        if let lastSyncTime = lastSyncTime {
            headers["since"] = Iso8601DateFormatter.instance.string(from: lastSyncTime)
        }

        let request = JSONRequest(url: url, body: Data(), headers: headers)

        jsonSession.get(request) { (data, _) in
            var response: APISyncResponse?
            defer { completionHandler(response) }

            if let data = data {
                do {
                    let decodedResponse = try self.decoder.decode(JSONSyncResponse.self, from: data)
                    response = decodedResponse.asAPIResponse()
                } catch {
                    print(error)
                }
            }
        }
    }

{'fraction_non_alphanumeric': 0.10451761102603369, 'fraction_numerical': 0.01761102603369066, 'mean_word_length': 2.270337922403004, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '816456', 'n_tokens_mistral': 803, 'n_tokens_neox': 723, 'n_words': 173}