-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore: wip add iOS code for raw request
- Loading branch information
Showing
10 changed files
with
1,025 additions
and
630 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
// Copyright (c) Ely Deckers. | ||
// | ||
// This source code is licensed under the MPL-2.0 license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
import Foundation | ||
|
||
open class BlobSender: NSObject { | ||
static func filterHeaders(unfilteredHeaders: NSDictionary) -> NSDictionary { | ||
Dictionary(uniqueKeysWithValues: unfilteredHeaders | ||
.map { key, value in (key as? String, value as? String) } | ||
.filter({ $0.1 != nil })) | ||
.mapValues { $0! } as NSDictionary | ||
} | ||
|
||
static func isValidTargetValue(_ value: String) -> Bool { | ||
return Constants.targetValues.contains(value) | ||
} | ||
|
||
static func buildRequestDataForFileSend( | ||
method: String, | ||
url: URL, | ||
absoluteFilePath: String, | ||
headers: NSDictionary) throws -> (URLRequest, Data) { | ||
var request = URLRequest(url: url) | ||
request.httpMethod = method | ||
|
||
for (key, value) in headers { | ||
if let headerKey = key as? String, let headerValue = value as? String { | ||
request.setValue( | ||
headerValue, | ||
forHTTPHeaderField: headerKey) | ||
} | ||
} | ||
|
||
let fileUrl = URL(string: absoluteFilePath)! | ||
|
||
let fileData = try Data(contentsOf: fileUrl) | ||
|
||
return (request, fileData) | ||
} | ||
|
||
// swiftlint:disable function_body_length | ||
static func sendBlobFromValidatedParameters(parameters: SendParameters) -> | ||
Result<NSDictionary, BlobCourierError> { | ||
let sessionConfig = URLSessionConfiguration.default | ||
|
||
let group = DispatchGroup() | ||
let groupId = UUID().uuidString | ||
|
||
let queue = DispatchQueue.global() | ||
|
||
var result: Result<NSDictionary, BlobCourierError> = .success([:]) | ||
|
||
print("Entering group (id=\(groupId))") | ||
group.enter() | ||
|
||
var cancelObserver: NSObjectProtocol? | ||
|
||
queue.async(group: group) { | ||
let successfulResult = { (theResult: NSDictionary) -> Void in | ||
result = .success(theResult) | ||
|
||
print("Leaving group (id=\(groupId),status=resolve)") | ||
group.leave() | ||
} | ||
|
||
let failedResult = { (error: BlobCourierError) -> Void in | ||
result = .failure(error) | ||
|
||
print("Leaving group (id=\(groupId),status=reject)") | ||
group.leave() | ||
} | ||
|
||
let senderDelegate = | ||
SenderDelegate( | ||
taskId: parameters.taskId, | ||
returnResponse: parameters.returnResponse, | ||
progressIntervalMilliseconds: parameters.progressIntervalMilliseconds, | ||
resolve: successfulResult, | ||
reject: failedResult) | ||
|
||
let session = URLSession(configuration: sessionConfig, delegate: senderDelegate, delegateQueue: nil) | ||
|
||
let headers = parameters.headers | ||
|
||
do { | ||
let (request, fileData) = | ||
try buildRequestDataForFileSend( | ||
method: parameters.method, | ||
url: parameters.url, | ||
absoluteFilePath: parameters.absoluteFilePath, | ||
headers: headers) | ||
|
||
session.uploadTask(with: request, from: fileData).resume() | ||
|
||
cancelObserver = CancelController.registerCancelObserver( | ||
session: session, taskId: parameters.taskId) | ||
} catch { | ||
failedResult(Errors.createUnexpectedError(error: error)) | ||
} | ||
} | ||
|
||
print("Waiting for group (id=\(groupId))") | ||
group.wait() | ||
print("Left group (id=\(groupId))") | ||
|
||
NotificationCenter.default.removeObserver(cancelObserver) | ||
|
||
return result | ||
} | ||
// swiftlint:enable function_body_length | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
// Copyright (c) Ely Deckers. | ||
// | ||
// This source code is licensed under the MPL-2.0 license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
import Foundation | ||
|
||
struct SendParameters { | ||
let absoluteFilePath: String | ||
let headers: NSDictionary | ||
let method: String | ||
let progressIntervalMilliseconds: Int | ||
let returnResponse: Bool | ||
let taskId: String | ||
let url: URL | ||
|
||
init( | ||
absoluteFilePath: String, | ||
headers: NSDictionary, | ||
method: String, | ||
progressIntervalMilliseconds: Int, | ||
returnResponse: Bool, | ||
taskId: String, | ||
url: URL) { | ||
self.absoluteFilePath = absoluteFilePath | ||
self.headers = headers | ||
self.method = method | ||
self.progressIntervalMilliseconds = progressIntervalMilliseconds | ||
self.returnResponse = returnResponse | ||
self.taskId = taskId | ||
self.url = url | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
// Copyright (c) Ely Deckers. | ||
// | ||
// This source code is licensed under the MPL-2.0 license found in the | ||
// LICENSE file in the root directory of this source tree. | ||
import Foundation | ||
|
||
open class SenderDelegate: NSObject, URLSessionDataDelegate, URLSessionTaskDelegate { | ||
typealias SuccessHandler = (NSDictionary) -> Void | ||
typealias FailureHandler = (BlobCourierError) -> Void | ||
|
||
private let resolve: SuccessHandler | ||
private let reject: FailureHandler | ||
|
||
private let taskId: String | ||
private let returnResponse: Bool | ||
|
||
private let eventEmitter: BlobCourierDelayedEventEmitter | ||
|
||
private var receivedData: Data = Data() | ||
|
||
init( | ||
taskId: String, | ||
returnResponse: Bool, | ||
progressIntervalMilliseconds: Int, | ||
resolve: @escaping SuccessHandler, | ||
reject: @escaping FailureHandler) { | ||
self.taskId = taskId | ||
self.returnResponse = returnResponse | ||
|
||
self.resolve = resolve | ||
self.reject = reject | ||
|
||
self.eventEmitter = | ||
BlobCourierDelayedEventEmitter( | ||
taskId: taskId, | ||
progressIntervalMilliseconds: progressIntervalMilliseconds) | ||
} | ||
|
||
public func urlSessionDidFinishEvents(forBackgroundURLSession session: URLSession) { | ||
} | ||
|
||
public func urlSession(_ session: URLSession, task: URLSessionTask, didCompleteWithError error: Error?) { | ||
guard let theError = error else { | ||
processCompletedSend(data: self.receivedData, response: task.response, error: error) | ||
return | ||
} | ||
|
||
processFailedSend(error: theError) | ||
} | ||
|
||
public func urlSession(_ session: URLSession, dataTask: URLSessionDataTask, didReceive data: Data) { | ||
self.receivedData.append(data) | ||
} | ||
|
||
public func urlSession( | ||
_ session: URLSession, | ||
task: URLSessionTask, | ||
didSendBodyData bytesSent: Int64, | ||
totalBytesSent: Int64, | ||
totalBytesExpectedToSend: Int64) { | ||
self.eventEmitter.notifyBridgeOfProgress( | ||
totalBytesWritten: totalBytesSent, | ||
totalBytesExpectedToWrite: totalBytesExpectedToSend) | ||
} | ||
|
||
func processFailedSend(error: Error) { | ||
if (error as NSError).code == NSURLErrorCancelled { | ||
self.reject(BlobCourierError(code: Errors.errorCanceledException, message: "Request was cancelled", error: error)) | ||
|
||
return | ||
} | ||
|
||
self.reject(Errors.createUnexpectedError(error: error)) | ||
} | ||
|
||
func processCompletedSend(data: Data, response: URLResponse?, error: Error?) { | ||
if let error = error { | ||
print( | ||
"Error while sending a file. Error description: \(error.localizedDescription)" | ||
) | ||
reject(Errors.createUnexpectedError(error: error)) | ||
return | ||
} | ||
|
||
if let statusCode = (response as? HTTPURLResponse)?.statusCode { | ||
let maybeRawResponse = returnResponse ? String(data: data, encoding: String.Encoding.utf8) : nil | ||
let rawResponse = maybeRawResponse ?? "" | ||
|
||
let result: NSDictionary = [ | ||
"response": [ | ||
"code": statusCode, | ||
"data": rawResponse, | ||
"headers": [] | ||
] | ||
] | ||
|
||
resolve(result) | ||
return | ||
} | ||
|
||
let noStatusCodeError = | ||
NSError( | ||
domain: Constants.libraryDomain, | ||
code: -1, | ||
userInfo: [NSLocalizedDescriptionKey: "Received no status code"]) | ||
|
||
reject(Errors.createUnexpectedError(error: noStatusCodeError)) | ||
} | ||
} |
Oops, something went wrong.