<commit_before>//
//  SpreedlyAPIClient.swift
//  Spreedly
//
//  Created by <NAME> on 9/22/15.
//  Copyright © 2015 Spreedly Inc. All rights reserved.
//

import Foundation
import PassKit

public class SpreedlyAPIClient {
    public typealias SpreedlyAPICompletionBlock = (paymentMethod: PaymentMethod?, error: NSError?) -> Void
    
    public var environmentKey: String
    public var apiUrl: String
    
    public init(environmentKey: String, apiUrl: String) {
        self.environmentKey = environmentKey
        self.apiUrl = apiUrl
    }
    
    convenience public init(environmentKey: String) {
        let apiUrl = "http://core.spreedly.com/v1/payment_methods.json"
        self.init(environmentKey: environmentKey, apiUrl: apiUrl)
    }
    
    public func createPaymentMethodTokenWithCreditCard(creditCard: CreditCard, completion: SpreedlyAPICompletionBlock) {
        let serializedRequest = RequestSerializer.serialize(creditCard)
        
        if serializedRequest.error == nil {
            if let data = serializedRequest.data {
                self.createPaymentMethodTokenWithData(data, completion: completion)
            }
        }
    }
    
    public func createPaymentMethodTokenWithApplePay(payment: PKPayment, completion: SpreedlyAPICompletionBlock) {
        self.createPaymentMethodTokenWithData(RequestSerializer.serialize(payment.token.paymentData), completion: completion)
    }

    func createPaymentMethodTokenWithData(data: NSData, completion: SpreedlyAPICompletionBlock) {
        let url = NSURL(string: apiUrl + "?environment_key=\(self.environmentKey)")

        let request = NSMutableURLRequest(URL: url!)
        let session = NSURLSession.sharedSession()

        request.HTTPBody = data
        request.HTTPMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")
        
        let task = session.dataTaskWithRequest(request) { data, response, error -> Void in
            if (error != nil) {
                completion(paymentMethod: nil, error: error)
            } else {
                do {
                    if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
                        if let transactionDict = json["transaction"] as? NSDictionary {
                            if let paymentMethodDict = transactionDict["payment_method"] as? [String: AnyObject] {
                                let paymentMethod = PaymentMethod(attributes: paymentMethodDict)
                                completion(paymentMethod: paymentMethod, error: nil)
                            }
                        } else {
                            // Eventually, we'll want to loop through the errors and send back those with
                            // if let errorsDict = json["errors"] as? NSDictionary
                            let userInfo = ["ProcessingError": "Unable to create payment method token with values sent"]
                            let apiError = NSError(domain: "com.spreedly.lib", code: 60, userInfo: userInfo)
                            completion(paymentMethod: nil, error: apiError)
                        }
                    }
                } catch let parseError as NSError {
                    completion(paymentMethod: nil, error: parseError)
                }
            }
        }
        
        task.resume()
    }
}
<commit_msg>Customer user agent for payment method requests<commit_after>//
//  SpreedlyAPIClient.swift
//  Spreedly
//
//  Created by <NAME> on 9/22/15.
//  Copyright © 2015 Spreedly Inc. All rights reserved.
//

import Foundation
import PassKit

public class SpreedlyAPIClient {
    public typealias SpreedlyAPICompletionBlock = (paymentMethod: PaymentMethod?, error: NSError?) -> Void
    
    public var environmentKey: String
    public var apiUrl: String
    
    public init(environmentKey: String, apiUrl: String) {
        self.environmentKey = environmentKey
        self.apiUrl = apiUrl
    }
    
    convenience public init(environmentKey: String) {
        let apiUrl = "http://core.spreedly.com/v1/payment_methods.json"
        self.init(environmentKey: environmentKey, apiUrl: apiUrl)
    }
    
    public func createPaymentMethodTokenWithCreditCard(creditCard: CreditCard, completion: SpreedlyAPICompletionBlock) {
        let serializedRequest = RequestSerializer.serialize(creditCard)
        
        if serializedRequest.error == nil {
            if let data = serializedRequest.data {
                self.createPaymentMethodTokenWithData(data, completion: completion)
            }
        }
    }
    
    public func createPaymentMethodTokenWithApplePay(payment: PKPayment, completion: SpreedlyAPICompletionBlock) {
        self.createPaymentMethodTokenWithData(RequestSerializer.serialize(payment.token.paymentData), completion: completion)
    }

    func createPaymentMethodTokenWithData(data: NSData, completion: SpreedlyAPICompletionBlock) {
        let url = NSURL(string: apiUrl + "?environment_key=\(self.environmentKey)")

        let request = NSMutableURLRequest(URL: url!)
        let session = NSURLSession.sharedSession()

        request.HTTPBody = data
        request.HTTPMethod = "POST"
        request.addValue("application/json", forHTTPHeaderField: "Content-Type")
        request.addValue("application/json", forHTTPHeaderField: "Accept")
        request.addValue("spreedly-ios-lib/0.1.0", forHTTPHeaderField: "User-Agent")
        
        let task = session.dataTaskWithRequest(request) { data, response, error -> Void in
            if (error != nil) {
                completion(paymentMethod: nil, error: error)
            } else {
                do {
                    if let json = try NSJSONSerialization.JSONObjectWithData(data!, options: []) as? NSDictionary {
                        if let transactionDict = json["transaction"] as? NSDictionary {
                            if let paymentMethodDict = transactionDict["payment_method"] as? [String: AnyObject] {
                                let paymentMethod = PaymentMethod(attributes: paymentMethodDict)
                                completion(paymentMethod: paymentMethod, error: nil)
                            }
                        } else {
                            // Eventually, we'll want to loop through the errors and send back those with
                            // if let errorsDict = json["errors"] as? NSDictionary
                            let userInfo = ["ProcessingError": "Unable to create payment method token with values sent"]
                            let apiError = NSError(domain: "com.spreedly.lib", code: 60, userInfo: userInfo)
                            completion(paymentMethod: nil, error: apiError)
                        }
                    }
                } catch let parseError as NSError {
                    completion(paymentMethod: nil, error: parseError)
                }
            }
        }
        
        task.resume()
    }
}

{'fraction_non_alphanumeric': 0.08247568024813196, 'fraction_numerical': 0.0038065698576060904, 'mean_word_length': 2.1073149364870782, 'pattern_counts': {'":': 2, '<': 5, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '949352', 'n_tokens_mistral': 1788, 'n_tokens_neox': 1743, 'n_words': 437}