https://github.com/astrian/swift-async-network
An async-favored network package used in Swift
https://github.com/astrian/swift-async-network
Last synced: 4 months ago
JSON representation
An async-favored network package used in Swift
- Host: GitHub
- URL: https://github.com/astrian/swift-async-network
- Owner: Astrian
- License: mit
- Created: 2022-04-27T06:07:06.000Z (about 4 years ago)
- Default Branch: main
- Last Pushed: 2024-09-06T11:24:48.000Z (almost 2 years ago)
- Last Synced: 2025-10-20T01:31:40.193Z (8 months ago)
- Language: Swift
- Size: 19.5 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Swift Async Network
Swift Async Network (SAN) is a package used to make requests in the Swift program.
## Usage
### General method
You can use `try await SAN.request("METHOD", "https://example.com/endpoint")` to execute a network request.
``` swift
import SwiftAsyncNetwork
func someFunction() async {
let (data, res) = try await SAN.request(
"GET", "https://httpstat.us/200",
params: SANReqParams(
query: ["hello": "world"],
header: ["Content-Type": "application/json", "Accept": "application/json"]
)
)
}
```
### Shortcut methods
The HTTP method can be executed as a shortcut method.
``` swift
import SwiftAsyncNetwork
func someFunction() async {
let (data, res) = try await SAN.POST(
"https://httpstat.us/200",
params: SANReqParams(
query: ["hello": "world"],
body: ["key": "value"],
header: ["Content-Type": "application/json", "Accept": "application/json"]
)
)
}
```
### Authentication
SAN supports the `Basic` and `Bearer` authentication header structures.
``` swift
import SwiftAsyncNetwork
func someFunction() async {
// Basic auth
let (data, res) = try await SAN.POST(
"https://httpstat.us/200",
params: SANReqParams(
auth: SANAuthCred(username: "ren_amaniya", password: "takeurheart")
)
)
// Bearer auth
let (data, res) = try await SAN.POST(
"https://httpstat.us/200",
params: SANReqParams(
auth: SANAuthCred(token: "kaitochannel")
)
)
}
```
### Instantiation
You can create a unique SAN instance to manage the config (domain or base URL of the backend, headers, authentication parameters, etc.) at the instance level. Note that the `body` and `query` parameters are not supported inside the instance.
You can also pass the SAN instance through the environment objects to create and manage the network requests at the whole project level.
Shortcut methods and config overwritten features are also supported in the instance.
``` swift
import SwiftAsyncNetwork
import SwiftUI
struct magusApp: App {
var backend = try! SANInstance(baseURL: "https://backe.nd/", params: SANReqParams(
auth: SANAuthCred(username: "ren_amaniya", password: "takeurheart")
))
var body: some Scene {
WindowGroup {
ContentView().environmentObject(backend)
}
}
}
struct ContentView: View {
@EnvironmentObject var backend: SANInstance
var body: some View {
VStack {
Text("Hello World!")
}.onAppear {
Task {
let (data, _) = try await backend.GET("/endpoint.json", params: SANReqParams(query: ["page":"1"]))
let dataString = String(data: data, encoding: .utf8)
print(dataString ?? "")
}
}
}
}
```