SimpleNetworking
Today i write about my Swift package SimpleNetworking.
It's a simple networking library for Swift it is a wrapper around URLSession and provides a variety of functions to make easy network requests.
You can find the source code and documentation at GitHub https://github.com/0xWDG/SimpleNetworking.
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"),
]),
]
Async / Await Examples
import SimpleNetworking
class myApiService {
// Create a shared instance of SimpleNetworking
let networking = SimpleNetworking.shared
init() {
// Set base URL (optional)
networking.set(serverURL: "https://wesleydegroot.nl")
// Set a custom user agent (optional)
networking.set(userAgent: "My App")
// Set a custom authorization header (optional)
networking.set(authorization: "MyToken")
// Set a custom post type (optional)
networking.set(postType: .json) // .plain, .json, .graphQL
}
/// Get a user (GET request)
func getUser() async {
let response = await networking.request(
path: "/api/getUser",
method: .get
)
print(response.string)
}
/// Update a user (POST request)
func updateUser() async {
let response = await networking.request(
path: "/api/updateUser",
method: .post([
"firstname": "Wesley",
"lastname": "de Groot"
])
)
print(response.string)
}
/// Get all users (GET request)
func getUsers() async {
let response = await networking.request(
path: "/api/getUsers",
method: .get
)
struct userProfile: Codable {
let firstname: String
let lastname: String
}
// Decode the response
let userProfiles: [userProfile]? = networkResponse.decoded()
print(userProfiles)
}
}
Closure Examples
Closure Examples
import SimpleNetworking
class myApiService {
// Create a shared instance of SimpleNetworking
let networking = SimpleNetworking.shared
init() {
// Set base URL (optional)
networking.set(serverURL: "https://wesleydegroot.nl")
// Set a custom user agent (optional)
networking.set(userAgent: "My App")
// Set a custom authorization header (optional)
networking.set(authorization: "MyToken")
// Set a custom post type (optional)
networking.set(postType: .json) // .plain, .json, .graphQL
}
/// Get a user (GET request)
func getUser() {
networking.request(
path: "/api/getUser",
method: .get
) { response in
print(response.string)
}
}
/// Update a user (POST request)
func updateUser() async {
networking.request(
path: "/api/updateUser",
method: .post([
"firstname": "Wesley",
"lastname": "de Groot"
])
) { response in
print(response.string)
}
}
/// Get all users (GET request)
func getUsers() async {
networking.request(
path: "/api/getUsers",
method: .get
) { response in
struct userProfile: Codable {
let firstname: String
let lastname: String
}
// Decode the response
let userProfiles: [userProfile]? = networkResponse.decoded()
print(userProfiles)
}
}
}
Debugging
Enable Debugging
import SimpleNetworking
/// Debug: print NSURLRequest (Default: false)
SimpleNetworking.shared.debug.requestURL = true
/// Debug: print sent HTTP Headers (Default: false)
SimpleNetworking.shared.debug.requestHeaders = true
/// Debug: print sent Cookies (Default: false)
SimpleNetworking.shared.debug.requestCookies = true
/// Debug: print sent Body (Default: false)
SimpleNetworking.shared.debug.requestBody = true
/// Debug: print received HTTP Headers (Default: false)
SimpleNetworking.shared.debug.responseHeaders = true
/// Debug: print received Body (Default: false)
SimpleNetworking.shared.debug.responseBody = true
/// Debug: print received JSON (if any) (Default: false)
SimpleNetworking.shared.debug.responseJSON = true
Example debug output (GET request)
Request:
GET https://wesleydegroot.nl/
Headers:
Content-Type: application/json
User-Agent: Simple Networking (https://github.com/0xWDG/SimpleNetworking)
Cookies:
Body:
HTTPURLResponse:
HTTP 200
Cache-Control: max-age=60, private, proxy-revalidate
Content-Encoding: gzip
Server: Apache/2
Content-Type: text/html; charset=UTF-8
Vary: Accept-Encoding,User-Agent
Date: Mon, 26 Feb 2024 19:50:42 GMT
Body:
... HTTP BODY ...
Decoded JSON:
Unable to parse JSON
Example debug output (POST request)
Request:
POST https://wesleydegroot.nl/api/userUpdate
Headers:
User-Agent: Simple Networking (https://github.com/0xWDG/SimpleNetworking)
Content-Type: application/json
Cookies:
Body:
{"age":33,"firstname":"Wesley","lastname":"de Groot"}
HTTPURLResponse:
HTTP 200
Content-Encoding: gzip
Content-Type: text/html; charset=UTF-8
Vary: Accept-Encoding,User-Agent
Cache-Control: max-age=60, private, proxy-revalidate
Server: Apache/2
Date: Mon, 26 Feb 2024 19:52:58 GMT
Body:
{"status":"user updated","code":200}
Decoded JSON:
["status": user updated, "code": 200]
Wrap up
SimpleNetworking is a simple networking library for Swift it is a wrapper around URLSession and provides a variety of functions to make easy network requests.
You can find the source code and documentation at GitHub https://github.com/0xWDG/SimpleNetworking.
If you have any questions or suggestions, feel free to contact me on Twitter/X or comment below this post.
Read more
- Translating closures to async • 4 minutes reading time.
- Implementing Sign in with Apple • 6 minutes reading time.
- SwiftUI Development with DynamicUI • 4 minutes reading time.
Share
Share Mastodon Twitter LinkedIn Facebook