Skip to content

Commit

Permalink
Easy way to access cookies in RestResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
Rudrani committed Jul 9, 2019
1 parent e3b3d1e commit d589cca
Show file tree
Hide file tree
Showing 3 changed files with 154 additions and 29 deletions.
98 changes: 70 additions & 28 deletions Sources/SwiftyRequest/RestRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,23 @@ public class RestRequest: NSObject {
task.resume()
}
}
private func getCookies(from response: HTTPURLResponse?) -> [HTTPCookie]? {
guard let headers = response?.allHeaderFields else{
return nil
}
var headerFields = [String : String]()
for (key, value) in headers{
guard let key = key as? String, let value = value as? String else{
continue }
headerFields[key] = value
}
guard headerFields["Set-Cookie"] != nil else {
return nil
}
let url = response?.url
let dummyUrl = URL(string:"http://example.com")!
return HTTPCookie.cookies(withResponseHeaderFields: headerFields, for: url ?? dummyUrl)
}

/// Request response method with the expected result of a `Data` object.
///
Expand All @@ -334,7 +351,7 @@ public class RestRequest: NSObject {

if let error = performSubstitutions(params: templateParams) {
let result = Result<Data>.failure(error)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil)
completionHandler(dataResponse)
return
}
Expand All @@ -349,19 +366,22 @@ public class RestRequest: NSObject {

if let error = error {
let result = Result<Data>.failure(error)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

guard let data = data else {
let result = Result<Data>.failure(RestError.noData)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}
let result = Result.success(data)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
}
}
Expand All @@ -383,7 +403,7 @@ public class RestRequest: NSObject {

if let error = performSubstitutions(params: templateParams) {
let result = Result<T>.failure(error)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil)
completionHandler(dataResponse)
return
}
Expand All @@ -398,23 +418,26 @@ public class RestRequest: NSObject {

if let error = error {
let result = Result<T>.failure(error)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

if let responseToError = responseToError,
let error = responseToError(response, data) {
let result = Result<T>.failure(error)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

// ensure data is not nil
guard let data = data else {
let result = Result<T>.failure(RestError.noData)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}
Expand Down Expand Up @@ -443,7 +466,8 @@ public class RestRequest: NSObject {
}

// execute callback
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
}
}
Expand All @@ -463,7 +487,7 @@ public class RestRequest: NSObject {

if let error = performSubstitutions(params: templateParams) {
let result = Result<T>.failure(error)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil)
completionHandler(dataResponse)
return
}
Expand All @@ -478,15 +502,17 @@ public class RestRequest: NSObject {

if let error = error ?? responseToError?(response, data) {
let result = Result<T>.failure(error)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

// ensure data is not nil
guard let data = data else {
let result = Result<T>.failure(RestError.noData)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}
Expand All @@ -501,7 +527,8 @@ public class RestRequest: NSObject {
}

// execute callback
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
}
}
Expand All @@ -523,7 +550,7 @@ public class RestRequest: NSObject {

if let error = performSubstitutions(params: templateParams) {
let result = Result<[T]>.failure(error)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil)
completionHandler(dataResponse)
return
}
Expand All @@ -538,23 +565,26 @@ public class RestRequest: NSObject {

if let error = error {
let result = Result<[T]>.failure(error)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

if let responseToError = responseToError,
let error = responseToError(response, data) {
let result = Result<[T]>.failure(error)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

// ensure data is not nil
guard let data = data else {
let result = Result<[T]>.failure(RestError.noData)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}
Expand Down Expand Up @@ -584,7 +614,8 @@ public class RestRequest: NSObject {
}

// execute callback
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
}
}
Expand All @@ -604,7 +635,7 @@ public class RestRequest: NSObject {

if let error = performSubstitutions(params: templateParams) {
let result = Result<String>.failure(error)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil)
completionHandler(dataResponse)
return
}
Expand All @@ -619,23 +650,26 @@ public class RestRequest: NSObject {

if let error = error {
let result = Result<String>.failure(error)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

if let responseToError = responseToError,
let error = responseToError(response, data) {
let result = Result<String>.failure(error)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

// ensure data is not nil
guard let data = data else {
let result = Result<String>.failure(RestError.noData)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}
Expand All @@ -646,14 +680,16 @@ public class RestRequest: NSObject {
// parse data as a string
guard let string = String(data: data, encoding: encoding) else {
let result = Result<String>.failure(RestError.serializationError)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: nil, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

// execute callback
let result = Result.success(string)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
}
}
Expand All @@ -673,7 +709,7 @@ public class RestRequest: NSObject {

if let error = performSubstitutions(params: templateParams) {
let result = Result<Void>.failure(error)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result)
let dataResponse = RestResponse(request: request, response: nil, data: nil, result: result, cookies: nil)
completionHandler(dataResponse)
return
}
Expand All @@ -688,21 +724,24 @@ public class RestRequest: NSObject {

if let error = error {
let result = Result<Void>.failure(error)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

if let responseToError = responseToError, let error = responseToError(response, data) {
let result = Result<Void>.failure(error)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
return
}

// execute callback
let result = Result<Void>.success(())
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result)
let cookies = self.getCookies(from: response)
let dataResponse = RestResponse(request: self.request, response: response, data: data, result: result, cookies: cookies)
completionHandler(dataResponse)
}
}
Expand Down Expand Up @@ -846,6 +885,9 @@ public struct RestResponse<T> {

/// The Reponse Result.
public let result: Result<T>

// The cookies
public let cookies: [HTTPCookie]?
}

/// Enum to differentiate a success or failure.
Expand Down
20 changes: 20 additions & 0 deletions TestServer/Sources/TestServer/main.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,23 @@
import Kitura
import HeliumLogger
import FileKit
import Foundation

func cookieHandler(request: RouterRequest, response: RouterResponse, next: @escaping () -> Void) throws {
let number = request.parameters["number"].map { Int($0) ?? 0 } ?? 0
for no in 0..<number {
var cookieProps: [HTTPCookiePropertyKey: Any]
cookieProps = [
HTTPCookiePropertyKey.domain: "localhost",
HTTPCookiePropertyKey.path: "/",
HTTPCookiePropertyKey.name: "name\(no)",
HTTPCookiePropertyKey.value: "value\(no)",
]
let cookie = HTTPCookie(properties: cookieProps)
response.cookies["name\(no)"] = cookie
}
try response.status(.OK).end()
}

// Enable logging
HeliumLogger.use(.info)
Expand Down Expand Up @@ -40,6 +57,7 @@ router.post("/echoJSON") {
try response.status(.badRequest).end()
}
}
router.get("/cookies/:number", handler: cookieHandler)

// Create a router that will be used for SSL requests
let sslRouter = Router()
Expand Down Expand Up @@ -82,6 +100,8 @@ sslRouter.get("/ssl/friends") {
try response.send(json: friends).end()
}

sslRouter.get("/ssl/cookies/:number", handler: cookieHandler)

// Add an HTTP server and connect it to the router
Kitura.addHTTPServer(onPort: 8080, with: router)
Kitura.addHTTPServer(onPort: 8443, with: sslRouter, withSSL: sslConfig)
Expand Down
Loading

0 comments on commit d589cca

Please sign in to comment.