Simple Networking
This class is meant for simple network tasks.
Requirements
- Swift 5.9+ (Xcode 15+)
- iOS 13+, macOS 10.15+, tvOS, watchOS, Linux
Installation
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
Usage
Declare networking variable (preferred)
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.
Setup (optional)
networking.set(serverURL: "https://wesleydegroot.nl")
Set user-agent (optional)
networking.set(userAgent: "STRING")
Set authentication (optional)
networking.set(authorization: "STRING")
Set post type (optional)
networking.set(postType: .json) // .plain, .json, .graphQL
GET data Async/Await
Task {
let response = await networking.request(
path: "/",
method: .get
)
print(response.string)
}
POST data Async/Await
Task {
let response = await networking.request(
path: "/",
method: .post(
[
"postfield1": "poststring1",
"postfield2": "poststring2"
]
)
)
print(response.string)
}
Upload Files Async/Await
Upload a single file with multipart/form-data encoding:
Task {
// Create a file upload from data
let fileData = "Hello, World!".data(using: .utf8)!
let fileUpload = SimpleNetworking.FileUpload(
data: fileData,
name: "file",
filename: "document.txt",
mimeType: "text/plain"
)
// Create upload data with optional parameters
let uploadData = SimpleNetworking.MultipartUploadData(
file: fileUpload,
parameters: ["description": "My document"]
)
// Perform the upload
let response = await networking.upload(
path: "/upload",
uploadData: uploadData
)
print(response.string)
}
Upload large files from disk
FileBackedMultipartUploadData keeps the source file out of memory. Multipart framing is written to a temporary file in chunks, which URLSession uploads directly.
let file = try SimpleNetworking.FileBackedMultipartUploadData.File(
fileURL: URL(fileURLWithPath: "/path/to/video.mov"),
name: "video"
)
let upload = SimpleNetworking.FileBackedMultipartUploadData(
file: file,
parameters: ["title": "Launch video"]
)
let response = await networking.upload(
path: "/upload",
fileUploadData: upload
) { progress in
print(progress.fractionCompleted ?? 0)
}
Isolated clients
Use an immutable configuration to create clients that can be safely shared across concurrent tasks. Configuration updates are applied atomically, so an in-flight request always uses one consistent snapshot.
Clients use URLSession.shared by default. To use an ephemeral session with isolated in-memory cookie and cache storage, set sessionMode: .ephemeral. You can also inject a custom URLSession through the initializer's session parameter.
let networking = SimpleNetworking(configuration: .init(
serverURL: "https://api.example.com",
authorization: "access-token",
headers: [.init(name: "X-App-Version", value: "1.0")],
sessionMode: .ephemeral
))
Per-request configuration
Use Request when query items, headers, timeouts, or cache behavior are specific to one call. Request headers override the client defaults without mutating the client.
let request = SimpleNetworking.Request(
path: "/users",
method: .get,
query: ["page": "2"],
headers: [.init(name: "X-Request-ID", value: UUID().uuidString)],
timeout: 15,
cachePolicy: .reloadIgnoringLocalCacheData
)
let response = await networking.execute(request)
Custom methods and raw bodies are also supported:
let request = SimpleNetworking.Request(
path: "/documents",
method: "PROPFIND",
body: Data()
)
let response = await networking.execute(request)
Transfers with progress
Download directly to a file without retaining the downloaded content in memory:
let response = await networking.download(path: "/archive.zip", to: destinationURL) { progress in
print(progress.fractionCompleted ?? 0)
}
Resume data from an interrupted URLSession download can also be used:
let response = await networking.download(resuming: resumeData, to: destinationURL)
For Swift concurrency code, consume progress as an AsyncStream while awaiting the final response:
let transfer = networking.uploadTransfer(path: "/upload", uploadData: uploadData)
for await progress in transfer.progress {
print(progress.fractionCompleted ?? 0)
}
let response = await transfer.response.value
Upload a file from a file URL:
Task {
do {
// Create a file upload from a file URL
let fileURL = URL(fileURLWithPath: "/path/to/file.jpg")
let fileUpload = try SimpleNetworking.FileUpload(
fileURL: fileURL,
name: "photo"
// filename and mimeType are auto-detected from the file
)
let uploadData = SimpleNetworking.MultipartUploadData(
file: fileUpload
)
let response = await networking.upload(
path: "/upload",
uploadData: uploadData
)
print(response.string)
} catch {
print("Error uploading file: \(error)")
}
}
Upload multiple files:
Task {
let file1 = SimpleNetworking.FileUpload(
data: "Content 1".data(using: .utf8)!,
name: "file1",
filename: "document1.txt"
)
let file2 = SimpleNetworking.FileUpload(
data: "Content 2".data(using: .utf8)!,
name: "file2",
filename: "document2.txt"
)
let uploadData = SimpleNetworking.MultipartUploadData(
files: [file1, file2],
parameters: [
"user_id": "123",
"description": "Multiple files"
]
)
let response = await networking.upload(
path: "/upload",
uploadData: uploadData
)
print(response.string)
}
GET data (closure based)
networking.request(
path: "/",
method: .get
) { networkResponse in
print(networkResponse)
}
POST data (closure based)
networking.request(
path: "/",
method: .post(
[
"postfield1": "poststring1",
"postfield2": "poststring2"
]
)
) { networkResponse in
print(networkResponse)
}
Upload Files (closure based)
let fileData = "Hello, World!".data(using: .utf8)!
let fileUpload = SimpleNetworking.FileUpload(
data: fileData,
name: "file",
filename: "document.txt"
)
let uploadData = SimpleNetworking.MultipartUploadData(
file: fileUpload,
parameters: ["key": "value"]
)
networking.upload(
path: "/upload",
uploadData: uploadData
) { networkResponse in
print(networkResponse.string)
}
Property Wrappers
SimpleNetworking provides property wrappers for a declarative approach to defining HTTP requests.
Available Property Wrappers
@Get("/path") - GET requests
@Post("/path") - POST requests with optional data
@Put("/path") - PUT requests with optional data
@Delete("/path") - DELETE requests with optional data
@Upload("/path") - File upload requests
@SimpleNetworkingWrapper(.method, "/path") - Generic wrapper for any HTTP method
Standalone Usage
The property wrapper types can be used as standalone values without the @ syntax:
import SimpleNetworking
// Configure the shared instance (optional)
SimpleNetworking.shared.set(serverURL: "https://api.example.com")
// Create wrappers as standalone values
let getUsers = SimpleNetworking.Get("/users")
let createUser = SimpleNetworking.Post("/users")
let updateUser = SimpleNetworking.Put("/users/123")
let deleteUser = SimpleNetworking.Delete("/users/123")
// Execute requests
let usersResponse = await getUsers.execute()
print(usersResponse.string)
let createResponse = await createUser.execute(["name": "John Doe", "email": "john@example.com"])
print(createResponse.string)
let updateResponse = await updateUser.execute(["name": "Jane Doe"])
print(updateResponse.string)
let deleteResponse = await deleteUser.execute()
print(deleteResponse.statuscode)
File Upload (Standalone)
import SimpleNetworking
// Create an upload wrapper as a standalone value
let uploadFile = SimpleNetworking.Upload("/upload")
// Prepare the file
let fileData = "Hello, World!".data(using: .utf8)!
let fileUpload = SimpleNetworking.FileUpload(
data: fileData,
name: "file",
filename: "document.txt",
mimeType: "text/plain"
)
let uploadData = SimpleNetworking.MultipartUploadData(
file: fileUpload,
parameters: ["user_id": "123"]
)
// Execute the upload
let response = await uploadFile.execute(uploadData)
print(response.string)
Generic Wrapper (Standalone)
import SimpleNetworking
// Use the generic wrapper with any HTTP method
let getRequest = SimpleNetworking.SimpleNetworkingWrapper(.get, "/users")
let response = await getRequest.execute()
// With data for POST/PUT/DELETE
let postRequest = SimpleNetworking.SimpleNetworkingWrapper(.post(nil), "/users")
let postResponse = await postRequest.execute(["name": "John"])
Using as Property Wrappers with @ Syntax
Property wrappers are primarily designed to be used with the @ syntax on properties in your classes or structs:
import SimpleNetworking
class UserService {
@Get("/users")
var users: SimpleNetworking.NetworkResponse?
@Post("/users")
var createUser: SimpleNetworking.NetworkResponse?
@Put("/users/123")
var updateUser: SimpleNetworking.NetworkResponse?
@Delete("/users/123")
var deleteUser: SimpleNetworking.NetworkResponse?
func fetchUsers() async {
let response = await $users.execute()
print(response.string ?? "No data")
}
func addUser(name: String, email: String) async {
let userData = ["name": name, "email": email]
let response = await $createUser.execute(userData)
print(response.string ?? "No data")
}
}
Note: Property wrappers use SimpleNetworking.shared by default, so any configuration you set on the shared instance will apply to all property wrappers.
When using property wrappers in a struct (rather than a class), methods that call execute() on the projected value must be marked as mutating because the execution updates the wrapped value. For example:
struct UserService {
@Get("/users")
var users: SimpleNetworking.NetworkResponse?
mutating func fetchUsers() async {
let response = await $users.execute()
print(response.string ?? "No data")
}
}
networkResponse
With networkResponse you can get the following data:
- response: URLResponse? // URLResponse
- statuscode: Int // HTTP status code
- error: Error? // Error
- data: Data? // Received data
- string: String? // Received data as string
- dictionary: [String: Any]? // Received data as dictionary (only works if data is JSON)
- cookies: [HTTPCookie]? // Received cookies
- request: URLRequest? // Sent request
- headers: [HTTPHeader] // Received headers
- decoded -> T? // Decoded data
- cURL: String // as cURL command
- asHTTPRequest: String // as HTTP Request
Bonus 1: JSON Decoding
Codable, decoding strategy = useDefaultKeys
struct MyCodable: Codable {
let value1: String
let value2: String
}
// Decode the response
let data: MyCodable? = networkResponse.decoded()
Use decode when a decoding failure should be handled explicitly. It throws NetworkingError, which includes the failure kind, status, response headers, and a bounded body snippet.