-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: enhance plain upload Resolves: none. * refactor: relocate logger files to common files group Resolves: none. * fix: add all platforms to version check Resolves: none. * fix: handle size properly Resolves: none. * feat: add memory logger class Resolves: none. * feat: enhance plain and multipart upload + applying SRP Resolves: none. * feat: update documentation Resolves: none. * feat: update documentation Resolves: none. * feat: update docs Resolves: none.
- Loading branch information
1 parent
a1edbe0
commit 18d71f0
Showing
39 changed files
with
1,470 additions
and
512 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
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.
66 changes: 66 additions & 0 deletions
66
Source/Common/Request/Logger/Factory/CURLCommandFactory.swift
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,66 @@ | ||
// | ||
// CURLCommandFactory.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 01/10/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A factory class responsible for making cURL command representations | ||
/// for HTTP requests. | ||
/// | ||
/// This class provides methods to create a cURL command string from a given | ||
/// `URLRequest`, including handling different body options like plain data, | ||
/// file uploads, and multipart form data. | ||
final class CURLCommandFactory { | ||
|
||
/// Makes cURL command representation for a given request. | ||
/// | ||
/// - Parameters: | ||
/// - request: `URLRequest` used to make the cURL command representation. | ||
/// - bodyOption: `HTTPLogBodyOption` that determines how the body is represented in the cURL command. | ||
/// | ||
/// - Returns: cURL command representation for the given request. | ||
func make(for request: URLRequest, bodyOption: HTTPLogBodyOption) -> String { | ||
guard let url = request.url else { return "" } | ||
var baseCommand = #"curl "\#(url.absoluteString)""# | ||
|
||
if request.httpMethod == "HEAD" { | ||
baseCommand += " --head" | ||
} | ||
|
||
var command = [baseCommand] | ||
|
||
if let method = request.httpMethod, method != "GET" && method != "HEAD" { | ||
command.append("-X \(method)") | ||
} | ||
|
||
if let headers = request.allHTTPHeaderFields { | ||
for (key, value) in headers where key != "Cookie" { | ||
command.append("-H '\(key): \(value)'") | ||
} | ||
} | ||
|
||
switch bodyOption { | ||
case .plain: | ||
if let data = request.httpBody, | ||
let body = String(data: data, encoding: .utf8) { | ||
command.append("-d '\(body)'") | ||
} | ||
case .file(let file): | ||
command.append("--upload-file \(file.path ?? "{ Path to the file }")") | ||
case .formData(let formData): | ||
for parameter in formData.parameters { | ||
command.append("-F '\(parameter.key)=\(parameter.value)'") | ||
} | ||
|
||
for file in formData.files { | ||
command.append("-F '\(file.key)=@\(file.path ?? "{ Path to the file }");filename=\(file.name)'") | ||
} | ||
} | ||
|
||
return command.joined(separator: " \\\n\t") | ||
} | ||
|
||
} |
116 changes: 116 additions & 0 deletions
116
Source/Common/Request/Logger/Factory/HTTPLogBodyMessageFactory.swift
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,116 @@ | ||
// | ||
// HTTPLogBodyMessageFactory.swift | ||
// RxNetworkKit | ||
// | ||
// Created by Loay Ashraf on 01/10/2024. | ||
// | ||
|
||
import Foundation | ||
|
||
/// A factory class responsible for making console messages | ||
/// for the body of outgoing HTTP requests based on different logging options. | ||
/// | ||
/// This class provides methods to create log messages for plain data, | ||
/// file uploads, and multipart form data. It is utilized by the | ||
/// `HTTPLogger` to format and present the body of HTTP requests in a readable manner. | ||
final class HTTPLogBodyMessageFactory { | ||
|
||
/// Makes console body message for the specified body option. | ||
/// | ||
/// - Parameters: | ||
/// - bodyOption: `HTTPLogBodyOption` option for how the body is represented in the log message. | ||
/// | ||
/// - Returns: `String` of the outgoing request body message. | ||
func make(bodyOption: HTTPLogBodyOption) -> String { | ||
switch bodyOption { | ||
case .plain(let body): | ||
return make(body) | ||
case .file(let file): | ||
return make(file) | ||
case .formData(let formData): | ||
return make(formData) | ||
} | ||
} | ||
|
||
/// Makes console message for raw data body. | ||
/// | ||
/// - Parameter rawData: Optional `Data` object representing the body. | ||
/// | ||
/// - Returns: `String` representation of the raw data body. | ||
private func make(_ rawData: Data?) -> String { | ||
var logBodyMessage = "" | ||
if let rawData = rawData { | ||
if let jsonString = rawData.json { | ||
logBodyMessage += "\n\(jsonString)\n" | ||
} else { | ||
logBodyMessage += "\n\(String(data: rawData, encoding: .utf8) ?? "{ HTTP Body }")\n" | ||
} | ||
} | ||
return logBodyMessage | ||
} | ||
|
||
/// Makes console message for a file body. | ||
/// | ||
/// - Parameters: | ||
/// - file: `FileType` file details to be printed in place of body. | ||
/// | ||
/// - Returns: `String` of the outgoing request body message. | ||
private func make(_ file: FileType) -> String { | ||
var logBodyMessage: String = "" | ||
|
||
if let filePath = file.path { | ||
let fileName = file.name | ||
let fileType = file.mimeType.rawValue | ||
let fileSize = file.size.formattedSize | ||
logBodyMessage += "{ File From Disk }\n" | ||
logBodyMessage += "- Name: \(fileName)\n" | ||
logBodyMessage += "- Type: \(fileType)\n" | ||
logBodyMessage += "- Size: \(fileSize)\n" | ||
logBodyMessage += "- Path: \(filePath)" | ||
} else if file.data != nil { | ||
let fileName = file.name | ||
let fileType = file.mimeType.rawValue | ||
let fileSize = file.size.formattedSize | ||
logBodyMessage += "{ File From Memory }\n" | ||
logBodyMessage += "- Name: \(fileName)\n" | ||
logBodyMessage += "- Type: \(fileType)\n" | ||
logBodyMessage += "- Size: \(fileSize)" | ||
} | ||
|
||
return logBodyMessage | ||
} | ||
|
||
/// Makes console message for multipart form data body. | ||
/// | ||
/// - Parameters: | ||
/// - formData: `FormData` form data details to be printed in place of body. | ||
/// | ||
/// - Returns: `String` of the outgoing request body message. | ||
private func make(_ formData: FormData) -> String { | ||
var logBodyMessage: String = "\n" | ||
let boundary = formData.boundary | ||
let lineBreak: String = "\r\n" | ||
|
||
for parameter in formData.parameters { | ||
logBodyMessage += "--\(formData.boundary + lineBreak)" | ||
logBodyMessage += "Content-Disposition: form-data; name=\"\(parameter.key)\"\(lineBreak + lineBreak)" | ||
logBodyMessage += "\(parameter.value + lineBreak)" | ||
} | ||
|
||
for file in formData.files { | ||
logBodyMessage += "--\(boundary + lineBreak)" | ||
logBodyMessage += "Content-Disposition: form-data; name=\"\(file.key)\"; filename=\"\(file.name)\"\(lineBreak)" | ||
logBodyMessage += "Content-Type: \(file.mimeType.rawValue + lineBreak + lineBreak)" | ||
|
||
let fileBodyLogMessage = make(file) | ||
logBodyMessage += fileBodyLogMessage | ||
|
||
logBodyMessage += lineBreak | ||
} | ||
|
||
logBodyMessage += "--\(boundary)--\(lineBreak)" | ||
|
||
return logBodyMessage | ||
} | ||
|
||
} |
Oops, something went wrong.