<commit_before>import Foundation

/// The WXKDarkSkyRequest class contains some utilities for working with the Dark Sky API in Swift.
public class WXKDarkSkyRequest {
	/// Your Dark Sky API key.
	var key : String
	
	public init(key: String) {
		self.key = key
	}
	
	/// Loads data from the Dark Sky API for a given point and set of options.
	/// - parameter point: The latitude-longitude point for the data request.
	/// - parameter time: The time for a Time Machine request; defaults to nil for current data.
	/// - parameter options: A set of options for fulfilling the request, such as units and language.
	/// - parameter completionHandler: A code block to handle the successful completion, or errors in completion, of the request.
	public func loadData(point: Point, time: Date? = nil, options: Options = Options.defaults, completionHandler: @escaping (WXKDarkSkyResponse?, Error?) -> Void) {
		// Set up a data task variable.
		var dataTask : URLSessionDataTask?
		
		// If there's an ongoing data task, cancel it.
		dataTask?.cancel()
		
		// Build a Dark Sky API URL.
		let url = self.buildDarkSkyURL(point: point, time: time, options: options)
		
		if let url = url {
			// Set up a URL load request.
			let session = URLSession(configuration: .default)
			dataTask = session.dataTask(with: url, completionHandler: { data, response, error in
				if let error = error {
					completionHandler(nil, error)
				} else if let data = data, let response = response as? HTTPURLResponse, response.statusCode == 200 {
					// Successfully retrieved data from the Dark Sky API.
					let decoder = JSONDecoder()
					decoder.dateDecodingStrategy = .secondsSince1970
					
					do {
						// Attempt to decode the response into a WXKDarkSkyResponse object.
						let decoded = try decoder.decode(WXKDarkSkyResponse.self, from: data)
						completionHandler(decoded, nil)
					} catch {
						// Something was wrong with the response such that it could not be decoded.
						completionHandler(nil, WXKDarkSkyError.malformedResponse)
					}
				} else {
					// ...something went wrong? Received data, but the status code was not 200 (OK).
					completionHandler(nil, WXKDarkSkyError.couldNotRetrieveData)
				}
			})
			
			dataTask?.resume()
		} else {
			// Some error occurred in...generating the URL. The circumstances behind this are so unlikely that this will likely never be called, but it's helpful to open a door to handle it.
			completionHandler(nil, WXKDarkSkyError.unspecified)
		}
	}
	
	/// Builds a URL for a Dark Sky API requests.
	/// - parameter key: The API key to use for the request.
	/// - parameter point: A latitude-longitude pair for the request.
	/// - parameter time: If present, the time for a Time Machine request before or after the current time.
	/// - parameter options: Options to use for the request.
	/// - returns: If a URL can be created, returns a `URL`. If not, returns nil.
	internal func buildDarkSkyURL(point: Point, time: Date? = nil, options: Options = Options.defaults) -> URL? {
		/// String describing the requested latitude-longitude pair.
		let coordinates = String(describing: point)
		
		// Build a URL to query.
		var components = URLComponents()
		components.scheme = "https"
		components.host = "api.darksky.net"
		if let time = time {
			// If this is a Time Machine request, modify the path to include a time.
			let unixTime = String(describing: time.unixTime)
			components.path = "/forecast/\(key)/\(coordinates),\(unixTime)"
		} else {
			// If no time was given, assume that we're getting the current data.
			components.path = "/forecast/\(key)/\(coordinates)"
		}
		
		switch options {
		case Options.defaults:
			break
		default:
			// If any of the options differ from the defaults, build a query string.
			components.queryItems = []
		
			// Now, from the options, build a query string.
			// If data blocks are to be excluded, exclude them.
			if options.exclude != Options.defaults.exclude {
				// We have to have a roundabout way of getting the exclusion list here.<commit_msg>Make Dark Sky API URL request builder public<commit_after>import Foundation

/// The WXKDarkSkyRequest class contains some utilities for working with the Dark Sky API in Swift.
public class WXKDarkSkyRequest {
	/// Your Dark Sky API key.
	var key : String
	
	public init(key: String) {
		self.key = key
	}
	
	/// Loads data from the Dark Sky API for a given point and set of options.
	/// - parameter point: The latitude-longitude point for the data request.
	/// - parameter time: The time for a Time Machine request; defaults to nil for current data.
	/// - parameter options: A set of options for fulfilling the request, such as units and language.
	/// - parameter completionHandler: A code block to handle the successful completion, or errors in completion, of the request.
	public func loadData(point: Point, time: Date? = nil, options: Options = Options.defaults, completionHandler: @escaping (WXKDarkSkyResponse?, Error?) -> Void) {
		// Set up a data task variable.
		var dataTask : URLSessionDataTask?
		
		// If there's an ongoing data task, cancel it.
		dataTask?.cancel()
		
		// Build a Dark Sky API URL.
		let url = self.buildDarkSkyURL(point: point, time: time, options: options)
		
		if let url = url {
			// Set up a URL load request.
			let session = URLSession(configuration: .default)
			dataTask = session.dataTask(with: url, completionHandler: { data, response, error in
				if let error = error {
					completionHandler(nil, error)
				} else if let data = data, let response = response as? HTTPURLResponse, response.statusCode == 200 {
					// Successfully retrieved data from the Dark Sky API.
					let decoder = JSONDecoder()
					decoder.dateDecodingStrategy = .secondsSince1970
					
					do {
						// Attempt to decode the response into a WXKDarkSkyResponse object.
						let decoded = try decoder.decode(WXKDarkSkyResponse.self, from: data)
						completionHandler(decoded, nil)
					} catch {
						// Something was wrong with the response such that it could not be decoded.
						completionHandler(nil, WXKDarkSkyError.malformedResponse)
					}
				} else {
					// ...something went wrong? Received data, but the status code was not 200 (OK).
					completionHandler(nil, WXKDarkSkyError.couldNotRetrieveData)
				}
			})
			
			dataTask?.resume()
		} else {
			// Some error occurred in...generating the URL. The circumstances behind this are so unlikely that this will likely never be called, but it's helpful to open a door to handle it.
			completionHandler(nil, WXKDarkSkyError.unspecified)
		}
	}
	
	/// Builds a URL for a Dark Sky API requests.
	/// - parameter key: The API key to use for the request.
	/// - parameter point: A latitude-longitude pair for the request.
	/// - parameter time: If present, the time for a Time Machine request before or after the current time.
	/// - parameter options: Options to use for the request.
	/// - returns: If a URL can be created, returns a `URL`. If not, returns nil.
	public func buildDarkSkyURL(point: Point, time: Date? = nil, options: Options = Options.defaults) -> URL? {
		/// String describing the requested latitude-longitude pair.
		let coordinates = String(describing: point)
		
		// Build a URL to query.
		var components = URLComponents()
		components.scheme = "https"
		components.host = "api.darksky.net"
		if let time = time {
			// If this is a Time Machine request, modify the path to include a time.
			let unixTime = String(describing: time.unixTime)
			components.path = "/forecast/\(key)/\(coordinates),\(unixTime)"
		} else {
			// If no time was given, assume that we're getting the current data.
			components.path = "/forecast/\(key)/\(coordinates)"
		}
		
		switch options {
		case Options.defaults:
			break
		default:
			// If any of the options differ from the defaults, build a query string.
			components.queryItems = []
		
			// Now, from the options, build a query string.
			// If data blocks are to be excluded, exclude them.
			if options.exclude != Options.defaults.exclude {
				// We have to have a roundabout way of getting the exclusion list here.
{'fraction_non_alphanumeric': 0.09067389429592214, 'fraction_numerical': 0.0024639645189109276, 'mean_word_length': 3.0958627648839556, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 7, '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': '3005354', 'n_tokens_mistral': 2619, 'n_tokens_neox': 2192, 'n_words': 964}