Skip to content

Commit 53ea6e2

Browse files
committed
Minimal changes for SwiftyRequest v3
1 parent 17f624c commit 53ea6e2

3 files changed

Lines changed: 33 additions & 33 deletions

File tree

Sources/KituraKit/Client.swift

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -96,11 +96,7 @@ public class KituraKit {
9696
/// - clientCertificate: Pass in `ClientCertificate` with the certificate name and path to use client certificates for 2-way SSL
9797
public init(baseURL: URL, containsSelfSignedCert: Bool = false, clientCertificate: ClientCertificate? = nil) {
9898
self.baseURL = baseURL
99-
if let clientCertificate = clientCertificate {
100-
self.clientCertificate = SwiftyRequest.ClientCertificate(name: clientCertificate.name, path: clientCertificate.path)
101-
} else {
102-
self.clientCertificate = nil
103-
}
99+
self.clientCertificate = clientCertificate
104100
self.containsSelfSignedCert = containsSelfSignedCert
105101
}
106102

@@ -226,11 +222,11 @@ public class KituraKit {
226222
request.headerParameters = credentials?.getHeaders() ?? [:]
227223
request.acceptType = mediaType
228224
request.contentType = mediaType
229-
request.responseData { response in
230-
switch response.result {
231-
case .success(let data):
232-
guard let item: O = try? self.decoder.decode(O.self, from: data),
233-
let locationHeader = response.response?.allHeaderFields["Location"] as? String,
225+
request.responseData { result in
226+
switch result {
227+
case .success(let response):
228+
guard let item: O = try? self.decoder.decode(O.self, from: response.body),
229+
let locationHeader = response.headers["Location"].first,
234230
let id = try? Id.init(value: locationHeader)
235231
else {
236232
respondWith(nil, nil, RequestError.clientDeserializationError)
@@ -239,7 +235,7 @@ public class KituraKit {
239235
respondWith(id, item, nil)
240236
case .failure(let error):
241237
Log.error("POST failure: \(error)")
242-
respondWith(nil, nil, constructRequestError(from: error, data: response.data))
238+
respondWith(nil, nil, constructRequestError(from: error, data: error.responseData))
243239
}
244240
}
245241
}
@@ -426,25 +422,25 @@ extension RestRequest {
426422

427423
/// Helper method to handle the given request for CodableArrayResultClosures and CodableResultClosures
428424
fileprivate func handle<O: Codable>(decoder: BodyDecoder, _ respondWith: @escaping (O?, RequestError?) -> (), queryItems: [URLQueryItem]? = nil) {
429-
self.responseData(queryItems: queryItems) { response in
430-
switch response.result {
431-
case .success(let data) :
432-
self.defaultCodableHandler(decoder: decoder, data, respondWith: respondWith)
425+
self.responseData(queryItems: queryItems) { result in
426+
switch result {
427+
case .success(let response):
428+
self.defaultCodableHandler(decoder: decoder, response.body, respondWith: respondWith)
433429
case .failure(let error):
434-
self.defaultErrorHandler(error, data: response.data, respondWith: respondWith)
430+
self.defaultErrorHandler(error, data: error.responseData, respondWith: respondWith)
435431
}
436432
}
437433
}
438434

439435
/// Helper method to handle the given delete request
440436
fileprivate func handleDelete(_ respondWith: @escaping (RequestError?) -> (), queryItems: [URLQueryItem]? = nil) {
441-
self.responseData(queryItems: queryItems) { response in
442-
switch response.result {
437+
self.responseVoid(queryItems: queryItems) { result in
438+
switch result {
443439
case .success:
444440
respondWith(nil)
445441
case .failure(let error):
446442
Log.error("DELETE failure: \(error)")
447-
respondWith(constructRequestError(from: error, data: response.data))
443+
respondWith(constructRequestError(from: error, data: error.responseData))
448444
}
449445
}
450446
}
Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright IBM Corporation 2018
2+
* Copyright IBM Corporation 2019
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -14,16 +14,7 @@
1414
* limitations under the License.
1515
*/
1616

17-
/// Struct to store client certificate name and path
18-
public struct ClientCertificate {
19-
/// The name for the client certificate
20-
public let name: String
21-
/// The path to the client certificate
22-
public let path: String
17+
import SwiftyRequest
2318

24-
/// Initialize a `ClientCertificate` instance
25-
public init(name: String, path: String) {
26-
self.name = name
27-
self.path = path
28-
}
29-
}
19+
/// An alias of `SwiftyRequest.ClientCertificate`, so that you do not have to import SwiftyRequest directly.
20+
public typealias ClientCertificate = SwiftyRequest.ClientCertificate

Sources/KituraKit/RequestErrorExtension.swift

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,9 @@ extension RequestError {
5353

5454
/// An HTTP 608 invalid substitution error
5555
public static var clientInvalidSubstitution = RequestError(clientErrorCode: 608, clientErrorDescription: "An invalid substitution error occurred. Please ensure that the data being substituted is correct.")
56+
57+
/// An HTTP 609 encoding error
58+
public static var clientDecodingError = RequestError(clientErrorCode: 609, clientErrorDescription: "A decoding error occurred. Please ensure that the types and format of the data being received is correct.")
5659
}
5760

5861
/// An extension to Kitura RequestErrors with additional error codes specifically for the client.
@@ -62,13 +65,23 @@ extension RequestError {
6265
/// - Parameter restError: The custom error type for the client.
6366
public init(restError: RestError) {
6467
switch restError {
65-
case .erroredResponseStatus(let code): self = RequestError(httpCode: code)
6668
case .noData: self = .clientNoData
6769
case .serializationError: self = .clientSerializationError
6870
case .encodingError: self = .clientEncodingError
71+
case .decodingError: self = .clientDecodingError
6972
case .fileManagerError: self = .clientFileManagerError
7073
case .invalidFile: self = .clientInvalidFile
7174
case .invalidSubstitution: self = .clientInvalidSubstitution
75+
case .invalidURL: fallthrough // Will not occur: Client can only be initialized with a valid URL
76+
case .downloadError: fallthrough // Will not occur: API is not used by KituraKit
77+
case .errorStatusCode: fallthrough
78+
default:
79+
// All other cases:
80+
if let response = restError.response {
81+
self = RequestError(httpCode: Int(response.status.code))
82+
} else {
83+
self = RequestError(rawValue: 0, reason: "Error: No response was received by the client")
84+
}
7285
}
7386
}
7487
}

0 commit comments

Comments
 (0)