SwiftNetzy is an open-source library that simplifies HTTP communication in Swift. This library is using Swift Concurrency and URLSession to handle HTTP communication asynchronously.
- Easy-to-use API for making HTTP requests
- Support HTTP methods(get, post, put, delete available. others will be supported)
- Support defining API endpoints with a simple, readable syntax
- Send URL query parameters or make HTTP requests with URL-encoded or JSON-encoded parameters
SwiftNetzy can be installed using Swift Package Manager.
Add the following line to your dependencies array in Package.swift.
In the project, select File > Swift Packages > Add Package Dependency.
dependencies: [
.package(url: "https://github.com/yudonlee/SwiftNetzy.git", from: "1.0.0")
]
To make an HTTP request using SwiftNetzy, simply call the request function, passing in the URL, method, headers, parameters, and body as needed. For example:
struct Post: Codable {
let userId: Int
let id: Int
let title: String
let body: String
}
func exampleAsyncFunction() async throws {
let url = URL(string: "https://jsonplaceholder.typicode.com/posts/1")!
let headers = ["Authorization": "Bearer <YOUR_ACCESS_TOKEN>"]
do {
let post: Post = try await SwiftNetzy.request(Post.self, url, method: .get, headers: headers)
print(post)
} catch {
print(error)
}
}
To make an HTTP request using SNRouter And URLTarget, SNRouter initializes a generic type that conforms to the URLTarget Protocol.
struct HttpBinResponseModel: Codable {
let url: String
}
public enum APIEndPointEasily {
case example(body: [String: String], parameter: [String: String])
}
extension APIEndPointEasily: URLTarget {
public var task: URLRequestTask {
switch self {
case .example(let body, let parameter):
return .bodyParametersAndURLParameters(bodyParameters: body, bodyEncoding: URLEncoding.httpBody, urlParameters: parameter)
}
}
public var baseURL: String {
switch self {
case .example:
return "https://httpbin.org"
}
}
public var path: String {
switch self {
case .example:
return "/post"
}
}
public var headers: [String : String] {
switch self {
case .example:
return ["Content-Type": "application/x-www-form-urlencoded"]
}
}
public var method: HTTPMethod {
switch self {
case .example:
return .post
}
}
}
func apiEndPointEasily() async throws {
let router = SNRouter<APIEndPointEasily>()
let body = [
"foo": "bar",
"baz": "qux"
]
let param = [
"param1": "value1",
"param2": "value2"
]
let postResponse = try await router.request(target: .example(body: body, parameter: param), type: HttpBinResponseModel.self)
print(postResponse.url)
}
SwiftNetzy is released under the MIT license. See LICENSE for more information.