<commit_before>//
//  V2LoginAPI.swift
//  Eurofurence
//
//  Created by <NAME> on 20/07/2017.
//  Copyright © 2017 Eurofurence. All rights reserved.
//

import Foundation

class V2LoginAPI {

    private var jsonPoster: JSONPoster

    init(jsonPoster: JSONPoster) {
        self.jsonPoster = jsonPoster
    }

    func performLogin(arguments: APILoginParameters,
                      completionHandler: @escaping (APIResponse<LoginCredential>) -> Void) {
        do {
            let postArguments: [String : Any] = ["RegNo": arguments.regNo,
                                                 "Username": arguments.username,
                                                 "Password": arguments.password]
            let jsonData = try JSONSerialization.data(withJSONObject: postArguments, options: [])
            let request = POSTRequest(url: "https://app.eurofurence.org/api/v2/Tokens/RegSys", body: jsonData)
            jsonPoster.post(request) { data in
                guard let responseData = data,
                    let json = try? JSONSerialization.jsonObject(with: responseData, options: .allowFragments),
                    let jsonDictionary = json as? [String : Any],
                    let response = JSONLoginResponse(json: jsonDictionary) else {
                        completionHandler(.failure)
                        return
                }

                let credential = LoginCredential(username: response.username,
                                                 registrationNumber: response.userID,
                                                 authenticationToken: response.authToken,
                                                 tokenExpiryDate: response.authTokenExpiry)
                completionHandler(.success(credential))
            }
        } catch {
            print("Unable to perform login due to error: \(error)")
        }
    }
<commit_msg>Split apart login function into bits<commit_after>//
//  V2LoginAPI.swift
//  Eurofurence
//
//  Created by <NAME> on 20/07/2017.
//  Copyright © 2017 Eurofurence. All rights reserved.
//

import Foundation

class V2LoginAPI {

    private var jsonPoster: JSONPoster

    init(jsonPoster: JSONPoster) {
        self.jsonPoster = jsonPoster
    }

    func performLogin(arguments: APILoginParameters,
                      completionHandler: @escaping (APIResponse<LoginCredential>) -> Void) {
        do {
            let jsonData = try makeLoginBody(from: arguments)
            performLogin(body: jsonData, completionHandler: completionHandler)
        } catch {
            print("Unable to perform login due to error: \(error)")
        }
    }

    private func makeLoginBody(from arguments: APILoginParameters) throws -> Data {
        let postArguments: [String : Any] = ["RegNo": arguments.regNo,
                                             "Username": arguments.username,
                                             "Password": arguments.password]
        return try JSONSerialization.data(withJSONObject: postArguments, options: [])
    }

    private func performLogin(body: Data, completionHandler: @escaping (APIResponse<LoginCredential>) -> Void) {
        let request = POSTRequest(url: "https://app.eurofurence.org/api/v2/Tokens/RegSys", body: body)
        jsonPoster.post(request) { data in
            guard let responseData = data,
                let json = try? JSONSerialization.jsonObject(with: responseData, options: .allowFragments),
                let jsonDictionary = json as? [String : Any],
                let response = JSONLoginResponse(json: jsonDictionary) else {
                    completionHandler(.failure)
                    return
            }

            let credential = LoginCredential(username: response.username,
                                             registrationNumber: response.userID,
                                             authenticationToken: response.authToken,
                                             tokenExpiryDate: response.authTokenExpiry)
            completionHandler(.success(credential))
        }
    }

{'fraction_non_alphanumeric': 0.08477889078915221, 'fraction_numerical': 0.007329587099926704, 'mean_word_length': 1.7130550033134526, 'pattern_counts': {'":': 6, '<': 8, '<?xml version=': 0, '>': 12, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 20, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2265950', 'n_tokens_mistral': 1001, 'n_tokens_neox': 940, 'n_words': 236}