SimpleNetworking
Tags: simplenetworking, swift
Published: 2024-02-27 16:04:32

---
title: SimpleNetworking
published: true
date: 2024-02-26 20:11:59 UTC
tags: SimpleNetworking,Swift
canonical_url: https://wesleydegroot.nl/blog/post/SimpleNetworking
---

<p>Today i write about my Swift package <a href="https://github.com/0xWDG/SimpleNetworking">SimpleNetworking</a>.<br> 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.<br> You can find the source code and documentation at GitHub <a href="https://github.com/0xWDG/SimpleNetworking">https://github.com/0xWDG/SimpleNetworking</a>.</p> <h3>Installation</h3> <p>Install using Swift Package Manager</p> <pre><code class="language-swift">dependencies: [.package(url: "https://github.com/0xWDG/SimpleNetworking.git", .branch("main")),], targets: [.target(name: "MyTarget", dependencies: [ .product(name: "SimpleNetworking", package: "SimpleNetworking"),]), ]</code></pre> <h3>Async / Await Examples</h3> <pre><code class="language-swift">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) } }</code></pre> <h3>Closure Examples</h3> <details> <summary>Closure Examples</summary> <pre><code class="language-swift">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) } } }</code></pre> </details> <h3>Debugging</h3> <details> <summary>Enable Debugging</summary> <pre><code class="language-swift">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</code></pre> </details> <details> <summary>Example debug output (GET request)</summary> <pre><code>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</code></pre> </details> <details> <summary>Example debug output (POST request)</summary> <pre><code>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]</code></pre> </details> <h3>Wrap up</h3> <p>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.<br> You can find the source code and documentation at GitHub <a href="https://github.com/0xWDG/SimpleNetworking">https://github.com/0xWDG/SimpleNetworking</a>.</p> <p>If you have any questions or suggestions, feel free to contact me on <a href="https://twitter.com/0xWDG">Twitter/X</a> or comment below this post.</p>