forked from loafle/openapi-generator-original
* [swift5] Add useSPMFileStructure Signed-off-by: Ayman Bagabas <ayman.bagabas@gmail.com> * [swift5] Add swiftPackagePath Prioritize swiftPackagePath over useSPMFileStructure * [swift5] Add cli options and update docs * [swift5] Fix tests * [swift5] Update XcodeGen source path * [swift5] Update samples and docs Add useSPMFileStructure to URLSession library
54 lines
1.6 KiB
Swift
54 lines
1.6 KiB
Swift
//
|
|
// JSONDataEncoding.swift
|
|
//
|
|
// Generated by openapi-generator
|
|
// https://openapi-generator.tech
|
|
//
|
|
|
|
import Foundation
|
|
|
|
public struct JSONDataEncoding {
|
|
|
|
// MARK: Properties
|
|
|
|
private static let jsonDataKey = "jsonData"
|
|
|
|
// MARK: Encoding
|
|
|
|
/// Creates a URL request by encoding parameters and applying them onto an existing request.
|
|
///
|
|
/// - parameter urlRequest: The request to have parameters applied.
|
|
/// - parameter parameters: The parameters to apply. This should have a single key/value
|
|
/// pair with "jsonData" as the key and a Data object as the value.
|
|
///
|
|
/// - throws: An `Error` if the encoding process encounters an error.
|
|
///
|
|
/// - returns: The encoded request.
|
|
public func encode(_ urlRequest: URLRequest, with parameters: [String: Any]?) -> URLRequest {
|
|
var urlRequest = urlRequest
|
|
|
|
guard let jsonData = parameters?[JSONDataEncoding.jsonDataKey] as? Data, !jsonData.isEmpty else {
|
|
return urlRequest
|
|
}
|
|
|
|
if urlRequest.value(forHTTPHeaderField: "Content-Type") == nil {
|
|
urlRequest.setValue("application/json", forHTTPHeaderField: "Content-Type")
|
|
}
|
|
|
|
urlRequest.httpBody = jsonData
|
|
|
|
return urlRequest
|
|
}
|
|
|
|
public static func encodingParameters(jsonData: Data?) -> [String: Any]? {
|
|
var returnedParams: [String: Any]?
|
|
if let jsonData = jsonData, !jsonData.isEmpty {
|
|
var params: [String: Any] = [:]
|
|
params[jsonDataKey] = jsonData
|
|
returnedParams = params
|
|
}
|
|
return returnedParams
|
|
}
|
|
|
|
}
|