This class is meant for simple network tasks.
- Swift 5.9+ (Xcode 15+)
- iOS 13+, macOS 10.15+, tvOS, watchOS, Linux
Install using Swift Package Manager
dependencies: [
.package(url: "https://github.com/0xWDG/SimpleNetworking.git", .branch("main")),
],
targets: [
.target(name: "MyTarget", dependencies: [
.product(name: "SimpleNetworking", package: "SimpleNetworking"),
]),
]
And import it:
import SimpleNetworking
import SimpleNetworking
let networking = SimpleNetworking.shared
We use networking
as the variable name, but you can use any name you like.
Please note in the examples below we use networking
as the variable name.
If you use a different name, please replace networking
with your variable name.
Or use SimpleNetworking.shared
instead of networking
.
networking.set(serverURL: "https://wesleydegroot.nl")
networking.set(userAgent: "STRING")
networking.set(authorization: "STRING")
networking.set(postType: .json) // .plain, .json, .graphQL
Task {
let response = await networking.request(
path: "/",
method: .get
)
print(response.string)
}
Task {
let response = await networking.request(
path: "/",
method: .post(
[
"postfield1": "poststring1",
"postfield2": "poststring2"
]
)
)
print(response.string)
}
networking.request(
path: "/",
method: .get
) { networkResponse in
print(networkResponse)
}
networking.request(
path: "/",
method: .post(
[
"postfield1": "poststring1",
"postfield2": "poststring2"
]
)
) { networkResponse in
print(networkResponse)
}
struct MyCodable: Codable {
let value1: String
let value2: String
}
// Decode the response
let data: MyCodable? = networkResponse.decoded()
struct MyCodable: Codable {
let snakeCase: String
let caseSnake: String
}
// Decode the response
let data: MyCodable? = networkResponse.decoded(.convertFromSnakeCase)
import SimpleNetworking
networking.connect(to: "https://api.github.com/users/0xWDG") { data in
print(data)
}
let cookie = HTTPCookie.init(properties: [
.name: "my cookie",
.value: "my value",
.domain: "wesleydegroot.nl"
.path: "/"
])
networking.add(cookie: cookie)
SimpleNetworking can be mocked (since version 1.0.3), so you can test your code without actually making a network request.
networking.set(mockData: [
"https://wesleydegroot.nl": .init(
data: "OVERRIDE", // Can be Data or String
response: .init( // NSURLResponse, Can be nil
url: .init(string: "https://wesleydegroot.nl")!,
mimeType: "text/html",
expectedContentLength: 8,
textEncodingName: "utf-8"
),
statusCode: 200, // Int: If omitted, 200 is used
error: nil
),
"/only/an/path": .init(
data: "OVERRIDE", // Can be Data or String
response: .init( // NSURLResponse, Can be nil
url: .init(string: "https://wesleydegroot.nl/only/an/path")!,
mimeType: "text/html",
expectedContentLength: 8,
textEncodingName: "utf-8"
),
statusCode: 200, // Int: If omitted, 200 is used
error: nil
)
])
/// Debug: NSURLRequest
networking.debug.requestURL = false
/// Debug: sent HTTP Headers
networking.debug.requestHeaders = false
/// Debug: sent Cookies
networking.debug.requestCookies = false
/// Debug: sent Body
networking.debug.requestBody = false
/// Debug: received HTTP Headers
networking.debug.responseHeaders = false
/// Debug: received Body
networking.debug.responseBody = false
/// Debug: received JSON (if any)
networking.debug.responseJSON = false
We can get in touch via Twitter/X, Discord, Mastodon, Threads, Bluesky.
Alternatively you can visit my Website.