Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rename makeResponse to response #1672

Merged
merged 1 commit into from
May 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Sources/Development/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ do {
print(loginRequest.email) // user@vapor.codes
print(loginRequest.password) // don't look!

return req.makeResponse()
return req.response()
}

router.get("string", String.parameter) { req -> String in
Expand All @@ -114,7 +114,7 @@ do {
}

router.get("fast") { req -> Response in
let res = req.makeResponse()
let res = req.response()
res.http.body = HTTPBody(string: "123")
return res
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Vapor/Content/Content.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public protocol Content: Codable, ResponseCodable, RequestCodable {
/// }
///
/// router.get("greeting2") { req in
/// let res = req.makeResponse()
/// let res = req.response()
/// try res.content.encode(Hello(), as: .json)
/// return res // {"message":"Hello!"}
/// }
Expand Down Expand Up @@ -62,7 +62,7 @@ extension Content {
///
/// See `ResponseEncodable`.
public func encode(for req: Request) throws -> Future<Response> {
let res = req.makeResponse()
let res = req.response()
try res.content.encode(self)
return Future.map(on: req) { res }
}
Expand Down
14 changes: 14 additions & 0 deletions Sources/Vapor/Deprecated.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,3 +36,17 @@ extension MemorySessions {
self.init()
}
}

extension Request {
/// See `req.response(http:)`.
@available(*, deprecated, renamed: "response(http:)")
public func makeResponse(http: HTTPResponse = .init()) -> Response {
return response(http: http)
}

/// See `req.response(_ body:as:)`.
@available(*, deprecated, renamed: "response(_body:as:)")
public func makeResponse(_ body: LosslessHTTPBodyRepresentable, as contentType: MediaType = .plainText) -> Response {
return response(body, as: contentType)
}
}
2 changes: 1 addition & 1 deletion Sources/Vapor/Middleware/CORSMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public final class CORSMiddleware: Middleware {
// Determine if the request is pre-flight.
// If it is, create empty response otherwise get response from the responder chain.
let response = request.isPreflight
? request.eventLoop.newSucceededFuture(result: request.makeResponse())
? request.eventLoop.newSucceededFuture(result: request.response())
: try next.respond(to: request)

return response.map { response in
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Middleware/ErrorMiddleware.swift
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ public final class ErrorMiddleware: Middleware, ServiceType {
}

// create a Response with appropriate status
let res = req.makeResponse(http: .init(status: status, headers: headers))
let res = req.response(http: .init(status: status, headers: headers))

// attempt to serialize the error to json
do {
Expand Down
14 changes: 7 additions & 7 deletions Sources/Vapor/Request/Request.swift
Original file line number Diff line number Diff line change
Expand Up @@ -123,36 +123,36 @@ public final class Request: ContainerAlias, DatabaseConnectable, HTTPMessageCont
}

// MARK: Response

/// Creates a `Response` on the same container as this `Request`.
///
/// router.get("greeting2") { req in
/// let res = req.makeResponse()
/// let res = req.response()
/// try res.content.encode("hello", as: .plaintext)
/// return res
/// }
///
/// - parameters:
/// - http: Optional `HTTPResponse` to use.
/// - returns: A new, empty 200 OK `Response` on the same container as the current `Request`.
public func makeResponse(http: HTTPResponse = .init()) -> Response {
public func response(http: HTTPResponse = .init()) -> Response {
return Response(http: http, using: sharedContainer)
}

/// Generate a `Response` for a `HTTPBody` convertible object using the supplied `MediaType`.
///
/// router.get("html") { req in
/// return req.makeResponse("<h1>Hello, world!</h1>", as: .html)
/// return req.response("<h1>Hello, world!</h1>", as: .html)
/// }
///
/// - parameters:
/// - type: The type of data to return the container with.
public func makeResponse(_ body: LosslessHTTPBodyRepresentable, as contentType: MediaType = .plainText) -> Response {
let res = makeResponse(http: .init(body: body))
public func response(_ body: LosslessHTTPBodyRepresentable, as contentType: MediaType = .plainText) -> Response {
let res = response(http: .init(body: body))
res.http.contentType = contentType
return res
}

// MARK: Database

/// See `DatabaseConnectable`.
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Response/ApplicationResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ private struct RouterResponder: Responder {
/// See `Responder`.
func respond(to req: Request) throws -> Future<Response> {
guard let responder = router.route(request: req) else {
let res = req.makeResponse(http: .init(status: .notFound, body: "Not found"))
let res = req.response(http: .init(status: .notFound, body: "Not found"))
return req.eventLoop.newSucceededFuture(result: res)
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Response/BasicResponder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ public struct BasicResponder: Responder {
/// Create a new `BasicResponder`.
///
/// let notFound: Responder = BasicResponder { req in
/// let res = req.makeResponse(http: .init(status: .notFound))
/// let res = req.response(http: .init(status: .notFound))
/// return req.eventLoop.newSucceededFuture(result: res)
/// }
///
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Response/Redirect.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ extension Request {
/// Set type to '.permanently' to allow caching to automatically redirect from browsers.
/// Defaulting to non-permanent to prevent unexpected caching.
public func redirect(to location: String, type: RedirectType = .normal) -> Response {
let res = makeResponse()
let res = response()
res.http.status = type.status
res.http.headers.replaceOrAdd(name: .location, value: location)
return res
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Response/ResponseCodable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ extension ResponseEncodable {
extension HTTPResponse: ResponseEncodable {
/// See `ResponseEncodable`.
public func encode(for req: Request) throws -> Future<Response> {
let new = req.makeResponse()
let new = req.response()
new.http = self
return req.eventLoop.newSucceededFuture(result: new)
}
Expand Down
2 changes: 1 addition & 1 deletion Sources/Vapor/Utilities/FileIO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ extension Request {
///
/// See `FileIO` for more information.
public func streamFile(at path: String) throws -> Future<Response> {
let res = makeResponse()
let res = response()
res.http = try fileio().chunkedResponse(file: path, for: http)
return eventLoop.newSucceededFuture(result: res)
}
Expand Down
8 changes: 4 additions & 4 deletions Tests/VaporTests/ApplicationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ class ApplicationTests: XCTestCase {

let app = try Application.makeTest { router in
router.get("encode") { req -> Response in
let res = req.makeResponse()
let res = req.response()
try res.content.encode(FooContent())
try res.content.encode(FooContent(), as: .json)
try res.content.encode(FooEncodable(), as: .json)
Expand Down Expand Up @@ -328,7 +328,7 @@ class ApplicationTests: XCTestCase {
func testCustomEncode() throws {
try Application.makeTest { router in
router.get("custom-encode") { req -> Response in
let res = req.makeResponse(http: .init(status: .ok))
let res = req.response(http: .init(status: .ok))
try res.content.encode(json: ["hello": "world"], using: .custom(format: .prettyPrinted))
return res
}
Expand Down Expand Up @@ -525,7 +525,7 @@ class ApplicationTests: XCTestCase {
// without specific content type
try Application.makeTest { router in
router.get("hello") { req in
return req.makeResponse("Hello!")
return req.response("Hello!")
}
}.test(.GET, "hello") { res in
XCTAssertEqual(res.http.status, .ok)
Expand All @@ -535,7 +535,7 @@ class ApplicationTests: XCTestCase {
// with specific content type
try Application.makeTest { router in
router.get("hello-html") { req -> Response in
return req.makeResponse("Hey!", as: .html)
return req.response("Hey!", as: .html)
}
}.test(.GET, "hello-html") { res in
XCTAssertEqual(res.http.status, .ok)
Expand Down
2 changes: 1 addition & 1 deletion Tests/VaporTests/ClientTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ final class LastRequestClient: Client {
}
func send(_ req: Request) -> Future<Response> {
lastReq = req
return Future.map(on: req) { req.makeResponse() }
return Future.map(on: req) { req.response() }
}
}

Expand Down