<commit_before>//
//  PaymentMethodSearchService.swift
//  MercadoPagoSDK
//
//  Created by <NAME> on 15/1/16.
//  Copyright © 2016 MercadoPago. All rights reserved.
//

internal class PaymentMethodSearchService: MercadoPagoService {

    let merchantPublicKey: String
    let payerAccessToken: String?
    let processingModes: [String]
    let branchId: String?

    init(baseURL: String, merchantPublicKey: String, payerAccessToken: String? = nil, processingModes: [String], branchId: String?) {
        self.merchantPublicKey = merchantPublicKey
        self.payerAccessToken = payerAccessToken
        self.processingModes = processingModes
        self.branchId = branchId
        super.init(baseURL: baseURL)
    }

    private func getInit(prefId: String?, bodyJSON: Data?, headers: [String: String]?, success: @escaping (_ paymentMethodSearch: PXInitDTO) -> Void, failure: @escaping ((_ error: PXError) -> Void)) {
        
        var uri = PXServicesURLConfigs.shared().MP_INIT_URI
        if let prefId = prefId {
            uri.append("/\(prefId)")
        }

        let params = MercadoPagoServices.getParamsAccessToken(payerAccessToken)

        self.request(uri: uri, params: params, body: bodyJSON, method: HTTPMethod.post, headers:
            headers, cache: false, success: { (data) -> Void in
                do {
                    print(try? data.mapToJSON())
                    let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
                    if let paymentSearchDic = jsonResult as? NSDictionary {
                        if paymentSearchDic["error"] != nil {
                            let apiException = try JSONDecoder().decode(PXApiException.self, from: data) as PXApiException
                            failure(PaymentMethodSearchService.getError(code: ErrorTypes.API_EXCEPTION_ERROR, apiException: apiException))
                        } else {
                            if paymentSearchDic.allKeys.count > 0 {
                                let initDTO = try JSONDecoder().decode(PXInitDTO.self, from: data) as PXInitDTO
                                success(initDTO)
                            } else {
                                failure(PaymentMethodSearchService.getError())
                            }
                        }
                    }
                } catch {
                    printDebug("PaymentMethodSearchService getInit error: \(error).")
                    failure(PaymentMethodSearchService.getError())
                }

        }, failure: { (_) -> Void in
            failure(PaymentMethodSearchService.getError(code: ErrorTypes.NO_INTERNET_ERROR, reason: "Verifique su conexión a internet e intente nuevamente"))
        })
    }
    
    private static func getError(code: Int = ErrorTypes.API_UNKNOWN_ERROR,
                                 reason: String = "No se ha podido obtener los métodos de pago",
                                 apiException: PXApiException? = nil) -> PXError {
        return PXError(domain: ApiDomain.GET_PAYMENT_METHODS, code: code, userInfo: [NSLocalizedDescriptionKey: "Hubo un error", NSLocalizedFailureReasonErrorKey: reason], apiException: apiException)
    }

    internal func getOpenPrefInit(pref: PXCheckoutPreference, cardsWithEsc: [String], splitEnabled: Bool, discountParamsConfiguration: PXDiscountParamsConfiguration?, flow: String?, charges: [PXPaymentTypeChargeRule], headers: [String: String]?, success: @escaping (_ paymentMethodSearch: PXInitDTO) -> Void, failure: @escaping ((_ error: PXError) -> Void)) {<commit_msg>removing print that should only be used for tests porpouse<commit_after>//
//  PaymentMethodSearchService.swift
//  MercadoPagoSDK
//
//  Created by <NAME> on 15/1/16.
//  Copyright © 2016 MercadoPago. All rights reserved.
//

internal class PaymentMethodSearchService: MercadoPagoService {

    let merchantPublicKey: String
    let payerAccessToken: String?
    let processingModes: [String]
    let branchId: String?

    init(baseURL: String, merchantPublicKey: String, payerAccessToken: String? = nil, processingModes: [String], branchId: String?) {
        self.merchantPublicKey = merchantPublicKey
        self.payerAccessToken = payerAccessToken
        self.processingModes = processingModes
        self.branchId = branchId
        super.init(baseURL: baseURL)
    }

    private func getInit(prefId: String?, bodyJSON: Data?, headers: [String: String]?, success: @escaping (_ paymentMethodSearch: PXInitDTO) -> Void, failure: @escaping ((_ error: PXError) -> Void)) {
        
        var uri = PXServicesURLConfigs.shared().MP_INIT_URI
        if let prefId = prefId {
            uri.append("/\(prefId)")
        }

        let params = MercadoPagoServices.getParamsAccessToken(payerAccessToken)

        self.request(uri: uri, params: params, body: bodyJSON, method: HTTPMethod.post, headers:
            headers, cache: false, success: { (data) -> Void in
                do {
                    let jsonResult = try JSONSerialization.jsonObject(with: data, options: JSONSerialization.ReadingOptions.allowFragments)
                    if let paymentSearchDic = jsonResult as? NSDictionary {
                        if paymentSearchDic["error"] != nil {
                            let apiException = try JSONDecoder().decode(PXApiException.self, from: data) as PXApiException
                            failure(PaymentMethodSearchService.getError(code: ErrorTypes.API_EXCEPTION_ERROR, apiException: apiException))
                        } else {
                            if paymentSearchDic.allKeys.count > 0 {
                                let initDTO = try JSONDecoder().decode(PXInitDTO.self, from: data) as PXInitDTO
                                success(initDTO)
                            } else {
                                failure(PaymentMethodSearchService.getError())
                            }
                        }
                    }
                } catch {
                    printDebug("PaymentMethodSearchService getInit error: \(error).")
                    failure(PaymentMethodSearchService.getError())
                }

        }, failure: { (_) -> Void in
            failure(PaymentMethodSearchService.getError(code: ErrorTypes.NO_INTERNET_ERROR, reason: "Verifique su conexión a internet e intente nuevamente"))
        })
    }
    
    private static func getError(code: Int = ErrorTypes.API_UNKNOWN_ERROR,
                                 reason: String = "No se ha podido obtener los métodos de pago",
                                 apiException: PXApiException? = nil) -> PXError {
        return PXError(domain: ApiDomain.GET_PAYMENT_METHODS, code: code, userInfo: [NSLocalizedDescriptionKey: "Hubo un error", NSLocalizedFailureReasonErrorKey: reason], apiException: apiException)
    }

    internal func getOpenPrefInit(pref: PXCheckoutPreference, cardsWithEsc: [String], splitEnabled: Bool, discountParamsConfiguration: PXDiscountParamsConfiguration?, flow: String?, charges: [PXPaymentTypeChargeRule], headers: [String: String]?, success: @escaping (_ paymentMethodSearch: PXInitDTO) -> Void, failure: @escaping ((_ error: PXError) -> Void)) {
{'fraction_non_alphanumeric': 0.0898814447201544, 'fraction_numerical': 0.0027570995312930797, 'mean_word_length': 2.3173296753543666, 'pattern_counts': {'":': 0, '<': 5, '<?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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '262159', 'n_tokens_mistral': 1944, 'n_tokens_neox': 1876, 'n_words': 459}