<commit_before>//
//  NSMutableURLRequest+reddift.swift
//  reddift
//
//  Created by sonson on 2015/04/13.
//  Copyright (c) 2015年 sonson. All rights reserved.
//

import Foundation

func parameterString(dictionary:[String:String])-> String {
    var buf = ""
    for (key, value) in dictionary {
        buf += "\(key)=\(value)&"
    }
    if count(buf) > 0 {
        var range = Range<String.Index>(start: advance(buf.endIndex, -1), end: buf.endIndex)
        buf.removeRange(range)
    }
    return buf
}

extension NSMutableURLRequest {
    func setRedditBasicAuthentication() {
        var basicAuthenticationChallenge = Config.sharedInstance.clientID + ":"
        let data = basicAuthenticationChallenge.dataUsingEncoding(NSUTF8StringEncoding)!
        let base64Str = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
        setValue("Basic " + base64Str, forHTTPHeaderField:"Authorization")
    }
    
    func setRedditBasicAuthentication(#username:String, password:String) {
        var basicAuthenticationChallenge = username + ":" + password
        let data = basicAuthenticationChallenge.dataUsingEncoding(NSUTF8StringEncoding)!
        let base64Str = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
        setValue("Basic " + base64Str, forHTTPHeaderField:"Authorization")
    }
    
    func setOAuth2Token(token:OAuth2Token) {
        setValue("bearer " + token.accessToken, forHTTPHeaderField:"Authorization")
    }
    
    func setUserAgentForReddit() {
        self.setValue(Config.sharedInstance.userAgent, forHTTPHeaderField: "User-Agent")
    }
    
    class func mutableOAuthRequestWithBaseURL(baseURL:String, path:String, method:String, token:OAuth2Token) -> NSMutableURLRequest {
        let URL = NSURL(string:baseURL + path)!
        var URLRequest = NSMutableURLRequest(URL: URL)
        URLRequest.setOAuth2Token(token)
        URLRequest.HTTPMethod = method
        URLRequest.setUserAgentForReddit()
        return URLRequest
    }
    
    class func mutableOAuthRequestWithBaseURL(baseURL:String, path:String, parameter:[String:String]?, method:String, token:OAuth2Token) -> NSMutableURLRequest {
		var params:[String:String] = [:]
		if let parameter = parameter {
			params = parameter
		}
        if method == "POST" {
            return self.mutableOAuthPostRequestWithBaseURL(baseURL, path:path, parameter:params, method:method, token:token)
        }
        else {
            return self.mutableOAuthGetRequestWithBaseURL(baseURL, path:path, parameter:params, method:method, token:token)
        }
    }
    
    class func mutableOAuthGetRequestWithBaseURL(baseURL:String, path:String, parameter:[String:String], method:String, token:OAuth2Token) -> NSMutableURLRequest {
        let URL = NSURL(string:baseURL + path + "?" + parameterString(parameter))!
        var URLRequest = NSMutableURLRequest(URL: URL)
        URLRequest.setOAuth2Token(token)
        URLRequest.HTTPMethod = method
        URLRequest.setUserAgentForReddit()
        return URLRequest
    }
    
    class func mutableOAuthPostRequestWithBaseURL(baseURL:String, path:String, parameter:[String:String], method:String, token:OAuth2Token) -> NSMutableURLRequest {
        let URL = NSURL(string:baseURL + path)!
        var URLRequest = NSMutableURLRequest(URL: URL)
        URLRequest.setOAuth2Token(token)
        URLRequest.HTTPMethod = method
        let data = parameterString(parameter).dataUsingEncoding(NSUTF8StringEncoding)
        URLRequest.HTTPBody = data
        URLRequest.setUserAgentForReddit()
        return URLRequest<commit_msg>Removed ? as suffix when URL does not have any queries.<commit_after>//
//  NSMutableURLRequest+reddift.swift
//  reddift
//
//  Created by sonson on 2015/04/13.
//  Copyright (c) 2015年 sonson. All rights reserved.
//

import Foundation

func parameterString(dictionary:[String:String])-> String {
    var buf = ""
    for (key, value) in dictionary {
        buf += "\(key)=\(value)&"
    }
    if count(buf) > 0 {
        var range = Range<String.Index>(start: advance(buf.endIndex, -1), end: buf.endIndex)
        buf.removeRange(range)
    }
    return buf
}

extension NSMutableURLRequest {
    func setRedditBasicAuthentication() {
        var basicAuthenticationChallenge = Config.sharedInstance.clientID + ":"
        let data = basicAuthenticationChallenge.dataUsingEncoding(NSUTF8StringEncoding)!
        let base64Str = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
        setValue("Basic " + base64Str, forHTTPHeaderField:"Authorization")
    }
    
    func setRedditBasicAuthentication(#username:String, password:String) {
        var basicAuthenticationChallenge = username + ":" + password
        let data = basicAuthenticationChallenge.dataUsingEncoding(NSUTF8StringEncoding)!
        let base64Str = data.base64EncodedStringWithOptions(NSDataBase64EncodingOptions.Encoding64CharacterLineLength)
        setValue("Basic " + base64Str, forHTTPHeaderField:"Authorization")
    }
    
    func setOAuth2Token(token:OAuth2Token) {
        setValue("bearer " + token.accessToken, forHTTPHeaderField:"Authorization")
    }
    
    func setUserAgentForReddit() {
        self.setValue(Config.sharedInstance.userAgent, forHTTPHeaderField: "User-Agent")
    }
    
    class func mutableOAuthRequestWithBaseURL(baseURL:String, path:String, method:String, token:OAuth2Token) -> NSMutableURLRequest {
        let URL = NSURL(string:baseURL + path)!
        var URLRequest = NSMutableURLRequest(URL: URL)
        URLRequest.setOAuth2Token(token)
        URLRequest.HTTPMethod = method
        URLRequest.setUserAgentForReddit()
        return URLRequest
    }
    
    class func mutableOAuthRequestWithBaseURL(baseURL:String, path:String, parameter:[String:String]?, method:String, token:OAuth2Token) -> NSMutableURLRequest {
		var params:[String:String] = [:]
		if let parameter = parameter {
			params = parameter
		}
        if method == "POST" {
            return self.mutableOAuthPostRequestWithBaseURL(baseURL, path:path, parameter:params, method:method, token:token)
        }
        else {
            return self.mutableOAuthGetRequestWithBaseURL(baseURL, path:path, parameter:params, method:method, token:token)
        }
    }
    
    class func mutableOAuthGetRequestWithBaseURL(baseURL:String, path:String, parameter:[String:String], method:String, token:OAuth2Token) -> NSMutableURLRequest {
        var param = parameterString(parameter)
        let URL = isEmpty(param) ? NSURL(string:baseURL + path)! : NSURL(string:baseURL + path + "?" + param)!
        var URLRequest = NSMutableURLRequest(URL: URL)
        URLRequest.setOAuth2Token(token)
        URLRequest.HTTPMethod = method
        URLRequest.setUserAgentForReddit()
        return URLRequest
    }
    
    class func mutableOAuthPostRequestWithBaseURL(baseURL:String, path:String, parameter:[String:String], method:String, token:OAuth2Token) -> NSMutableURLRequest {
        let URL = NSURL(string:baseURL + path)!
        var URLRequest = NSMutableURLRequest(URL: URL)
        URLRequest.setOAuth2Token(token)
        URLRequest.HTTPMethod = method
        let data = parameterString(parameter).dataUsingEncoding(NSUTF8StringEncoding)
        URLRequest.HTTPBody = data
        URLRequest.setUserAgentForReddit()
        return URLRequest
{'fraction_non_alphanumeric': 0.09573752857334947, 'fraction_numerical': 0.01237057953475864, 'mean_word_length': 3.672110552763819, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 17, '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': '3221062', 'n_tokens_mistral': 2197, 'n_tokens_neox': 2086, 'n_words': 388}