-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added support for different post types, fixes #1
- Loading branch information
Showing
6 changed files
with
156 additions
and
37 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
89 changes: 89 additions & 0 deletions
89
Sources/SimpleNetworking/SimpleNetworking/createHTTPBody.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,89 @@ | ||
// | ||
// createHTTPBody.swift | ||
// Simple Networking | ||
// | ||
// Created by Wesley de Groot on 26/02/2024. | ||
// https://wesleydegroot.nl | ||
// | ||
// https://github.com/0xWDG/SimpleNetworking | ||
// MIT LICENCE | ||
// | ||
|
||
import Foundation | ||
|
||
extension SimpleNetworking { | ||
/// httpBody Method for internal use | ||
func createHTTPBody(with value: Any?) -> Data? { | ||
switch self.postType { | ||
case .json: | ||
if let contents = value as? [String: Codable] { | ||
return try? JSONSerialization.data(withJSONObject: contents) | ||
} | ||
|
||
if let contents = value as? Codable { | ||
return try? JSONEncoder().encode(contents) | ||
} | ||
|
||
case .plain: | ||
if let contents = value as? String { | ||
return contents.data(using: .utf8) | ||
} | ||
|
||
if let contents = value as? [String: Codable] { | ||
return contents.map { key, value in | ||
let escapedKey = "\(key)".addingPercentEncoding( | ||
withAllowedCharacters: urlQueryValueAllowed() | ||
) ?? "" | ||
let escapedValue = "\(value)".addingPercentEncoding( | ||
withAllowedCharacters: urlQueryValueAllowed() | ||
) ?? "" | ||
return escapedKey + "=" + escapedValue | ||
} | ||
.joined(separator: "&") | ||
.data(using: .utf8) | ||
} | ||
|
||
if let contents = value as? [String: Any] { | ||
return contents.map { key, value in | ||
let escapedKey = "\(key)".addingPercentEncoding( | ||
withAllowedCharacters: urlQueryValueAllowed() | ||
) ?? "" | ||
let escapedValue = "\(value)".addingPercentEncoding( | ||
withAllowedCharacters: urlQueryValueAllowed() | ||
) ?? "" | ||
return escapedKey + "=" + escapedValue | ||
} | ||
.joined(separator: "&") | ||
.data(using: .utf8) | ||
} | ||
|
||
case .graphQL: | ||
if let query = value as? String { | ||
return try? JSONSerialization.data(withJSONObject: [ | ||
"query": query | ||
]) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
/// Content-Type Header for internal use | ||
func getContentType() -> String { | ||
switch postType { | ||
case .plain: | ||
return "application/x-www-form-urlencoded" | ||
case .json, .graphQL: | ||
return "application/json" | ||
} | ||
} | ||
|
||
private func urlQueryValueAllowed() -> CharacterSet { | ||
let generalDelimitersToEncode = ":#[]@" | ||
let subDelimitersToEncode = "!$&'()*+,;=" | ||
|
||
var allowed: CharacterSet = .urlQueryAllowed | ||
allowed.remove(charactersIn: "\(generalDelimitersToEncode)\(subDelimitersToEncode)") | ||
return allowed | ||
} | ||
} |
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,18 @@ | ||
// | ||
// POSTType.swift | ||
// Simple Networking | ||
// | ||
// Created by Wesley de Groot on 26/02/2024. | ||
// https://wesleydegroot.nl | ||
// | ||
// https://github.com/0xWDG/SimpleNetworking | ||
// MIT LICENCE | ||
// | ||
|
||
import Foundation | ||
|
||
extension SimpleNetworking { | ||
public enum POSTType { | ||
case json, plain, graphQL | ||
} | ||
} |