Skip to content

SwiftNetzy is a lightweight library that uses Swift Concurrency to make HTTP communication in Swift easier.

License

Notifications You must be signed in to change notification settings

yudonlee/SwiftNetzy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

SwiftNetzy

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.

Swift 5.7 Badge macOS 12.0 Swift Package Manager Badge

Features

  • 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

Installation

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")
]

Usage

SwiftNetzy Example

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)
    }
}

API Endpoint Example

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)
    
}

License

SwiftNetzy is released under the MIT license. See LICENSE for more information.

About

SwiftNetzy is a lightweight library that uses Swift Concurrency to make HTTP communication in Swift easier.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages