                                 .mapError(Error.jsonDecodingError)
                                 .flatMap { error in
                                     .failure(Error.apiError(response.statusCode, Response(headerFields: headers), error))
-                                }
+                            }
                         }
                         return .success(JSON)
                     }
                     .map { JSON in
                         return (Response(headerFields: headers), JSON)
-                    }
-            }
+                }
+        }
     }
     
     /// Fetch an object from the API.

    /// Fetch an endpoint from the API.
    private func fetch(_ endpoint: Endpoint, page: UInt?, pageSize: UInt?) -> SignalProducer<(Response, Any), Error> {
        let url = URL(server, endpoint, page: page, pageSize: pageSize)
        let request = URLRequest.create(url, credentials)
        return urlSession
            .reactive
            .data(with: request)
            .mapError { Error.networkError($0.error) }
            .flatMap(.concat) { data, response -> SignalProducer<(Response, Any), Error> in
                let response = response as! HTTPURLResponse

    internal func send<Request: RequestType>(_ request: Request, to endpoint: Endpoint) -> SignalProducer<(Response, Request.Response), Error> where Request.Response == Request.Response.DecodedType {
        let urlRequest = URLRequest.create(URL(server, endpoint), request, credentials)

        return fetch(urlRequest)
            .attemptMap { response, JSON in
                return decode(JSON)
                    .map { resource in
                        (response, resource)
                    }
                    .mapError(Error.jsonDecodingError)
        }
    }
}

