<commit_before>//
//  DataRequest.swift
//  Alamofire
//
//  Created by <NAME> on 9/25/17.
//

import Foundation

internal struct DataRequestor {
	
	internal typealias ResponseHandler = (Data?, URLResponse?, Error?) throws -> Data

	internal private(set) var connectedApp: ConnectedApp
	internal private(set) var session: URLSession
	
	internal init(connectedApp: ConnectedApp, session: URLSession = URLSession.shared) {
		self.connectedApp = connectedApp
		self.session = session
	}
	
	internal static let defaultResponseHandler: (Data?, URLResponse?, Error?) throws -> Data = {
		(data, response, error) throws in
		if let error = error {
			throw error
		}
		guard let resp = response, let httpResp = resp as? HTTPURLResponse, let data = data else {
			throw  SalesforceError.unexpectedResponse(response: response)
		}
		switch httpResp.statusCode {
		case 200..<300:
			return data
		case 401:
			throw SalesforceError.userAuthenticationRequired
		case 400..<500:
			// Try to form error from Salesforce's response
			// See: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/errorcodes.htm
			if let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: Any]],
				let firstError = json?[0],
				let errorCode = firstError["errorCode"] as? String,
				let message = firstError["message"] as? String {
				throw SalesforceError.resourceException(code: errorCode, message: message, fields: firstError["fields"] as? [String])
			}
			else {
				throw SalesforceError.unexpectedResponse(response: response)
			}
		case 500:
			throw SalesforceError.serverFailure
		default:
			throw SalesforceError.unexpectedResponse(response: response)
		}
	}
	
	internal func request(resource: Resource, responseHandler: @escaping ResponseHandler = DataRequestor.defaultResponseHandler) -> Promise<Data> {
		
		let _request = {
			(req: URLRequest) -> Promise<Data> in
			return Promise {
				(fulfill, reject) -> () in
				let task: URLSessionDataTask = URLSession.shared.dataTask(with: req) {
					(data, resp, err) -> Void in
					do {
						fulfill(try responseHandler(data, resp, err))
					}
					catch {
						reject(error)
					}
				}
				task.resume()
			}
		}
		
		return Promise<OAuth2Result> {
			(fulfill, reject) -> () in
			if let auth = connectedApp.authData {
				fulfill(auth)
			}
			else {
				reject(SalesforceError.userAuthenticationRequired)
			}
		}.then {
			return try _request(resource.asURLRequest(authData: $0))
		}.recover {
			(error: Error) -> Promise<Data> in
			if case SalesforceError.userAuthenticationRequired = error {
				return self.connectedApp.authorize().then {
					return try _request(resource.asURLRequest(authData: $0))
				}
			}
			else {
				throw error
			}
		}
	}
}<commit_msg>Handle HTTP 404 error<commit_after>//
//  DataRequest.swift
//  Alamofire
//
//  Created by <NAME> on 9/25/17.
//

import Foundation

internal struct DataRequestor {
	
	internal typealias ResponseHandler = (Data?, URLResponse?, Error?) throws -> Data

	internal private(set) var connectedApp: ConnectedApp
	internal private(set) var session: URLSession
	
	internal init(connectedApp: ConnectedApp, session: URLSession = URLSession.shared) {
		self.connectedApp = connectedApp
		self.session = session
	}
	
	internal static let defaultResponseHandler: (Data?, URLResponse?, Error?) throws -> Data = {
		(data, response, error) throws in
		if let error = error {
			throw error
		}
		guard let resp = response, let httpResp = resp as? HTTPURLResponse, let data = data else {
			throw  SalesforceError.unexpectedResponse(response: response)
		}
		switch httpResp.statusCode {
		case 200..<300:
			return data
		case 401:
			throw SalesforceError.userAuthenticationRequired
		case 404:
			throw SalesforceError.resourceNotFound
		case 400..<500:
			// Try to form error from Salesforce's response
			// See: https://developer.salesforce.com/docs/atlas.en-us.api_rest.meta/api_rest/errorcodes.htm
			if let json = try? JSONSerialization.jsonObject(with: data, options: .allowFragments) as? [[String: Any]],
				let firstError = json?[0],
				let errorCode = firstError["errorCode"] as? String,
				let message = firstError["message"] as? String {
				throw SalesforceError.resourceException(code: errorCode, message: message, fields: firstError["fields"] as? [String])
			}
			else {
				throw SalesforceError.unexpectedResponse(response: response)
			}
		case 500:
			throw SalesforceError.serverFailure
		default:
			throw SalesforceError.unexpectedResponse(response: response)
		}
	}
	
	internal func request(resource: Resource, responseHandler: @escaping ResponseHandler = DataRequestor.defaultResponseHandler) -> Promise<Data> {
		
		let go = {
			(req: URLRequest) -> Promise<Data> in
			return Promise {
				(fulfill, reject) -> () in
				let task: URLSessionDataTask = self.session.dataTask(with: req) {
					(data, resp, err) -> Void in
					do {
						fulfill(try responseHandler(data, resp, err))
					}
					catch {
						reject(error)
					}
				}
				task.resume()
			}
		}
		
		return Promise<OAuth2Result> {
			(fulfill, reject) -> () in
			if let auth = connectedApp.authData {
				fulfill(auth)
			}
			else {
				reject(SalesforceError.userAuthenticationRequired)
			}
		}.then {
			return try go(resource.asURLRequest(authData: $0))
		}.recover {
			(error: Error) -> Promise<Data> in
			if case SalesforceError.userAuthenticationRequired = error {
				return self.connectedApp.authorize().then {
					return try go(resource.asURLRequest(authData: $0))
				}
			}
			else {
				throw error
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.10970162587100232, 'fraction_numerical': 0.010720028586742899, 'mean_word_length': 3.543831168831169, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 29, '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': '6495573', 'n_tokens_mistral': 2061, 'n_tokens_neox': 1670, 'n_words': 439}