Repository: Moya/Moya
File: Moya/Moya.swift (Swift)

Code before:
    public init(endpointClosure: MoyaEndpointsClosure = MoyaProvider.DefaultEndpointMapping, endpointResolver: MoyaEndpointResolution = MoyaProvider.DefaultEndpointResolution, stubBehavior: MoyaStubbedBehavior = MoyaProvider.NoStubbingBehavior, credentialClosure: MoyaCredentialClosure? = nil, networkActivityClosure: Moya.NetworkActivityClosure? = nil, manager: Manager = Alamofire.Manager.sharedInstance) {
        self.endpointClosure = endpointClosure
        self.endpointResolver = endpointResolver
        self.credentialClosure = credentialClosure
        self.stubBehavior = stubBehavior
        self.networkActivityClosure = networkActivityClosure
        self.manager = manager
    }
    
    /// Returns an Endpoint based on the token, method, and parameters by invoking the endpointsClosure.
    public func endpoint(token: T) -> Endpoint<T> {
        return endpointClosure(token)
    }
    
    /// Designated request-making method.
    public func request(token: T, completion: MoyaCompletion) -> Cancellable {
        let endpoint = self.endpoint(token)
        let request = endpointResolver(endpoint: endpoint)
        let credential = credentialClosure?(token)
        let stubBehavior = self.stubBehavior(token)

        switch stubBehavior {
        case .NoStubbing:
            return sendRequest(request, credential:credential, completion: completion)
        default:
            return stubRequest(request, completion: completion, endpoint: endpoint, stubBehavior: stubBehavior)
        }
    }

    public class func DefaultEndpointMapping(target: T) -> Endpoint<T> {
        let url = target.baseURL.URLByAppendingPathComponent(target.path).absoluteString
        return Endpoint(URL: url!, sampleResponse: .Success(200, {target.sampleData}), method: target.method, parameters: target.parameters)
    }

    public class func DefaultEnpointResolution(endpoint: Endpoint<T>) -> NSURLRequest {
        return DefaultEndpointResolution(endpoint)
    }

    public class func DefaultEndpointResolution(endpoint: Endpoint<T>) -> NSURLRequest {
        return endpoint.urlRequest
    }

    public class func NoStubbingBehavior(_: T) -> Moya.StubbedBehavior {
        return .NoStubbing
    }

    public class func ImmediateStubbingBehaviour(_: T) -> Moya.StubbedBehavior {
        return .Immediate
    }

    public class func DelayedStubbingBehaviour(seconds: NSTimeInterval) -> MoyaStubbedBehavior {

Review comment (security):
Do you think it could be valuable to use the credentials when stubbing a request, too? I can't _really_ see how, but wanted to see if it was something we might want to add. 


Code after:
    public init(endpointClosure: MoyaEndpointsClosure = MoyaProvider.DefaultEndpointMapping, endpointResolver: MoyaEndpointResolution = MoyaProvider.DefaultEndpointResolution, stubBehavior: MoyaStubbedBehavior = MoyaProvider.NoStubbingBehavior, credentialClosure: MoyaCredentialClosure? = nil, networkActivityClosure: Moya.NetworkActivityClosure? = nil, manager: Manager = Alamofire.Manager.sharedInstance) {
        self.endpointClosure = endpointClosure
        self.endpointResolver = endpointResolver
        self.credentialClosure = credentialClosure
        self.stubBehavior = stubBehavior
        self.networkActivityClosure = networkActivityClosure
        self.manager = manager
    }

    /// Returns an Endpoint based on the token, method, and parameters by invoking the endpointsClosure.
    public func endpoint(token: T) -> Endpoint<T> {
        return endpointClosure(token)
    }

    /// Designated request-making method.
    public func request(token: T, completion: MoyaCompletion) -> Cancellable {
        let endpoint = self.endpoint(token)
        let request = endpointResolver(endpoint: endpoint)
        let credential = credentialClosure?(token)
        let stubBehavior = self.stubBehavior(token)

        switch stubBehavior {
        case .NoStubbing:
            return sendRequest(request, credential:credential, completion: completion)
        default:
            return stubRequest(request, completion: completion, endpoint: endpoint, stubBehavior: stubBehavior)
        }
    }

    public class func DefaultEndpointMapping(target: T) -> Endpoint<T> {
        let url = target.baseURL.URLByAppendingPathComponent(target.path).absoluteString
        return Endpoint(URL: url!, sampleResponse: .Success(200, {target.sampleData}), method: target.method, parameters: target.parameters)
    }

    @availability(*, unavailable, renamed="DefaultEndpointResolution", message="Use #DefaultEndpointResolution method instead")
    public class func DefaultEnpointResolution(endpoint: Endpoint<T>) -> NSURLRequest {
        return DefaultEndpointResolution(endpoint)
    }

    public class func DefaultEndpointResolution(endpoint: Endpoint<T>) -> NSURLRequest {
        return endpoint.urlRequest
    }

    public class func NoStubbingBehavior(_: T) -> Moya.StubbedBehavior {
        return .NoStubbing
    }

    public class func ImmediateStubbingBehaviour(_: T) -> Moya.StubbedBehavior {
        return .Immediate
    }