<commit_before>//
//  APIClient.swift
//  mstdn
//
//  Created by tarunon on 2017/04/22.
//  Copyright © 2017年 tarunon. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa

struct APIClientRequestProxy<Base: Request>: RequestProxy {
    let base: Base
    let clientURL: URL
    
    var baseURL: URL {
        return clientURL
    }
}

open class APIClient {
    let baseURL: URL
    let session: URLSession
    public init(baseURL: URL, session: URLSession = URLSession.shared) {
        self.baseURL = baseURL
        self.session = session
    }
    
    open func request<R: Request>(request: @autoclosure @escaping () throws -> R) -> Observable<R.Response> {
        return Observable<() throws -> R>.just(request)
            .map { try $0() }
            .map { APIClientRequestProxy(base: $0, clientURL: self.baseURL) }
            .flatMap { (request) in
                return try self.session.rx.response(request: request.buildURLRequest())
                    .map { (response, data) in
                        try request.parse(data: data, urlResponse: response)
                    }
            }
    }
}
<commit_msg>refactoring<commit_after>//
//  APIClient.swift
//  mstdn
//
//  Created by tarunon on 2017/04/22.
//  Copyright © 2017年 tarunon. All rights reserved.
//

import Foundation
import RxSwift
import RxCocoa


open class APIClient {
    struct _RequestProxy<Base: Request>: RequestProxy {
        let base: Base
        let clientURL: URL
        
        var baseURL: URL {
            return clientURL
        }
    }
    
    let baseURL: URL
    let session: URLSession
    public init(baseURL: URL, session: URLSession = URLSession.shared) {
        self.baseURL = baseURL
        self.session = session
    }
    
    open func request<R: Request>(request: @autoclosure @escaping () throws -> R) -> Observable<R.Response> {
        return Observable<() throws -> R>.just(request)
            .map { try $0() }
            .map { _RequestProxy(base: $0, clientURL: self.baseURL) }
            .flatMap { (request) in
                return try self.session.rx.response(request: request.buildURLRequest())
                    .map { (response, data) in
                        try request.parse(data: data, urlResponse: response)
                    }
            }
    }
}

{'fraction_non_alphanumeric': 0.10485603781693167, 'fraction_numerical': 0.012032660077352814, 'mean_word_length': 1.9319899244332495, 'pattern_counts': {'":': 0, '<': 11, '<?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': '647677', 'n_tokens_mistral': 703, 'n_tokens_neox': 675, 'n_words': 184}