<commit_before>//
//  AuthenticationRequest.swift
//  CreatubblesAPIClient
//
//  Created by <NAME> on 26.10.2016.
//  Copyright © 2016 Nomtek. All rights reserved.
//

import UIKit

class AuthenticationRequest: Request {
    override var method: RequestMethod  { return .post }
    override var endpoint: String { return settings.tokenUri }
    override var onlyPath: Bool { return false }
    override var parameters: Dictionary<String, AnyObject> { return prepareParameters() }
    
    fileprivate let settings: APIClientSettings
    fileprivate let username: String?
    fileprivate let password: String?
    
    init(username: String?, password: String?, settings: APIClientSettings) {
        self.username = username
        self.password = password
        self.settings = settings
    }
    
    fileprivate func prepareParameters() -> Dictionary<String,AnyObject> {
        var params = Dictionary<String,AnyObject>()
        if let username = username {
            params["username"] = username as AnyObject?
        }
        
        if let password = password {
            params["password"] = password as AnyObject?
        }
        
        params["grant_type"] = "password" as AnyObject?
        params["client_id"] = settings.appId as AnyObject?
        params["client_secret"] = settings.appSecret as AnyObject?
        
        return params
    }
}<commit_msg>CRI-637 - fallback to client_credentials if username and password are missing<commit_after>//
//  AuthenticationRequest.swift
//  CreatubblesAPIClient
//
//  Created by <NAME> on 26.10.2016.
//  Copyright © 2016 Nomtek. All rights reserved.
//

import UIKit

class AuthenticationRequest: Request {
    override var method: RequestMethod  { return .post }
    override var endpoint: String { return settings.tokenUri }
    override var onlyPath: Bool { return false }
    override var parameters: Dictionary<String, AnyObject> { return prepareParameters() }
    
    fileprivate let settings: APIClientSettings
    fileprivate let username: String?
    fileprivate let password: String?
    
    init(username: String?, password: String?, settings: APIClientSettings) {
        self.username = username
        self.password = password
        self.settings = settings
    }
    
    fileprivate func prepareParameters() -> Dictionary<String,AnyObject> {
        var params = Dictionary<String,AnyObject>()
        if let username = username {
            params["username"] = username as AnyObject?
        }
        
        if let password = password {
            params["password"] = password as AnyObject?
        }
        
        if username != nil && password != nil {
            params["grant_type"] = "password" as AnyObject?
        } else {
            params["grant_type"] = "client_credentials" as AnyObject?
        }
        params["client_id"] = settings.appId as AnyObject?
        params["client_secret"] = settings.appSecret as AnyObject?
        
        return params
    }
}
{'fraction_non_alphanumeric': 0.09045226130653267, 'fraction_numerical': 0.009045226130653266, 'mean_word_length': 2.5760479041916167, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 13, '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': '4558591', 'n_tokens_mistral': 779, 'n_tokens_neox': 758, 'n_words': 231}