Skip to content

Commit

Permalink
Merged in feature/NSMSIO-119 (pull request #34)
Browse files Browse the repository at this point in the history
NSMSIO-119 In 'RequestEntity' the array of 'HTTPCookie' replaced by 'HTTPCookieStorage' (pt.2)

Approved-by: Natalia Mamunina
  • Loading branch information
roxiemobile-forks committed Oct 9, 2022
2 parents c2461f1 + 901a942 commit 4c4e5cc
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,11 @@ open class BasicRequestEntity<T>: RequestEntity<T> {

// MARK: - Construction

init(builder: BasicRequestEntityBuilder<T>) {
internal init(builder: BasicRequestEntityBuilder<T>) {
super.init(
url: builder.url,
headers: builder.headers,
cookies: builder.cookies,
cookieStorage: builder.cookieStorage ?? InMemoryCookieStorage(),
body: builder.body
)
}
Expand All @@ -40,14 +40,14 @@ open class BasicRequestEntityBuilder<T> {
public init(entity: RequestEntity<T>) {
self.url = entity.url
self.headers = entity.headers
self.cookies = entity.cookies
self.cookieStorage = entity.cookieStorage
self.body = entity.body
}

public init<Ti>(entity: RequestEntity<Ti>, body: T?) {
self.url = entity.url
self.headers = entity.headers
self.cookies = entity.cookies
self.cookieStorage = entity.cookieStorage
self.body = body
}

Expand All @@ -57,7 +57,7 @@ open class BasicRequestEntityBuilder<T> {

fileprivate(set) var headers: HttpHeaders?

fileprivate(set) var cookies: [HttpCookieProtocol]?
fileprivate(set) var cookieStorage: HTTPCookieStorage?

fileprivate(set) var body: T?

Expand All @@ -73,8 +73,15 @@ open class BasicRequestEntityBuilder<T> {
return self
}

@discardableResult
open func cookieStorage(_ cookieStorage: HTTPCookieStorage) -> Self {
self.cookieStorage = cookieStorage
return self
}

@available(*, deprecated, message: "use cookieStorage(_:) instead")
@discardableResult open func cookies(_ cookies: [HttpCookieProtocol]?) -> Self {
self.cookies = cookies
self.cookieStorage = InMemoryCookieStorage(cookies: cookies?.compactMap { HTTPCookie($0) } ?? [])
return self
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ open class RequestEntity<T> {

// MARK: - Construction

init(url: URL?, headers: HttpHeaders?, cookies: [HttpCookieProtocol]?, body: T?) {
internal init(url: URL?, headers: HttpHeaders?, cookieStorage: HTTPCookieStorage, body: T?) {
self.url = url
self.headers = headers
self.cookies = cookies
self.cookieStorage = cookieStorage
self.body = body
}

Expand All @@ -31,7 +31,7 @@ open class RequestEntity<T> {

public let headers: HttpHeaders?

public let cookies: [HttpCookieProtocol]?
public let cookieStorage: HTTPCookieStorage

public let body: T?
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ open class BasicResponseEntity<T>: ResponseEntity<T> {
super.init(
url: builder.url,
headers: builder.headers,
cookies: builder.cookies,
cookieStorage: builder.cookieStorage ?? InMemoryCookieStorage(),
body: builder.body,
status: builder.status,
mediaType: builder.mediaType
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ open class ResponseEntity<T>: RequestEntity<T> {

// MARK: - Construction

init(
internal init(
url: URL?,
headers: HttpHeaders?,
cookies: [HttpCookieProtocol]?,
cookieStorage: HTTPCookieStorage,
body: T?,
status: HttpStatus?,
mediaType: MediaType?
Expand All @@ -31,7 +31,7 @@ open class ResponseEntity<T>: RequestEntity<T> {
self.mediaType = mediaType

// Parent processing
super.init(url: url, headers: headers, cookies: cookies, body: body)
super.init(url: url, headers: headers, cookieStorage: cookieStorage, body: body)
}

// MARK: - Properties
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,26 +59,26 @@ public final class RestApiClient {
fileprivate func execute(_ method: HTTPMethod, entity: RequestEntity<HttpBody>) -> HttpResult {

let result: HttpResult
let cookieStore = BasicHttpCookieStore(cookies: entity.cookies)
let cookieStorage = entity.cookieStorage

// Create HTTP request
let urlRequest = createRequest(method, entity: entity)

do {
// Execute HTTP request
var response = try executeWithAllInterceptors(urlRequest, cookieStore: cookieStore)
var response = try executeWithAllInterceptors(urlRequest, cookieStorage: cookieStorage)

// Follow up redirects
while let redirectRequest = response.redirectRequest {
response = try executeWithNetworkInterceptors(redirectRequest, cookieStore: cookieStore)
response = try executeWithNetworkInterceptors(redirectRequest, cookieStorage: cookieStorage)
}

// Handle response
result = HttpResult.success(handleHttpResponse(response, cookieStore))
result = HttpResult.success(handleHttpResponse(response, cookieStorage))
}
catch let error as HttpResponseError {
// Handle interrupted HTTP requests
result = HttpResult.success(handleHttpResponse(error.httpResponse, cookieStore))
result = HttpResult.success(handleHttpResponse(error.httpResponse, cookieStorage))
}
catch let error {
// Handle any other errors
Expand All @@ -91,35 +91,35 @@ public final class RestApiClient {

fileprivate func executeWithAllInterceptors(
_ urlRequest: URLRequest,
cookieStore: HttpCookieStore
cookieStorage: HTTPCookieStorage
) throws -> HttpResponse {

var interceptors: [Interceptor] = []

interceptors.append(contentsOf: _httpClientConfig.interceptors ?? [])
interceptors.append(contentsOf: _httpClientConfig.networkInterceptors ?? [])
interceptors.append(createCallServerInterceptor(cookieStore))
interceptors.append(createCallServerInterceptor(cookieStorage))

return try execute(urlRequest, withInterceptors: interceptors, cookieStore: cookieStore)
return try execute(urlRequest, withInterceptors: interceptors, cookieStorage: cookieStorage)
}

fileprivate func executeWithNetworkInterceptors(
_ urlRequest: URLRequest,
cookieStore: HttpCookieStore
cookieStorage: HTTPCookieStorage
) throws -> HttpResponse {

var interceptors: [Interceptor] = []

interceptors.append(contentsOf: _httpClientConfig.networkInterceptors ?? [])
interceptors.append(createCallServerInterceptor(cookieStore))
interceptors.append(createCallServerInterceptor(cookieStorage))

return try execute(urlRequest, withInterceptors: interceptors, cookieStore: cookieStore)
return try execute(urlRequest, withInterceptors: interceptors, cookieStorage: cookieStorage)
}

fileprivate func execute(
_ urlRequest: URLRequest,
withInterceptors interceptors: [Interceptor],
cookieStore: HttpCookieStore
cookieStorage: HTTPCookieStorage
) throws -> HttpResponse {

// Create first chain element
Expand All @@ -129,7 +129,7 @@ public final class RestApiClient {
return try chain.proceed(urlRequest)
}

fileprivate func createCallServerInterceptor(_ cookieStore: HttpCookieStore) -> CallServerInterceptor {
fileprivate func createCallServerInterceptor(_ cookieStorage: HTTPCookieStorage) -> CallServerInterceptor {

let requestTimeoutConfig = _httpClientConfig.requestTimeoutConfig
?? DefaultRequestTimeoutConfig.shared
Expand All @@ -138,7 +138,7 @@ public final class RestApiClient {
.connectionTimeout(requestTimeoutConfig.connectionTimeout)
.readTimeout(requestTimeoutConfig.readTimeout)
.tlsConfig(_httpClientConfig.tlsConfig)
.cookieStore(cookieStore)
.cookieStorage(cookieStorage)
.build()
}

Expand All @@ -160,7 +160,7 @@ public final class RestApiClient {

fileprivate func handleHttpResponse(
_ httpResponse: HttpResponse,
_ cookieStore: HttpCookieStore
_ cookieStorage: HTTPCookieStorage
) -> ResponseEntity<Data> {

var statusCode = HttpStatus.InternalServerError
Expand All @@ -186,7 +186,7 @@ public final class RestApiClient {
.status(statusCode)
// @see https://tools.ietf.org/html/rfc7231#section-3.1.1.5
.mediaType(MediaType.ApplicationOctetStream)
.cookies(cookieStore.cookies)
.cookieStorage(cookieStorage)

if let responseBody = httpResponse.body {
entityBuilder.body(responseBody)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ class CallServerInterceptor: Interceptor {
var httpError: Error?

// Create session configuration
let config = URLSessionConfiguration.ephemeral
let config = URLSessionConfiguration.ephemeral.copy() as! URLSessionConfiguration

config.httpAdditionalHeaders = [:]
config.timeoutIntervalForResource = _options.connectionTimeout
config.timeoutIntervalForRequest = _options.readTimeout
config.httpCookieStorage = (_options.cookieStore as? HTTPCookieStorage)
config.httpCookieStorage = _options.cookieStorage

// Send POST request using NSURLSession
// @link https://stackoverflow.com/a/19101084
Expand Down Expand Up @@ -130,7 +130,7 @@ class CallServerInterceptor: Interceptor {
var connectionTimeout = DefaultRequestTimeoutConfig.shared.connectionTimeout
var readTimeout = DefaultRequestTimeoutConfig.shared.readTimeout
var tlsConfig: TlsConfig?
var cookieStore: HttpCookieStore?
var cookieStorage: HTTPCookieStorage?
}

// MARK: - Variables
Expand Down Expand Up @@ -161,8 +161,8 @@ class CallServerInterceptorBuilder {
return self
}

func cookieStore(_ cookieStore: HttpCookieStore?) -> Self {
self.options.cookieStore = cookieStore
func cookieStorage(_ cookieStorage: HTTPCookieStorage) -> Self {
self.options.cookieStorage = cookieStorage
return self
}

Expand Down

0 comments on commit 4c4e5cc

Please sign in to comment.